Mutex.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_MUTEX_HPP
  14. #define ZT_MUTEX_HPP
  15. #include "Constants.hpp"
  16. #ifdef __UNIX_LIKE__
  17. #include <pthread.h>
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. namespace ZeroTier {
  21. // libpthread based mutex lock
  22. class Mutex {
  23. public:
  24. Mutex()
  25. {
  26. pthread_mutex_init(&_mh, (const pthread_mutexattr_t*)0);
  27. }
  28. ~Mutex()
  29. {
  30. pthread_mutex_destroy(&_mh);
  31. }
  32. inline void lock() const
  33. {
  34. pthread_mutex_lock(&((const_cast<Mutex*>(this))->_mh));
  35. }
  36. inline void unlock() const
  37. {
  38. pthread_mutex_unlock(&((const_cast<Mutex*>(this))->_mh));
  39. }
  40. class Lock {
  41. public:
  42. Lock(Mutex& m) : _m(&m)
  43. {
  44. m.lock();
  45. }
  46. Lock(const Mutex& m) : _m(const_cast<Mutex*>(&m))
  47. {
  48. _m->lock();
  49. }
  50. ~Lock()
  51. {
  52. _m->unlock();
  53. }
  54. private:
  55. Mutex* const _m;
  56. };
  57. private:
  58. Mutex(const Mutex&)
  59. {
  60. }
  61. const Mutex& operator=(const Mutex&)
  62. {
  63. return *this;
  64. }
  65. pthread_mutex_t _mh;
  66. };
  67. } // namespace ZeroTier
  68. #endif
  69. #ifdef __WINDOWS__
  70. #include <stdlib.h>
  71. #include <windows.h>
  72. namespace ZeroTier {
  73. // Windows critical section based lock
  74. class Mutex {
  75. public:
  76. Mutex()
  77. {
  78. InitializeCriticalSection(&_cs);
  79. }
  80. ~Mutex()
  81. {
  82. DeleteCriticalSection(&_cs);
  83. }
  84. inline void lock()
  85. {
  86. EnterCriticalSection(&_cs);
  87. }
  88. inline void unlock()
  89. {
  90. LeaveCriticalSection(&_cs);
  91. }
  92. inline void lock() const
  93. {
  94. (const_cast<Mutex*>(this))->lock();
  95. }
  96. inline void unlock() const
  97. {
  98. (const_cast<Mutex*>(this))->unlock();
  99. }
  100. class Lock {
  101. public:
  102. Lock(Mutex& m) : _m(&m)
  103. {
  104. m.lock();
  105. }
  106. Lock(const Mutex& m) : _m(const_cast<Mutex*>(&m))
  107. {
  108. _m->lock();
  109. }
  110. ~Lock()
  111. {
  112. _m->unlock();
  113. }
  114. private:
  115. Mutex* const _m;
  116. };
  117. private:
  118. Mutex(const Mutex&)
  119. {
  120. }
  121. const Mutex& operator=(const Mutex&)
  122. {
  123. return *this;
  124. }
  125. CRITICAL_SECTION _cs;
  126. };
  127. } // namespace ZeroTier
  128. #endif // _WIN32
  129. #endif