Mutex.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_MUTEX_HPP
  14. #define ZT_MUTEX_HPP
  15. #include "Constants.hpp"
  16. #ifdef __UNIX_LIKE__
  17. #include <stdint.h>
  18. #include <stdlib.h>
  19. #include <pthread.h>
  20. namespace ZeroTier {
  21. #if defined(__GNUC__) && (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
  22. // Inline ticket lock on x64 systems with GCC and CLANG (Mac, Linux) -- this is really fast as long as locking durations are very short
  23. class Mutex
  24. {
  25. public:
  26. Mutex() :
  27. nextTicket(0),
  28. nowServing(0)
  29. {
  30. }
  31. inline void lock() const
  32. {
  33. const uint16_t myTicket = __sync_fetch_and_add(&(const_cast<Mutex *>(this)->nextTicket),1);
  34. while (nowServing != myTicket) {
  35. __asm__ __volatile__("rep;nop"::);
  36. __asm__ __volatile__("":::"memory");
  37. }
  38. }
  39. inline void unlock() const
  40. {
  41. ++(const_cast<Mutex *>(this)->nowServing);
  42. }
  43. /**
  44. * Uses C++ contexts and constructor/destructor to lock/unlock automatically
  45. */
  46. class Lock
  47. {
  48. public:
  49. Lock(Mutex &m) :
  50. _m(&m)
  51. {
  52. m.lock();
  53. }
  54. Lock(const Mutex &m) :
  55. _m(const_cast<Mutex *>(&m))
  56. {
  57. _m->lock();
  58. }
  59. ~Lock()
  60. {
  61. _m->unlock();
  62. }
  63. private:
  64. Mutex *const _m;
  65. };
  66. private:
  67. Mutex(const Mutex &) {}
  68. const Mutex &operator=(const Mutex &) { return *this; }
  69. uint16_t nextTicket;
  70. uint16_t nowServing;
  71. };
  72. #else
  73. // libpthread based mutex lock
  74. class Mutex
  75. {
  76. public:
  77. Mutex()
  78. {
  79. pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
  80. }
  81. ~Mutex()
  82. {
  83. pthread_mutex_destroy(&_mh);
  84. }
  85. inline void lock() const
  86. {
  87. pthread_mutex_lock(&((const_cast <Mutex *> (this))->_mh));
  88. }
  89. inline void unlock() const
  90. {
  91. pthread_mutex_unlock(&((const_cast <Mutex *> (this))->_mh));
  92. }
  93. class Lock
  94. {
  95. public:
  96. Lock(Mutex &m) :
  97. _m(&m)
  98. {
  99. m.lock();
  100. }
  101. Lock(const Mutex &m) :
  102. _m(const_cast<Mutex *>(&m))
  103. {
  104. _m->lock();
  105. }
  106. ~Lock()
  107. {
  108. _m->unlock();
  109. }
  110. private:
  111. Mutex *const _m;
  112. };
  113. private:
  114. Mutex(const Mutex &) {}
  115. const Mutex &operator=(const Mutex &) { return *this; }
  116. pthread_mutex_t _mh;
  117. };
  118. #endif
  119. } // namespace ZeroTier
  120. #endif // Apple / Linux
  121. #ifdef __WINDOWS__
  122. #include <stdlib.h>
  123. #include <Windows.h>
  124. namespace ZeroTier {
  125. // Windows critical section based lock
  126. class Mutex
  127. {
  128. public:
  129. Mutex()
  130. {
  131. InitializeCriticalSection(&_cs);
  132. }
  133. ~Mutex()
  134. {
  135. DeleteCriticalSection(&_cs);
  136. }
  137. inline void lock()
  138. {
  139. EnterCriticalSection(&_cs);
  140. }
  141. inline void unlock()
  142. {
  143. LeaveCriticalSection(&_cs);
  144. }
  145. inline void lock() const
  146. {
  147. (const_cast <Mutex *> (this))->lock();
  148. }
  149. inline void unlock() const
  150. {
  151. (const_cast <Mutex *> (this))->unlock();
  152. }
  153. class Lock
  154. {
  155. public:
  156. Lock(Mutex &m) :
  157. _m(&m)
  158. {
  159. m.lock();
  160. }
  161. Lock(const Mutex &m) :
  162. _m(const_cast<Mutex *>(&m))
  163. {
  164. _m->lock();
  165. }
  166. ~Lock()
  167. {
  168. _m->unlock();
  169. }
  170. private:
  171. Mutex *const _m;
  172. };
  173. private:
  174. Mutex(const Mutex &) {}
  175. const Mutex &operator=(const Mutex &) { return *this; }
  176. CRITICAL_SECTION _cs;
  177. };
  178. } // namespace ZeroTier
  179. #endif // _WIN32
  180. #endif