shared_mutex 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_SHARED_MUTEX
  10. #define _LIBCPP_SHARED_MUTEX
  11. /*
  12. shared_mutex synopsis
  13. // C++1y
  14. namespace std
  15. {
  16. class shared_mutex // C++17
  17. {
  18. public:
  19. shared_mutex();
  20. ~shared_mutex();
  21. shared_mutex(const shared_mutex&) = delete;
  22. shared_mutex& operator=(const shared_mutex&) = delete;
  23. // Exclusive ownership
  24. void lock(); // blocking
  25. bool try_lock();
  26. void unlock();
  27. // Shared ownership
  28. void lock_shared(); // blocking
  29. bool try_lock_shared();
  30. void unlock_shared();
  31. typedef implementation-defined native_handle_type; // See 30.2.3
  32. native_handle_type native_handle(); // See 30.2.3
  33. };
  34. class shared_timed_mutex
  35. {
  36. public:
  37. shared_timed_mutex();
  38. ~shared_timed_mutex();
  39. shared_timed_mutex(const shared_timed_mutex&) = delete;
  40. shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
  41. // Exclusive ownership
  42. void lock(); // blocking
  43. bool try_lock();
  44. template <class Rep, class Period>
  45. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
  46. template <class Clock, class Duration>
  47. bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
  48. void unlock();
  49. // Shared ownership
  50. void lock_shared(); // blocking
  51. bool try_lock_shared();
  52. template <class Rep, class Period>
  53. bool
  54. try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time);
  55. template <class Clock, class Duration>
  56. bool
  57. try_lock_shared_until(const chrono::time_point<Clock, Duration>& abs_time);
  58. void unlock_shared();
  59. };
  60. template <class Mutex>
  61. class shared_lock
  62. {
  63. public:
  64. typedef Mutex mutex_type;
  65. // Shared locking
  66. shared_lock() noexcept;
  67. explicit shared_lock(mutex_type& m); // blocking
  68. shared_lock(mutex_type& m, defer_lock_t) noexcept;
  69. shared_lock(mutex_type& m, try_to_lock_t);
  70. shared_lock(mutex_type& m, adopt_lock_t);
  71. template <class Clock, class Duration>
  72. shared_lock(mutex_type& m,
  73. const chrono::time_point<Clock, Duration>& abs_time);
  74. template <class Rep, class Period>
  75. shared_lock(mutex_type& m,
  76. const chrono::duration<Rep, Period>& rel_time);
  77. ~shared_lock();
  78. shared_lock(shared_lock const&) = delete;
  79. shared_lock& operator=(shared_lock const&) = delete;
  80. shared_lock(shared_lock&& u) noexcept;
  81. shared_lock& operator=(shared_lock&& u) noexcept;
  82. void lock(); // blocking
  83. bool try_lock();
  84. template <class Rep, class Period>
  85. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
  86. template <class Clock, class Duration>
  87. bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
  88. void unlock();
  89. // Setters
  90. void swap(shared_lock& u) noexcept;
  91. mutex_type* release() noexcept;
  92. // Getters
  93. bool owns_lock() const noexcept;
  94. explicit operator bool () const noexcept;
  95. mutex_type* mutex() const noexcept;
  96. };
  97. template <class Mutex>
  98. void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept;
  99. } // std
  100. */
  101. #include <__assert> // all public C++ headers provide the assertion handler
  102. #include <__availability>
  103. #include <__chrono/duration.h>
  104. #include <__chrono/steady_clock.h>
  105. #include <__chrono/time_point.h>
  106. #include <__condition_variable/condition_variable.h>
  107. #include <__config>
  108. #include <__memory/addressof.h>
  109. #include <__mutex/mutex.h>
  110. #include <__mutex/tag_types.h>
  111. #include <__mutex/unique_lock.h>
  112. #include <__system_error/system_error.h>
  113. #include <__utility/swap.h>
  114. #include <cerrno>
  115. #include <version>
  116. _LIBCPP_PUSH_MACROS
  117. #include <__undef_macros>
  118. #if _LIBCPP_STD_VER >= 14
  119. # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  120. # pragma GCC system_header
  121. # endif
  122. # ifdef _LIBCPP_HAS_NO_THREADS
  123. # error "<shared_mutex> is not supported since libc++ has been configured without support for threads."
  124. # endif
  125. _LIBCPP_BEGIN_NAMESPACE_STD
  126. struct _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_SHARED_MUTEX __shared_mutex_base {
  127. mutex __mut_;
  128. condition_variable __gate1_;
  129. condition_variable __gate2_;
  130. unsigned __state_;
  131. static const unsigned __write_entered_ = 1U << (sizeof(unsigned) * __CHAR_BIT__ - 1);
  132. static const unsigned __n_readers_ = ~__write_entered_;
  133. __shared_mutex_base();
  134. _LIBCPP_HIDE_FROM_ABI ~__shared_mutex_base() = default;
  135. __shared_mutex_base(const __shared_mutex_base&) = delete;
  136. __shared_mutex_base& operator=(const __shared_mutex_base&) = delete;
  137. // Exclusive ownership
  138. void lock(); // blocking
  139. bool try_lock();
  140. void unlock();
  141. // Shared ownership
  142. void lock_shared(); // blocking
  143. bool try_lock_shared();
  144. void unlock_shared();
  145. // typedef implementation-defined native_handle_type; // See 30.2.3
  146. // native_handle_type native_handle(); // See 30.2.3
  147. };
  148. # if _LIBCPP_STD_VER >= 17
  149. class _LIBCPP_EXPORTED_FROM_ABI
  150. _LIBCPP_AVAILABILITY_SHARED_MUTEX _LIBCPP_THREAD_SAFETY_ANNOTATION(__capability__("shared_mutex")) shared_mutex {
  151. __shared_mutex_base __base_;
  152. public:
  153. _LIBCPP_HIDE_FROM_ABI shared_mutex() : __base_() {}
  154. _LIBCPP_HIDE_FROM_ABI ~shared_mutex() = default;
  155. shared_mutex(const shared_mutex&) = delete;
  156. shared_mutex& operator=(const shared_mutex&) = delete;
  157. // Exclusive ownership
  158. _LIBCPP_HIDE_FROM_ABI void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_capability__()) {
  159. return __base_.lock();
  160. }
  161. _LIBCPP_HIDE_FROM_ABI bool try_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true)) {
  162. return __base_.try_lock();
  163. }
  164. _LIBCPP_HIDE_FROM_ABI void unlock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_capability__()) {
  165. return __base_.unlock();
  166. }
  167. // Shared ownership
  168. _LIBCPP_HIDE_FROM_ABI void lock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_shared_capability__()) {
  169. return __base_.lock_shared();
  170. }
  171. _LIBCPP_HIDE_FROM_ABI bool try_lock_shared()
  172. _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true)) {
  173. return __base_.try_lock_shared();
  174. }
  175. _LIBCPP_HIDE_FROM_ABI void unlock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_shared_capability__()) {
  176. return __base_.unlock_shared();
  177. }
  178. // typedef __shared_mutex_base::native_handle_type native_handle_type;
  179. // _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __base::unlock_shared(); }
  180. };
  181. # endif
  182. class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_SHARED_MUTEX _LIBCPP_THREAD_SAFETY_ANNOTATION(
  183. __capability__("shared_timed_mutex")) shared_timed_mutex {
  184. __shared_mutex_base __base_;
  185. public:
  186. shared_timed_mutex();
  187. _LIBCPP_HIDE_FROM_ABI ~shared_timed_mutex() = default;
  188. shared_timed_mutex(const shared_timed_mutex&) = delete;
  189. shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
  190. // Exclusive ownership
  191. void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_capability__());
  192. bool try_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true));
  193. template <class _Rep, class _Period>
  194. _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time)
  195. _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true)) {
  196. return try_lock_until(chrono::steady_clock::now() + __rel_time);
  197. }
  198. template <class _Clock, class _Duration>
  199. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
  200. try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
  201. _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true));
  202. void unlock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_capability__());
  203. // Shared ownership
  204. void lock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_shared_capability__());
  205. bool try_lock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true));
  206. template <class _Rep, class _Period>
  207. _LIBCPP_HIDE_FROM_ABI bool try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rel_time)
  208. _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true)) {
  209. return try_lock_shared_until(chrono::steady_clock::now() + __rel_time);
  210. }
  211. template <class _Clock, class _Duration>
  212. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
  213. try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
  214. _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true));
  215. void unlock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_shared_capability__());
  216. };
  217. template <class _Clock, class _Duration>
  218. bool shared_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time) {
  219. unique_lock<mutex> __lk(__base_.__mut_);
  220. if (__base_.__state_ & __base_.__write_entered_) {
  221. while (true) {
  222. cv_status __status = __base_.__gate1_.wait_until(__lk, __abs_time);
  223. if ((__base_.__state_ & __base_.__write_entered_) == 0)
  224. break;
  225. if (__status == cv_status::timeout)
  226. return false;
  227. }
  228. }
  229. __base_.__state_ |= __base_.__write_entered_;
  230. if (__base_.__state_ & __base_.__n_readers_) {
  231. while (true) {
  232. cv_status __status = __base_.__gate2_.wait_until(__lk, __abs_time);
  233. if ((__base_.__state_ & __base_.__n_readers_) == 0)
  234. break;
  235. if (__status == cv_status::timeout) {
  236. __base_.__state_ &= ~__base_.__write_entered_;
  237. __base_.__gate1_.notify_all();
  238. return false;
  239. }
  240. }
  241. }
  242. return true;
  243. }
  244. template <class _Clock, class _Duration>
  245. bool shared_timed_mutex::try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time) {
  246. unique_lock<mutex> __lk(__base_.__mut_);
  247. if ((__base_.__state_ & __base_.__write_entered_) ||
  248. (__base_.__state_ & __base_.__n_readers_) == __base_.__n_readers_) {
  249. while (true) {
  250. cv_status __status = __base_.__gate1_.wait_until(__lk, __abs_time);
  251. if ((__base_.__state_ & __base_.__write_entered_) == 0 &&
  252. (__base_.__state_ & __base_.__n_readers_) < __base_.__n_readers_)
  253. break;
  254. if (__status == cv_status::timeout)
  255. return false;
  256. }
  257. }
  258. unsigned __num_readers = (__base_.__state_ & __base_.__n_readers_) + 1;
  259. __base_.__state_ &= ~__base_.__n_readers_;
  260. __base_.__state_ |= __num_readers;
  261. return true;
  262. }
  263. template <class _Mutex>
  264. class shared_lock {
  265. public:
  266. typedef _Mutex mutex_type;
  267. private:
  268. mutex_type* __m_;
  269. bool __owns_;
  270. public:
  271. _LIBCPP_HIDE_FROM_ABI shared_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
  272. _LIBCPP_HIDE_FROM_ABI explicit shared_lock(mutex_type& __m) : __m_(_VSTD::addressof(__m)), __owns_(true) {
  273. __m_->lock_shared();
  274. }
  275. _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
  276. : __m_(_VSTD::addressof(__m)),
  277. __owns_(false) {}
  278. _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, try_to_lock_t)
  279. : __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock_shared()) {}
  280. _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, adopt_lock_t) : __m_(_VSTD::addressof(__m)), __owns_(true) {}
  281. template <class _Clock, class _Duration>
  282. _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __abs_time)
  283. : __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock_shared_until(__abs_time)) {}
  284. template <class _Rep, class _Period>
  285. _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __rel_time)
  286. : __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock_shared_for(__rel_time)) {}
  287. _LIBCPP_HIDE_FROM_ABI ~shared_lock() {
  288. if (__owns_)
  289. __m_->unlock_shared();
  290. }
  291. shared_lock(shared_lock const&) = delete;
  292. shared_lock& operator=(shared_lock const&) = delete;
  293. _LIBCPP_HIDE_FROM_ABI shared_lock(shared_lock&& __u) _NOEXCEPT : __m_(__u.__m_), __owns_(__u.__owns_) {
  294. __u.__m_ = nullptr;
  295. __u.__owns_ = false;
  296. }
  297. _LIBCPP_HIDE_FROM_ABI shared_lock& operator=(shared_lock&& __u) _NOEXCEPT {
  298. if (__owns_)
  299. __m_->unlock_shared();
  300. __m_ = nullptr;
  301. __owns_ = false;
  302. __m_ = __u.__m_;
  303. __owns_ = __u.__owns_;
  304. __u.__m_ = nullptr;
  305. __u.__owns_ = false;
  306. return *this;
  307. }
  308. _LIBCPP_HIDE_FROM_ABI void lock();
  309. _LIBCPP_HIDE_FROM_ABI bool try_lock();
  310. template <class _Rep, class _Period>
  311. _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time);
  312. template <class _Clock, class _Duration>
  313. _LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time);
  314. _LIBCPP_HIDE_FROM_ABI void unlock();
  315. // Setters
  316. _LIBCPP_HIDE_FROM_ABI void swap(shared_lock& __u) _NOEXCEPT {
  317. _VSTD::swap(__m_, __u.__m_);
  318. _VSTD::swap(__owns_, __u.__owns_);
  319. }
  320. _LIBCPP_HIDE_FROM_ABI mutex_type* release() _NOEXCEPT {
  321. mutex_type* __m = __m_;
  322. __m_ = nullptr;
  323. __owns_ = false;
  324. return __m;
  325. }
  326. // Getters
  327. _LIBCPP_HIDE_FROM_ABI bool owns_lock() const _NOEXCEPT { return __owns_; }
  328. _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __owns_; }
  329. _LIBCPP_HIDE_FROM_ABI mutex_type* mutex() const _NOEXCEPT { return __m_; }
  330. };
  331. _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(shared_lock);
  332. template <class _Mutex>
  333. void shared_lock<_Mutex>::lock() {
  334. if (__m_ == nullptr)
  335. __throw_system_error(EPERM, "shared_lock::lock: references null mutex");
  336. if (__owns_)
  337. __throw_system_error(EDEADLK, "shared_lock::lock: already locked");
  338. __m_->lock_shared();
  339. __owns_ = true;
  340. }
  341. template <class _Mutex>
  342. bool shared_lock<_Mutex>::try_lock() {
  343. if (__m_ == nullptr)
  344. __throw_system_error(EPERM, "shared_lock::try_lock: references null mutex");
  345. if (__owns_)
  346. __throw_system_error(EDEADLK, "shared_lock::try_lock: already locked");
  347. __owns_ = __m_->try_lock_shared();
  348. return __owns_;
  349. }
  350. template <class _Mutex>
  351. template <class _Rep, class _Period>
  352. bool shared_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
  353. if (__m_ == nullptr)
  354. __throw_system_error(EPERM, "shared_lock::try_lock_for: references null mutex");
  355. if (__owns_)
  356. __throw_system_error(EDEADLK, "shared_lock::try_lock_for: already locked");
  357. __owns_ = __m_->try_lock_shared_for(__d);
  358. return __owns_;
  359. }
  360. template <class _Mutex>
  361. template <class _Clock, class _Duration>
  362. bool shared_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
  363. if (__m_ == nullptr)
  364. __throw_system_error(EPERM, "shared_lock::try_lock_until: references null mutex");
  365. if (__owns_)
  366. __throw_system_error(EDEADLK, "shared_lock::try_lock_until: already locked");
  367. __owns_ = __m_->try_lock_shared_until(__t);
  368. return __owns_;
  369. }
  370. template <class _Mutex>
  371. void shared_lock<_Mutex>::unlock() {
  372. if (!__owns_)
  373. __throw_system_error(EPERM, "shared_lock::unlock: not locked");
  374. __m_->unlock_shared();
  375. __owns_ = false;
  376. }
  377. template <class _Mutex>
  378. inline _LIBCPP_HIDE_FROM_ABI void swap(shared_lock<_Mutex>& __x, shared_lock<_Mutex>& __y) _NOEXCEPT {
  379. __x.swap(__y);
  380. }
  381. _LIBCPP_END_NAMESPACE_STD
  382. #endif // _LIBCPP_STD_VER >= 14
  383. _LIBCPP_POP_MACROS
  384. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  385. # include <system_error>
  386. #endif
  387. #endif // _LIBCPP_SHARED_MUTEX