Mutex.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c)2013-2020 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. // If C++17 is available use std::mutex and std::shared_mutex as
  17. // these will probably use whatever is fastest on a given platform.
  18. // Older compilers require pthreads to be available. The compiler
  19. // now used on Windows is new enough to use C++17 stuff, so no more
  20. // need for Windows-specific implementations here.
  21. #if __cplusplus >= 201703L
  22. #include <mutex>
  23. #include <shared_mutex>
  24. #else
  25. #define ZT_USE_PTHREADS
  26. #ifndef __WINDOWS__
  27. #include <pthread.h>
  28. #endif
  29. #endif
  30. namespace ZeroTier {
  31. /**
  32. * A simple mutual exclusion lock.
  33. */
  34. class Mutex
  35. {
  36. public:
  37. #ifdef ZT_USE_PTHREADS
  38. ZT_INLINE Mutex() noexcept { pthread_mutex_init(&_mh,nullptr); }
  39. ZT_INLINE ~Mutex() noexcept { pthread_mutex_destroy(&_mh); }
  40. ZT_INLINE void lock() const noexcept { pthread_mutex_lock(&((const_cast <Mutex *> (this))->_mh)); }
  41. ZT_INLINE void unlock() const noexcept { pthread_mutex_unlock(&((const_cast <Mutex *> (this))->_mh)); }
  42. #else
  43. ZT_INLINE Mutex() noexcept : _m() {}
  44. ZT_INLINE void lock() const noexcept { const_cast<Mutex *>(this)->_m.lock(); }
  45. ZT_INLINE void unlock() const noexcept { const_cast<Mutex *>(this)->_m.unlock(); }
  46. #endif
  47. class Lock
  48. {
  49. public:
  50. explicit ZT_INLINE Lock(Mutex &m) noexcept : _m(&m) { m.lock(); }
  51. explicit ZT_INLINE Lock(const Mutex &m) noexcept : _m(const_cast<Mutex *>(&m)) { _m->lock(); }
  52. ZT_INLINE ~Lock() { _m->unlock(); }
  53. private:
  54. Mutex *const _m;
  55. };
  56. private:
  57. ZT_INLINE Mutex(const Mutex &) noexcept {}
  58. ZT_INLINE const Mutex &operator=(const Mutex &) noexcept { return *this; }
  59. #ifdef ZT_USE_PTHREADS
  60. pthread_mutex_t _mh;
  61. #else
  62. std::mutex _m;
  63. #endif
  64. };
  65. /**
  66. * A lock allowing multiple threads to read but making all wait on any writing thread.
  67. */
  68. class RWMutex
  69. {
  70. public:
  71. #ifdef ZT_USE_PTHREADS
  72. ZT_INLINE RWMutex() noexcept { pthread_rwlock_init(&_mh,nullptr); }
  73. ZT_INLINE ~RWMutex() noexcept { pthread_rwlock_destroy(&_mh); }
  74. ZT_INLINE void lock() const noexcept { pthread_rwlock_wrlock(&((const_cast <RWMutex *> (this))->_mh)); }
  75. ZT_INLINE void rlock() const noexcept { pthread_rwlock_rdlock(&((const_cast <RWMutex *> (this))->_mh)); }
  76. ZT_INLINE void unlock() const noexcept { pthread_rwlock_unlock(&((const_cast <RWMutex *> (this))->_mh)); }
  77. ZT_INLINE void runlock() const noexcept { pthread_rwlock_unlock(&((const_cast <RWMutex *> (this))->_mh)); }
  78. #else
  79. ZT_INLINE RWMutex() noexcept : _m() {}
  80. ZT_INLINE void lock() const noexcept { const_cast<RWMutex *>(this)->_m.lock(); }
  81. ZT_INLINE void rlock() const noexcept { const_cast<RWMutex *>(this)->_m.lock_shared(); }
  82. ZT_INLINE void unlock() const noexcept { const_cast<RWMutex *>(this)->_m.unlock(); }
  83. ZT_INLINE void runlock() const noexcept { const_cast<RWMutex *>(this)->_m.unlock_shared(); }
  84. #endif
  85. /**
  86. * RAAI locker that acquires only the read lock (shared read)
  87. */
  88. class RLock
  89. {
  90. public:
  91. explicit ZT_INLINE RLock(RWMutex &m) noexcept : _m(&m) { m.rlock(); }
  92. explicit ZT_INLINE RLock(const RWMutex &m) noexcept : _m(const_cast<RWMutex *>(&m)) { _m->rlock(); }
  93. ZT_INLINE ~RLock() { _m->runlock(); }
  94. private:
  95. RWMutex *const _m;
  96. };
  97. /**
  98. * RAAI locker that acquires the write lock (exclusive write, no readers)
  99. */
  100. class Lock
  101. {
  102. public:
  103. explicit ZT_INLINE Lock(RWMutex &m) noexcept : _m(&m) { m.lock(); }
  104. explicit ZT_INLINE Lock(const RWMutex &m) noexcept : _m(const_cast<RWMutex *>(&m)) { _m->lock(); }
  105. ZT_INLINE ~Lock() { _m->unlock(); }
  106. private:
  107. RWMutex *const _m;
  108. };
  109. /**
  110. * RAAI locker that acquires the read lock first and can switch to writing.
  111. *
  112. * Use writing() to acquire the write lock if not already acquired. Use reading() to
  113. * let go of the write lock and go back to only holding the read lock. Note that on
  114. * most platforms there's a brief moment where the lock is unlocked during the
  115. * transition, meaning protected variable states can change. Code must not assume
  116. * that the lock is held constantly if writing() is used to change mode.
  117. */
  118. class RMaybeWLock
  119. {
  120. public:
  121. explicit ZT_INLINE RMaybeWLock(RWMutex &m) noexcept : _m(&m),_w(false) { m.rlock(); }
  122. explicit ZT_INLINE RMaybeWLock(const RWMutex &m) noexcept : _m(const_cast<RWMutex *>(&m)),_w(false) { _m->rlock(); }
  123. ZT_INLINE void writing() noexcept { if (!_w) { _w = true; _m->runlock(); _m->lock(); } }
  124. ZT_INLINE void reading() noexcept { if (_w) { _w = false; _m->unlock(); _m->rlock(); } }
  125. ZT_INLINE bool isWriting() const noexcept { return _w; }
  126. ZT_INLINE ~RMaybeWLock() { if (_w) _m->unlock(); else _m->runlock(); }
  127. private:
  128. RWMutex *const _m;
  129. bool _w;
  130. };
  131. private:
  132. ZT_INLINE RWMutex(const RWMutex &) noexcept {}
  133. ZT_INLINE const RWMutex &operator=(const RWMutex &) noexcept { return *this; }
  134. #ifdef ZT_USE_PTHREADS
  135. pthread_rwlock_t _mh;
  136. #else
  137. std::shared_mutex _m;
  138. #endif
  139. };
  140. } // namespace ZeroTier
  141. #endif