Mutex.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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: 2023-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. ZT_ALWAYS_INLINE Mutex() :
  27. nextTicket(0),
  28. nowServing(0)
  29. {
  30. }
  31. ZT_ALWAYS_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. ZT_ALWAYS_INLINE void unlock() const { ++(const_cast<Mutex *>(this)->nowServing); }
  40. /**
  41. * Uses C++ contexts and constructor/destructor to lock/unlock automatically
  42. */
  43. class Lock
  44. {
  45. public:
  46. ZT_ALWAYS_INLINE Lock(Mutex &m) : _m(&m) { m.lock(); }
  47. ZT_ALWAYS_INLINE Lock(const Mutex &m) : _m(const_cast<Mutex *>(&m)) { _m->lock(); }
  48. ZT_ALWAYS_INLINE ~Lock() { _m->unlock(); }
  49. private:
  50. Mutex *const _m;
  51. };
  52. private:
  53. inline Mutex(const Mutex &) {}
  54. const Mutex &operator=(const Mutex &) { return *this; }
  55. uint16_t nextTicket;
  56. uint16_t nowServing;
  57. };
  58. #else
  59. // libpthread based mutex lock
  60. class Mutex
  61. {
  62. public:
  63. ZT_ALWAYS_INLINE Mutex()
  64. {
  65. pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
  66. }
  67. ZT_ALWAYS_INLINE ~Mutex()
  68. {
  69. pthread_mutex_destroy(&_mh);
  70. }
  71. ZT_ALWAYS_INLINE void lock() const
  72. {
  73. pthread_mutex_lock(&((const_cast <Mutex *> (this))->_mh));
  74. }
  75. ZT_ALWAYS_INLINE void unlock() const
  76. {
  77. pthread_mutex_unlock(&((const_cast <Mutex *> (this))->_mh));
  78. }
  79. class Lock
  80. {
  81. public:
  82. ZT_ALWAYS_INLINE Lock(Mutex &m) :
  83. _m(&m)
  84. {
  85. m.lock();
  86. }
  87. ZT_ALWAYS_INLINE Lock(const Mutex &m) :
  88. _m(const_cast<Mutex *>(&m))
  89. {
  90. _m->lock();
  91. }
  92. ZT_ALWAYS_INLINE ~Lock()
  93. {
  94. _m->unlock();
  95. }
  96. private:
  97. Mutex *const _m;
  98. };
  99. private:
  100. inline Mutex(const Mutex &) {}
  101. const Mutex &operator=(const Mutex &) { return *this; }
  102. pthread_mutex_t _mh;
  103. };
  104. #endif
  105. } // namespace ZeroTier
  106. #endif // Apple / Linux
  107. #ifdef __WINDOWS__
  108. #include <stdlib.h>
  109. #include <Windows.h>
  110. namespace ZeroTier {
  111. // Windows critical section based lock
  112. class Mutex
  113. {
  114. public:
  115. inline Mutex()
  116. {
  117. InitializeCriticalSection(&_cs);
  118. }
  119. inline ~Mutex()
  120. {
  121. DeleteCriticalSection(&_cs);
  122. }
  123. inline void lock()
  124. {
  125. EnterCriticalSection(&_cs);
  126. }
  127. inline void unlock()
  128. {
  129. LeaveCriticalSection(&_cs);
  130. }
  131. inline void lock() const
  132. {
  133. (const_cast <Mutex *> (this))->lock();
  134. }
  135. inline void unlock() const
  136. {
  137. (const_cast <Mutex *> (this))->unlock();
  138. }
  139. class Lock
  140. {
  141. public:
  142. inline Lock(Mutex &m) :
  143. _m(&m)
  144. {
  145. m.lock();
  146. }
  147. inline Lock(const Mutex &m) :
  148. _m(const_cast<Mutex *>(&m))
  149. {
  150. _m->lock();
  151. }
  152. inline ~Lock()
  153. {
  154. _m->unlock();
  155. }
  156. private:
  157. Mutex *const _m;
  158. };
  159. private:
  160. inline Mutex(const Mutex &) {}
  161. const Mutex &operator=(const Mutex &) { return *this; }
  162. CRITICAL_SECTION _cs;
  163. };
  164. } // namespace ZeroTier
  165. #endif // _WIN32
  166. #endif