Mutex.hpp 2.9 KB

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