Mutex.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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: 2025-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 <stdint.h>
  18. #include <stdlib.h>
  19. #include <pthread.h>
  20. namespace ZeroTier {
  21. // libpthread based mutex lock
  22. class Mutex
  23. {
  24. public:
  25. Mutex()
  26. {
  27. pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
  28. }
  29. ~Mutex()
  30. {
  31. pthread_mutex_destroy(&_mh);
  32. }
  33. inline void lock() const
  34. {
  35. pthread_mutex_lock(&((const_cast <Mutex *> (this))->_mh));
  36. }
  37. inline void unlock() const
  38. {
  39. pthread_mutex_unlock(&((const_cast <Mutex *> (this))->_mh));
  40. }
  41. class Lock
  42. {
  43. public:
  44. Lock(Mutex &m) :
  45. _m(&m)
  46. {
  47. m.lock();
  48. }
  49. Lock(const Mutex &m) :
  50. _m(const_cast<Mutex *>(&m))
  51. {
  52. _m->lock();
  53. }
  54. ~Lock()
  55. {
  56. _m->unlock();
  57. }
  58. private:
  59. Mutex *const _m;
  60. };
  61. private:
  62. Mutex(const Mutex &) {}
  63. const Mutex &operator=(const Mutex &) { return *this; }
  64. pthread_mutex_t _mh;
  65. };
  66. } // namespace ZeroTier
  67. #endif
  68. #ifdef __WINDOWS__
  69. #include <stdlib.h>
  70. #include <windows.h>
  71. namespace ZeroTier {
  72. // Windows critical section based lock
  73. class Mutex
  74. {
  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. {
  102. public:
  103. Lock(Mutex &m) :
  104. _m(&m)
  105. {
  106. m.lock();
  107. }
  108. Lock(const Mutex &m) :
  109. _m(const_cast<Mutex *>(&m))
  110. {
  111. _m->lock();
  112. }
  113. ~Lock()
  114. {
  115. _m->unlock();
  116. }
  117. private:
  118. Mutex *const _m;
  119. };
  120. private:
  121. Mutex(const Mutex &) {}
  122. const Mutex &operator=(const Mutex &) { return *this; }
  123. CRITICAL_SECTION _cs;
  124. };
  125. } // namespace ZeroTier
  126. #endif // _WIN32
  127. #endif