Mutex.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 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 <stdint.h>
  32. #include <stdlib.h>
  33. #include <pthread.h>
  34. namespace ZeroTier {
  35. #if defined(__GNUC__) && (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
  36. // Inline ticket lock on x64 systems with GCC and CLANG (Mac, Linux) -- this is really fast as long as locking durations are very short
  37. class Mutex : NonCopyable
  38. {
  39. public:
  40. Mutex() :
  41. nextTicket(0),
  42. nowServing(0)
  43. {
  44. }
  45. inline void lock() const
  46. {
  47. const uint16_t myTicket = __sync_fetch_and_add(&(const_cast<Mutex *>(this)->nextTicket),1);
  48. while (nowServing != myTicket) {
  49. __asm__ __volatile__("rep;nop"::);
  50. __asm__ __volatile__("":::"memory");
  51. }
  52. }
  53. inline void unlock() const
  54. {
  55. ++(const_cast<Mutex *>(this)->nowServing);
  56. }
  57. /**
  58. * Uses C++ contexts and constructor/destructor to lock/unlock automatically
  59. */
  60. class Lock : NonCopyable
  61. {
  62. public:
  63. Lock(Mutex &m) :
  64. _m(&m)
  65. {
  66. m.lock();
  67. }
  68. Lock(const Mutex &m) :
  69. _m(const_cast<Mutex *>(&m))
  70. {
  71. _m->lock();
  72. }
  73. ~Lock()
  74. {
  75. _m->unlock();
  76. }
  77. private:
  78. Mutex *const _m;
  79. };
  80. private:
  81. uint16_t nextTicket;
  82. uint16_t nowServing;
  83. };
  84. #else
  85. // libpthread based mutex lock
  86. class Mutex : NonCopyable
  87. {
  88. public:
  89. Mutex()
  90. {
  91. pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
  92. }
  93. ~Mutex()
  94. {
  95. pthread_mutex_destroy(&_mh);
  96. }
  97. inline void lock() const
  98. {
  99. pthread_mutex_lock(&((const_cast <Mutex *> (this))->_mh));
  100. }
  101. inline void unlock() const
  102. {
  103. pthread_mutex_unlock(&((const_cast <Mutex *> (this))->_mh));
  104. }
  105. class Lock : NonCopyable
  106. {
  107. public:
  108. Lock(Mutex &m) :
  109. _m(&m)
  110. {
  111. m.lock();
  112. }
  113. Lock(const Mutex &m) :
  114. _m(const_cast<Mutex *>(&m))
  115. {
  116. _m->lock();
  117. }
  118. ~Lock()
  119. {
  120. _m->unlock();
  121. }
  122. private:
  123. Mutex *const _m;
  124. };
  125. private:
  126. pthread_mutex_t _mh;
  127. };
  128. #endif
  129. } // namespace ZeroTier
  130. #endif // Apple / Linux
  131. #ifdef __WINDOWS__
  132. #include <stdlib.h>
  133. #include <Windows.h>
  134. namespace ZeroTier {
  135. // Windows critical section based lock
  136. class Mutex : NonCopyable
  137. {
  138. public:
  139. Mutex()
  140. {
  141. InitializeCriticalSection(&_cs);
  142. }
  143. ~Mutex()
  144. {
  145. DeleteCriticalSection(&_cs);
  146. }
  147. inline void lock()
  148. {
  149. EnterCriticalSection(&_cs);
  150. }
  151. inline void unlock()
  152. {
  153. LeaveCriticalSection(&_cs);
  154. }
  155. inline void lock() const
  156. {
  157. (const_cast <Mutex *> (this))->lock();
  158. }
  159. inline void unlock() const
  160. {
  161. (const_cast <Mutex *> (this))->unlock();
  162. }
  163. class Lock : NonCopyable
  164. {
  165. public:
  166. Lock(Mutex &m) :
  167. _m(&m)
  168. {
  169. m.lock();
  170. }
  171. Lock(const Mutex &m) :
  172. _m(const_cast<Mutex *>(&m))
  173. {
  174. _m->lock();
  175. }
  176. ~Lock()
  177. {
  178. _m->unlock();
  179. }
  180. private:
  181. Mutex *const _m;
  182. };
  183. private:
  184. CRITICAL_SECTION _cs;
  185. };
  186. } // namespace ZeroTier
  187. #endif // _WIN32
  188. #endif