Mutex.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 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 "Constants.hpp"
  30. #include "NonCopyable.hpp"
  31. #ifdef __UNIX_LIKE__
  32. #include <stdlib.h>
  33. #include <pthread.h>
  34. namespace ZeroTier {
  35. class Mutex : NonCopyable
  36. {
  37. public:
  38. Mutex()
  39. throw()
  40. {
  41. pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
  42. }
  43. ~Mutex()
  44. {
  45. pthread_mutex_destroy(&_mh);
  46. }
  47. inline void lock()
  48. throw()
  49. {
  50. pthread_mutex_lock(&_mh);
  51. }
  52. inline void unlock()
  53. throw()
  54. {
  55. pthread_mutex_unlock(&_mh);
  56. }
  57. inline void lock() const
  58. throw()
  59. {
  60. (const_cast <Mutex *> (this))->lock();
  61. }
  62. inline void unlock() const
  63. throw()
  64. {
  65. (const_cast <Mutex *> (this))->unlock();
  66. }
  67. /**
  68. * Uses C++ contexts and constructor/destructor to lock/unlock automatically
  69. */
  70. class Lock : NonCopyable
  71. {
  72. public:
  73. Lock(Mutex &m)
  74. throw() :
  75. _m(&m)
  76. {
  77. m.lock();
  78. }
  79. Lock(const Mutex &m)
  80. throw() :
  81. _m(const_cast<Mutex *>(&m))
  82. {
  83. _m->lock();
  84. }
  85. ~Lock()
  86. {
  87. _m->unlock();
  88. }
  89. private:
  90. Mutex *const _m;
  91. };
  92. private:
  93. pthread_mutex_t _mh;
  94. };
  95. } // namespace ZeroTier
  96. #endif // Apple / Linux
  97. #ifdef __WINDOWS__
  98. #include <stdlib.h>
  99. #include <Windows.h>
  100. namespace ZeroTier {
  101. class Mutex : NonCopyable
  102. {
  103. public:
  104. Mutex()
  105. throw()
  106. {
  107. InitializeCriticalSection(&_cs);
  108. }
  109. ~Mutex()
  110. {
  111. DeleteCriticalSection(&_cs);
  112. }
  113. inline void lock()
  114. throw()
  115. {
  116. EnterCriticalSection(&_cs);
  117. }
  118. inline void unlock()
  119. throw()
  120. {
  121. LeaveCriticalSection(&_cs);
  122. }
  123. inline void lock() const
  124. throw()
  125. {
  126. (const_cast <Mutex *> (this))->lock();
  127. }
  128. inline void unlock() const
  129. throw()
  130. {
  131. (const_cast <Mutex *> (this))->unlock();
  132. }
  133. class Lock : NonCopyable
  134. {
  135. public:
  136. Lock(Mutex &m)
  137. throw() :
  138. _m(&m)
  139. {
  140. m.lock();
  141. }
  142. Lock(const Mutex &m)
  143. throw() :
  144. _m(const_cast<Mutex *>(&m))
  145. {
  146. _m->lock();
  147. }
  148. ~Lock()
  149. {
  150. _m->unlock();
  151. }
  152. private:
  153. Mutex *const _m;
  154. };
  155. private:
  156. CRITICAL_SECTION _cs;
  157. };
  158. } // namespace ZeroTier
  159. #endif // _WIN32
  160. #endif