2
0

Mutex.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef _ZT_MUTEX_HPP
  28. #define _ZT_MUTEX_HPP
  29. #include "NonCopyable.hpp"
  30. #if defined(__APPLE__) || defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  31. #include <stdlib.h>
  32. #include <pthread.h>
  33. namespace ZeroTier {
  34. class Mutex : NonCopyable
  35. {
  36. public:
  37. Mutex()
  38. throw()
  39. {
  40. pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
  41. }
  42. ~Mutex()
  43. {
  44. pthread_mutex_destroy(&_mh);
  45. }
  46. inline void lock()
  47. throw()
  48. {
  49. pthread_mutex_lock(&_mh);
  50. }
  51. inline void unlock()
  52. throw()
  53. {
  54. pthread_mutex_unlock(&_mh);
  55. }
  56. inline void lock() const
  57. throw()
  58. {
  59. (const_cast <Mutex *> (this))->lock();
  60. }
  61. inline void unlock() const
  62. throw()
  63. {
  64. (const_cast <Mutex *> (this))->unlock();
  65. }
  66. /**
  67. * Uses C++ contexts and constructor/destructor to lock/unlock automatically
  68. */
  69. class Lock : NonCopyable
  70. {
  71. public:
  72. Lock(Mutex &m)
  73. throw() :
  74. _m(&m)
  75. {
  76. m.lock();
  77. }
  78. Lock(const Mutex &m)
  79. throw() :
  80. _m(const_cast<Mutex *>(&m))
  81. {
  82. _m->lock();
  83. }
  84. ~Lock()
  85. {
  86. _m->unlock();
  87. }
  88. private:
  89. Mutex *const _m;
  90. };
  91. private:
  92. pthread_mutex_t _mh;
  93. };
  94. } // namespace ZeroTier
  95. #endif // Apple / Linux
  96. #ifdef _WIN32
  97. #include <stdlib.h>
  98. #include <Windows.h>
  99. namespace ZeroTier {
  100. class Mutex : NonCopyable
  101. {
  102. public:
  103. Mutex()
  104. throw()
  105. {
  106. InitializeCriticalSection(&_cs);
  107. }
  108. ~Mutex()
  109. {
  110. DeleteCriticalSection(&_cs);
  111. }
  112. inline void lock()
  113. throw()
  114. {
  115. EnterCriticalSection(&_cs);
  116. }
  117. inline void unlock()
  118. throw()
  119. {
  120. LeaveCriticalSection(&_cs);
  121. }
  122. inline void lock() const
  123. throw()
  124. {
  125. (const_cast <Mutex *> (this))->lock();
  126. }
  127. inline void unlock() const
  128. throw()
  129. {
  130. (const_cast <Mutex *> (this))->unlock();
  131. }
  132. /**
  133. * Uses C++ contexts and constructor/destructor to lock/unlock automatically
  134. */
  135. class Lock : NonCopyable
  136. {
  137. public:
  138. Lock(Mutex &m)
  139. throw() :
  140. _m(&m)
  141. {
  142. m.lock();
  143. }
  144. Lock(const Mutex &m)
  145. throw() :
  146. _m(const_cast<Mutex *>(&m))
  147. {
  148. _m->lock();
  149. }
  150. ~Lock()
  151. {
  152. _m->unlock();
  153. }
  154. private:
  155. Mutex *const _m;
  156. };
  157. private:
  158. CRITICAL_SECTION _cs;
  159. };
  160. } // namespace ZeroTier
  161. #endif // _WIN32
  162. #endif