Mutex.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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: 2023-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. ZT_ALWAYS_INLINE Mutex() { pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0); }
  26. ZT_ALWAYS_INLINE ~Mutex() { pthread_mutex_destroy(&_mh); }
  27. ZT_ALWAYS_INLINE void lock() const { pthread_mutex_lock(&((const_cast <Mutex *> (this))->_mh)); }
  28. ZT_ALWAYS_INLINE void unlock() const { pthread_mutex_unlock(&((const_cast <Mutex *> (this))->_mh)); }
  29. class Lock
  30. {
  31. public:
  32. ZT_ALWAYS_INLINE Lock(Mutex &m) : _m(&m) { m.lock(); }
  33. ZT_ALWAYS_INLINE Lock(const Mutex &m) : _m(const_cast<Mutex *>(&m)) { _m->lock(); }
  34. ZT_ALWAYS_INLINE ~Lock() { _m->unlock(); }
  35. private:
  36. Mutex *const _m;
  37. };
  38. private:
  39. inline Mutex(const Mutex &) {}
  40. const Mutex &operator=(const Mutex &) { return *this; }
  41. pthread_mutex_t _mh;
  42. };
  43. } // namespace ZeroTier
  44. #endif
  45. #ifdef __WINDOWS__
  46. #include <stdlib.h>
  47. #include <Windows.h>
  48. namespace ZeroTier {
  49. // Windows critical section based lock
  50. class Mutex
  51. {
  52. public:
  53. ZT_ALWAYS_INLINE Mutex() { InitializeCriticalSection(&_cs); }
  54. ZT_ALWAYS_INLINE ~Mutex() { DeleteCriticalSection(&_cs); }
  55. ZT_ALWAYS_INLINE void lock() { EnterCriticalSection(&_cs); }
  56. ZT_ALWAYS_INLINE void unlock() { LeaveCriticalSection(&_cs); }
  57. ZT_ALWAYS_INLINE void lock() const { (const_cast <Mutex *> (this))->lock(); }
  58. ZT_ALWAYS_INLINE void unlock() const { (const_cast <Mutex *> (this))->unlock(); }
  59. class Lock
  60. {
  61. public:
  62. ZT_ALWAYS_INLINE Lock(Mutex &m) : _m(&m) { m.lock(); }
  63. ZT_ALWAYS_INLINE Lock(const Mutex &m) : _m(const_cast<Mutex *>(&m)) { _m->lock(); }
  64. ZT_ALWAYS_INLINE ~Lock() { _m->unlock(); }
  65. private:
  66. Mutex *const _m;
  67. };
  68. private:
  69. inline Mutex(const Mutex &) {}
  70. const Mutex &operator=(const Mutex &) { return *this; }
  71. CRITICAL_SECTION _cs;
  72. };
  73. } // namespace ZeroTier
  74. #endif // _WIN32
  75. #endif