Mutex.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. *
  5. * (c) ZeroTier, Inc.
  6. * https://www.zerotier.com/
  7. */
  8. #ifndef ZT_MUTEX_HPP
  9. #define ZT_MUTEX_HPP
  10. #include "Constants.hpp"
  11. #ifdef __UNIX_LIKE__
  12. #include <pthread.h>
  13. #include <stdlib.h>
  14. namespace ZeroTier {
  15. // libpthread based mutex lock
  16. class Mutex {
  17. public:
  18. Mutex()
  19. {
  20. pthread_mutex_init(&_mh, (const pthread_mutexattr_t*)0);
  21. }
  22. ~Mutex()
  23. {
  24. pthread_mutex_destroy(&_mh);
  25. }
  26. inline void lock() const
  27. {
  28. pthread_mutex_lock(&((const_cast<Mutex*>(this))->_mh));
  29. }
  30. inline void unlock() const
  31. {
  32. pthread_mutex_unlock(&((const_cast<Mutex*>(this))->_mh));
  33. }
  34. class Lock {
  35. public:
  36. Lock(Mutex& m) : _m(&m)
  37. {
  38. m.lock();
  39. }
  40. Lock(const Mutex& m) : _m(const_cast<Mutex*>(&m))
  41. {
  42. _m->lock();
  43. }
  44. ~Lock()
  45. {
  46. _m->unlock();
  47. }
  48. private:
  49. Mutex* const _m;
  50. };
  51. private:
  52. Mutex(const Mutex&)
  53. {
  54. }
  55. const Mutex& operator=(const Mutex&)
  56. {
  57. return *this;
  58. }
  59. pthread_mutex_t _mh;
  60. };
  61. } // namespace ZeroTier
  62. #endif
  63. #ifdef __WINDOWS__
  64. #include <stdlib.h>
  65. #include <windows.h>
  66. namespace ZeroTier {
  67. // Windows critical section based lock
  68. class Mutex {
  69. public:
  70. Mutex()
  71. {
  72. InitializeCriticalSection(&_cs);
  73. }
  74. ~Mutex()
  75. {
  76. DeleteCriticalSection(&_cs);
  77. }
  78. inline void lock()
  79. {
  80. EnterCriticalSection(&_cs);
  81. }
  82. inline void unlock()
  83. {
  84. LeaveCriticalSection(&_cs);
  85. }
  86. inline void lock() const
  87. {
  88. (const_cast<Mutex*>(this))->lock();
  89. }
  90. inline void unlock() const
  91. {
  92. (const_cast<Mutex*>(this))->unlock();
  93. }
  94. class Lock {
  95. public:
  96. Lock(Mutex& m) : _m(&m)
  97. {
  98. m.lock();
  99. }
  100. Lock(const Mutex& m) : _m(const_cast<Mutex*>(&m))
  101. {
  102. _m->lock();
  103. }
  104. ~Lock()
  105. {
  106. _m->unlock();
  107. }
  108. private:
  109. Mutex* const _m;
  110. };
  111. private:
  112. Mutex(const Mutex&)
  113. {
  114. }
  115. const Mutex& operator=(const Mutex&)
  116. {
  117. return *this;
  118. }
  119. CRITICAL_SECTION _cs;
  120. };
  121. } // namespace ZeroTier
  122. #endif // _WIN32
  123. #endif