Mutex.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. inline 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. inline Lock(Mutex &m) : _m(&m) { m.lock(); }
  63. inline Lock(const Mutex &m) : _m(const_cast<Mutex *>(&m)) { _m->lock(); }
  64. inline ~Lock() { _m->unlock(); }
  65. private:
  66. Mutex *const _m;
  67. };
  68. private:
  69. inline Mutex(const Mutex &) {}
  70. const Mutex &operator=(const Mutex &) { return *this; }
  71. uint16_t nextTicket;
  72. uint16_t nowServing;
  73. };
  74. #else
  75. // libpthread based mutex lock
  76. class Mutex
  77. {
  78. public:
  79. inline Mutex()
  80. {
  81. pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
  82. }
  83. inline ~Mutex()
  84. {
  85. pthread_mutex_destroy(&_mh);
  86. }
  87. inline void lock() const
  88. {
  89. pthread_mutex_lock(&((const_cast <Mutex *> (this))->_mh));
  90. }
  91. inline void unlock() const
  92. {
  93. pthread_mutex_unlock(&((const_cast <Mutex *> (this))->_mh));
  94. }
  95. class Lock
  96. {
  97. public:
  98. inline Lock(Mutex &m) :
  99. _m(&m)
  100. {
  101. m.lock();
  102. }
  103. inline Lock(const Mutex &m) :
  104. _m(const_cast<Mutex *>(&m))
  105. {
  106. _m->lock();
  107. }
  108. inline ~Lock()
  109. {
  110. _m->unlock();
  111. }
  112. private:
  113. Mutex *const _m;
  114. };
  115. private:
  116. inline Mutex(const Mutex &) {}
  117. const Mutex &operator=(const Mutex &) { return *this; }
  118. pthread_mutex_t _mh;
  119. };
  120. #endif
  121. } // namespace ZeroTier
  122. #endif // Apple / Linux
  123. #ifdef __WINDOWS__
  124. #include <stdlib.h>
  125. #include <Windows.h>
  126. namespace ZeroTier {
  127. // Windows critical section based lock
  128. class Mutex
  129. {
  130. public:
  131. inline Mutex()
  132. {
  133. InitializeCriticalSection(&_cs);
  134. }
  135. inline ~Mutex()
  136. {
  137. DeleteCriticalSection(&_cs);
  138. }
  139. inline void lock()
  140. {
  141. EnterCriticalSection(&_cs);
  142. }
  143. inline void unlock()
  144. {
  145. LeaveCriticalSection(&_cs);
  146. }
  147. inline void lock() const
  148. {
  149. (const_cast <Mutex *> (this))->lock();
  150. }
  151. inline void unlock() const
  152. {
  153. (const_cast <Mutex *> (this))->unlock();
  154. }
  155. class Lock
  156. {
  157. public:
  158. inline Lock(Mutex &m) :
  159. _m(&m)
  160. {
  161. m.lock();
  162. }
  163. inline Lock(const Mutex &m) :
  164. _m(const_cast<Mutex *>(&m))
  165. {
  166. _m->lock();
  167. }
  168. inline ~Lock()
  169. {
  170. _m->unlock();
  171. }
  172. private:
  173. Mutex *const _m;
  174. };
  175. private:
  176. inline Mutex(const Mutex &) {}
  177. const Mutex &operator=(const Mutex &) { return *this; }
  178. CRITICAL_SECTION _cs;
  179. };
  180. } // namespace ZeroTier
  181. #endif // _WIN32
  182. #endif