cm_thread.hxx 1007 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef CM_THREAD_HXX
  4. #define CM_THREAD_HXX
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include "cm_uv.h"
  7. namespace cm {
  8. class shared_mutex
  9. {
  10. uv_rwlock_t _M_;
  11. public:
  12. shared_mutex() { uv_rwlock_init(&_M_); }
  13. ~shared_mutex() { uv_rwlock_destroy(&_M_); }
  14. shared_mutex(shared_mutex const&) = delete;
  15. shared_mutex& operator=(shared_mutex const&) = delete;
  16. void lock() { uv_rwlock_wrlock(&_M_); }
  17. void unlock() { uv_rwlock_wrunlock(&_M_); }
  18. void lock_shared() { uv_rwlock_rdlock(&_M_); }
  19. void unlock_shared() { uv_rwlock_rdunlock(&_M_); }
  20. };
  21. template <typename T>
  22. class shared_lock
  23. {
  24. T& _mutex;
  25. public:
  26. shared_lock(T& m)
  27. : _mutex(m)
  28. {
  29. _mutex.lock_shared();
  30. }
  31. ~shared_lock() { _mutex.unlock_shared(); }
  32. shared_lock(shared_lock const&) = delete;
  33. shared_lock& operator=(shared_lock const&) = delete;
  34. };
  35. }
  36. #endif