Mutex.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_MUTEX_HPP
  27. #define ZT_MUTEX_HPP
  28. #include "Constants.hpp"
  29. #include "NonCopyable.hpp"
  30. #ifdef __UNIX_LIKE__
  31. #include <stdlib.h>
  32. #include <pthread.h>
  33. namespace ZeroTier {
  34. class Mutex : NonCopyable
  35. {
  36. public:
  37. Mutex()
  38. {
  39. pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
  40. }
  41. ~Mutex()
  42. {
  43. pthread_mutex_destroy(&_mh);
  44. }
  45. inline void lock()
  46. {
  47. pthread_mutex_lock(&_mh);
  48. }
  49. inline void unlock()
  50. {
  51. pthread_mutex_unlock(&_mh);
  52. }
  53. inline void lock() const
  54. {
  55. (const_cast <Mutex *> (this))->lock();
  56. }
  57. inline void unlock() const
  58. {
  59. (const_cast <Mutex *> (this))->unlock();
  60. }
  61. /**
  62. * Uses C++ contexts and constructor/destructor to lock/unlock automatically
  63. */
  64. class Lock : NonCopyable
  65. {
  66. public:
  67. Lock(Mutex &m) :
  68. _m(&m)
  69. {
  70. m.lock();
  71. }
  72. Lock(const Mutex &m) :
  73. _m(const_cast<Mutex *>(&m))
  74. {
  75. _m->lock();
  76. }
  77. ~Lock()
  78. {
  79. _m->unlock();
  80. }
  81. private:
  82. Mutex *const _m;
  83. };
  84. private:
  85. pthread_mutex_t _mh;
  86. };
  87. } // namespace ZeroTier
  88. #endif // Apple / Linux
  89. #ifdef __WINDOWS__
  90. #include <stdlib.h>
  91. #include <Windows.h>
  92. namespace ZeroTier {
  93. class Mutex : NonCopyable
  94. {
  95. public:
  96. Mutex()
  97. {
  98. InitializeCriticalSection(&_cs);
  99. }
  100. ~Mutex()
  101. {
  102. DeleteCriticalSection(&_cs);
  103. }
  104. inline void lock()
  105. {
  106. EnterCriticalSection(&_cs);
  107. }
  108. inline void unlock()
  109. {
  110. LeaveCriticalSection(&_cs);
  111. }
  112. inline void lock() const
  113. {
  114. (const_cast <Mutex *> (this))->lock();
  115. }
  116. inline void unlock() const
  117. {
  118. (const_cast <Mutex *> (this))->unlock();
  119. }
  120. class Lock : NonCopyable
  121. {
  122. public:
  123. Lock(Mutex &m) :
  124. _m(&m)
  125. {
  126. m.lock();
  127. }
  128. Lock(const Mutex &m) :
  129. _m(const_cast<Mutex *>(&m))
  130. {
  131. _m->lock();
  132. }
  133. ~Lock()
  134. {
  135. _m->unlock();
  136. }
  137. private:
  138. Mutex *const _m;
  139. };
  140. private:
  141. CRITICAL_SECTION _cs;
  142. };
  143. } // namespace ZeroTier
  144. #endif // _WIN32
  145. #endif