Mutex.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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: 2024-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. #include <cstdint>
  17. #include <cstdlib>
  18. #ifdef __UNIX_LIKE__
  19. #include <pthread.h>
  20. namespace ZeroTier {
  21. class Mutex
  22. {
  23. public:
  24. ZT_ALWAYS_INLINE Mutex() { pthread_mutex_init(&_mh,0); }
  25. ZT_ALWAYS_INLINE ~Mutex() { pthread_mutex_destroy(&_mh); }
  26. ZT_ALWAYS_INLINE void lock() const { pthread_mutex_lock(&((const_cast <Mutex *> (this))->_mh)); }
  27. ZT_ALWAYS_INLINE void unlock() const { pthread_mutex_unlock(&((const_cast <Mutex *> (this))->_mh)); }
  28. class Lock
  29. {
  30. public:
  31. ZT_ALWAYS_INLINE Lock(Mutex &m) : _m(&m) { m.lock(); }
  32. ZT_ALWAYS_INLINE Lock(const Mutex &m) : _m(const_cast<Mutex *>(&m)) { _m->lock(); }
  33. ZT_ALWAYS_INLINE ~Lock() { _m->unlock(); }
  34. private:
  35. Mutex *const _m;
  36. };
  37. private:
  38. ZT_ALWAYS_INLINE Mutex(const Mutex &) {}
  39. ZT_ALWAYS_INLINE const Mutex &operator=(const Mutex &) { return *this; }
  40. pthread_mutex_t _mh;
  41. };
  42. class RWMutex
  43. {
  44. public:
  45. ZT_ALWAYS_INLINE RWMutex() { pthread_rwlock_init(&_mh,0); }
  46. ZT_ALWAYS_INLINE ~RWMutex() { pthread_rwlock_destroy(&_mh); }
  47. ZT_ALWAYS_INLINE void lock() const { pthread_rwlock_wrlock(&((const_cast <RWMutex *> (this))->_mh)); }
  48. ZT_ALWAYS_INLINE void rlock() const { pthread_rwlock_rdlock(&((const_cast <RWMutex *> (this))->_mh)); }
  49. ZT_ALWAYS_INLINE void unlock() const { pthread_rwlock_unlock(&((const_cast <RWMutex *> (this))->_mh)); }
  50. ZT_ALWAYS_INLINE void runlock() const { pthread_rwlock_unlock(&((const_cast <RWMutex *> (this))->_mh)); }
  51. class RLock
  52. {
  53. public:
  54. ZT_ALWAYS_INLINE RLock(RWMutex &m) : _m(&m) { m.rlock(); }
  55. ZT_ALWAYS_INLINE RLock(const RWMutex &m) : _m(const_cast<RWMutex *>(&m)) { _m->rlock(); }
  56. ZT_ALWAYS_INLINE ~RLock() { _m->runlock(); }
  57. private:
  58. RWMutex *const _m;
  59. };
  60. class Lock
  61. {
  62. public:
  63. ZT_ALWAYS_INLINE Lock(RWMutex &m) : _m(&m) { m.lock(); }
  64. ZT_ALWAYS_INLINE Lock(const RWMutex &m) : _m(const_cast<RWMutex *>(&m)) { _m->lock(); }
  65. ZT_ALWAYS_INLINE ~Lock() { _m->unlock(); }
  66. private:
  67. RWMutex *const _m;
  68. };
  69. private:
  70. ZT_ALWAYS_INLINE RWMutex(const RWMutex &) {}
  71. ZT_ALWAYS_INLINE const RWMutex &operator=(const RWMutex &) { return *this; }
  72. pthread_rwlock_t _mh;
  73. };
  74. } // namespace ZeroTier
  75. #endif
  76. #ifdef __WINDOWS__
  77. #include <Windows.h>
  78. namespace ZeroTier {
  79. class Mutex
  80. {
  81. public:
  82. ZT_ALWAYS_INLINE Mutex() { InitializeCriticalSection(&_cs); }
  83. ZT_ALWAYS_INLINE ~Mutex() { DeleteCriticalSection(&_cs); }
  84. ZT_ALWAYS_INLINE void lock() { EnterCriticalSection(&_cs); }
  85. ZT_ALWAYS_INLINE void unlock() { LeaveCriticalSection(&_cs); }
  86. ZT_ALWAYS_INLINE void lock() const { (const_cast <Mutex *> (this))->lock(); }
  87. ZT_ALWAYS_INLINE void unlock() const { (const_cast <Mutex *> (this))->unlock(); }
  88. class Lock
  89. {
  90. public:
  91. ZT_ALWAYS_INLINE Lock(Mutex &m) : _m(&m) { m.lock(); }
  92. ZT_ALWAYS_INLINE Lock(const Mutex &m) : _m(const_cast<Mutex *>(&m)) { _m->lock(); }
  93. ZT_ALWAYS_INLINE ~Lock() { _m->unlock(); }
  94. private:
  95. Mutex *const _m;
  96. };
  97. private:
  98. ZT_ALWAYS_INLINE Mutex(const Mutex &) {}
  99. ZT_ALWAYS_INLINE const Mutex &operator=(const Mutex &) { return *this; }
  100. CRITICAL_SECTION _cs;
  101. };
  102. } // namespace ZeroTier
  103. #endif // _WIN32
  104. #endif