Mutex.hpp 4.3 KB

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