shared_mutex 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 <__config>
  102. #ifdef _LIBCPP_HAS_NO_THREADS
  103. # error "<shared_mutex> is not supported since libc++ has been configured without support for threads."
  104. #endif
  105. #include <__chrono/duration.h>
  106. #include <__chrono/steady_clock.h>
  107. #include <__chrono/time_point.h>
  108. #include <__condition_variable/condition_variable.h>
  109. #include <__memory/addressof.h>
  110. #include <__mutex/mutex.h>
  111. #include <__mutex/tag_types.h>
  112. #include <__mutex/unique_lock.h>
  113. #include <__system_error/system_error.h>
  114. #include <__utility/swap.h>
  115. #include <cerrno>
  116. #include <version>
  117. _LIBCPP_PUSH_MACROS
  118. #include <__undef_macros>
  119. #if _LIBCPP_STD_VER >= 14
  120. # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  121. # pragma GCC system_header
  122. # endif
  123. _LIBCPP_BEGIN_NAMESPACE_STD
  124. struct _LIBCPP_EXPORTED_FROM_ABI __shared_mutex_base {
  125. mutex __mut_;
  126. condition_variable __gate1_;
  127. condition_variable __gate2_;
  128. unsigned __state_;
  129. static const unsigned __write_entered_ = 1U << (sizeof(unsigned) * __CHAR_BIT__ - 1);
  130. static const unsigned __n_readers_ = ~__write_entered_;
  131. __shared_mutex_base();
  132. _LIBCPP_HIDE_FROM_ABI ~__shared_mutex_base() = default;
  133. __shared_mutex_base(const __shared_mutex_base&) = delete;
  134. __shared_mutex_base& operator=(const __shared_mutex_base&) = delete;
  135. // Exclusive ownership
  136. void lock(); // blocking
  137. bool try_lock();
  138. void unlock();
  139. // Shared ownership
  140. void lock_shared(); // blocking
  141. bool try_lock_shared();
  142. void unlock_shared();
  143. // typedef implementation-defined native_handle_type; // See 30.2.3
  144. // native_handle_type native_handle(); // See 30.2.3
  145. };
  146. # if _LIBCPP_STD_VER >= 17
  147. class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_THREAD_SAFETY_ANNOTATION(__capability__("shared_mutex")) shared_mutex {
  148. __shared_mutex_base __base_;
  149. public:
  150. _LIBCPP_HIDE_FROM_ABI shared_mutex() : __base_() {}
  151. _LIBCPP_HIDE_FROM_ABI ~shared_mutex() = default;
  152. shared_mutex(const shared_mutex&) = delete;
  153. shared_mutex& operator=(const shared_mutex&) = delete;
  154. // Exclusive ownership
  155. _LIBCPP_HIDE_FROM_ABI void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_capability__()) {
  156. return __base_.lock();
  157. }
  158. _LIBCPP_HIDE_FROM_ABI bool try_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true)) {
  159. return __base_.try_lock();
  160. }
  161. _LIBCPP_HIDE_FROM_ABI void unlock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_capability__()) {
  162. return __base_.unlock();
  163. }
  164. // Shared ownership
  165. _LIBCPP_HIDE_FROM_ABI void lock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_shared_capability__()) {
  166. return __base_.lock_shared();
  167. }
  168. _LIBCPP_HIDE_FROM_ABI bool try_lock_shared()
  169. _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true)) {
  170. return __base_.try_lock_shared();
  171. }
  172. _LIBCPP_HIDE_FROM_ABI void unlock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_shared_capability__()) {
  173. return __base_.unlock_shared();
  174. }
  175. // typedef __shared_mutex_base::native_handle_type native_handle_type;
  176. // _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __base::unlock_shared(); }
  177. };
  178. # endif
  179. class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_THREAD_SAFETY_ANNOTATION(__capability__("shared_timed_mutex"))
  180. shared_timed_mutex {
  181. __shared_mutex_base __base_;
  182. public:
  183. shared_timed_mutex();
  184. _LIBCPP_HIDE_FROM_ABI ~shared_timed_mutex() = default;
  185. shared_timed_mutex(const shared_timed_mutex&) = delete;
  186. shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
  187. // Exclusive ownership
  188. void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_capability__());
  189. bool try_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true));
  190. template <class _Rep, class _Period>
  191. _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time)
  192. _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true)) {
  193. return try_lock_until(chrono::steady_clock::now() + __rel_time);
  194. }
  195. template <class _Clock, class _Duration>
  196. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
  197. try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
  198. _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true));
  199. void unlock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_capability__());
  200. // Shared ownership
  201. void lock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_shared_capability__());
  202. bool try_lock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true));
  203. template <class _Rep, class _Period>
  204. _LIBCPP_HIDE_FROM_ABI bool try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rel_time)
  205. _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true)) {
  206. return try_lock_shared_until(chrono::steady_clock::now() + __rel_time);
  207. }
  208. template <class _Clock, class _Duration>
  209. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
  210. try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
  211. _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true));
  212. void unlock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_shared_capability__());
  213. };
  214. template <class _Clock, class _Duration>
  215. bool shared_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time) {
  216. unique_lock<mutex> __lk(__base_.__mut_);
  217. if (__base_.__state_ & __base_.__write_entered_) {
  218. while (true) {
  219. cv_status __status = __base_.__gate1_.wait_until(__lk, __abs_time);
  220. if ((__base_.__state_ & __base_.__write_entered_) == 0)
  221. break;
  222. if (__status == cv_status::timeout)
  223. return false;
  224. }
  225. }
  226. __base_.__state_ |= __base_.__write_entered_;
  227. if (__base_.__state_ & __base_.__n_readers_) {
  228. while (true) {
  229. cv_status __status = __base_.__gate2_.wait_until(__lk, __abs_time);
  230. if ((__base_.__state_ & __base_.__n_readers_) == 0)
  231. break;
  232. if (__status == cv_status::timeout) {
  233. __base_.__state_ &= ~__base_.__write_entered_;
  234. __base_.__gate1_.notify_all();
  235. return false;
  236. }
  237. }
  238. }
  239. return true;
  240. }
  241. template <class _Clock, class _Duration>
  242. bool shared_timed_mutex::try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time) {
  243. unique_lock<mutex> __lk(__base_.__mut_);
  244. if ((__base_.__state_ & __base_.__write_entered_) ||
  245. (__base_.__state_ & __base_.__n_readers_) == __base_.__n_readers_) {
  246. while (true) {
  247. cv_status __status = __base_.__gate1_.wait_until(__lk, __abs_time);
  248. if ((__base_.__state_ & __base_.__write_entered_) == 0 &&
  249. (__base_.__state_ & __base_.__n_readers_) < __base_.__n_readers_)
  250. break;
  251. if (__status == cv_status::timeout)
  252. return false;
  253. }
  254. }
  255. unsigned __num_readers = (__base_.__state_ & __base_.__n_readers_) + 1;
  256. __base_.__state_ &= ~__base_.__n_readers_;
  257. __base_.__state_ |= __num_readers;
  258. return true;
  259. }
  260. template <class _Mutex>
  261. class shared_lock {
  262. public:
  263. typedef _Mutex mutex_type;
  264. private:
  265. mutex_type* __m_;
  266. bool __owns_;
  267. public:
  268. _LIBCPP_HIDE_FROM_ABI shared_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
  269. _LIBCPP_HIDE_FROM_ABI explicit shared_lock(mutex_type& __m) : __m_(std::addressof(__m)), __owns_(true) {
  270. __m_->lock_shared();
  271. }
  272. _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
  273. : __m_(std::addressof(__m)),
  274. __owns_(false) {}
  275. _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, try_to_lock_t)
  276. : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared()) {}
  277. _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, adopt_lock_t) : __m_(std::addressof(__m)), __owns_(true) {}
  278. template <class _Clock, class _Duration>
  279. _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __abs_time)
  280. : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared_until(__abs_time)) {}
  281. template <class _Rep, class _Period>
  282. _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __rel_time)
  283. : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared_for(__rel_time)) {}
  284. _LIBCPP_HIDE_FROM_ABI ~shared_lock() {
  285. if (__owns_)
  286. __m_->unlock_shared();
  287. }
  288. shared_lock(shared_lock const&) = delete;
  289. shared_lock& operator=(shared_lock const&) = delete;
  290. _LIBCPP_HIDE_FROM_ABI shared_lock(shared_lock&& __u) _NOEXCEPT : __m_(__u.__m_), __owns_(__u.__owns_) {
  291. __u.__m_ = nullptr;
  292. __u.__owns_ = false;
  293. }
  294. _LIBCPP_HIDE_FROM_ABI shared_lock& operator=(shared_lock&& __u) _NOEXCEPT {
  295. if (__owns_)
  296. __m_->unlock_shared();
  297. __m_ = nullptr;
  298. __owns_ = false;
  299. __m_ = __u.__m_;
  300. __owns_ = __u.__owns_;
  301. __u.__m_ = nullptr;
  302. __u.__owns_ = false;
  303. return *this;
  304. }
  305. _LIBCPP_HIDE_FROM_ABI void lock();
  306. _LIBCPP_HIDE_FROM_ABI bool try_lock();
  307. template <class _Rep, class _Period>
  308. _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time);
  309. template <class _Clock, class _Duration>
  310. _LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time);
  311. _LIBCPP_HIDE_FROM_ABI void unlock();
  312. // Setters
  313. _LIBCPP_HIDE_FROM_ABI void swap(shared_lock& __u) _NOEXCEPT {
  314. std::swap(__m_, __u.__m_);
  315. std::swap(__owns_, __u.__owns_);
  316. }
  317. _LIBCPP_HIDE_FROM_ABI mutex_type* release() _NOEXCEPT {
  318. mutex_type* __m = __m_;
  319. __m_ = nullptr;
  320. __owns_ = false;
  321. return __m;
  322. }
  323. // Getters
  324. _LIBCPP_HIDE_FROM_ABI bool owns_lock() const _NOEXCEPT { return __owns_; }
  325. _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __owns_; }
  326. _LIBCPP_HIDE_FROM_ABI mutex_type* mutex() const _NOEXCEPT { return __m_; }
  327. };
  328. _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(shared_lock);
  329. template <class _Mutex>
  330. void shared_lock<_Mutex>::lock() {
  331. if (__m_ == nullptr)
  332. __throw_system_error(EPERM, "shared_lock::lock: references null mutex");
  333. if (__owns_)
  334. __throw_system_error(EDEADLK, "shared_lock::lock: already locked");
  335. __m_->lock_shared();
  336. __owns_ = true;
  337. }
  338. template <class _Mutex>
  339. bool shared_lock<_Mutex>::try_lock() {
  340. if (__m_ == nullptr)
  341. __throw_system_error(EPERM, "shared_lock::try_lock: references null mutex");
  342. if (__owns_)
  343. __throw_system_error(EDEADLK, "shared_lock::try_lock: already locked");
  344. __owns_ = __m_->try_lock_shared();
  345. return __owns_;
  346. }
  347. template <class _Mutex>
  348. template <class _Rep, class _Period>
  349. bool shared_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
  350. if (__m_ == nullptr)
  351. __throw_system_error(EPERM, "shared_lock::try_lock_for: references null mutex");
  352. if (__owns_)
  353. __throw_system_error(EDEADLK, "shared_lock::try_lock_for: already locked");
  354. __owns_ = __m_->try_lock_shared_for(__d);
  355. return __owns_;
  356. }
  357. template <class _Mutex>
  358. template <class _Clock, class _Duration>
  359. bool shared_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
  360. if (__m_ == nullptr)
  361. __throw_system_error(EPERM, "shared_lock::try_lock_until: references null mutex");
  362. if (__owns_)
  363. __throw_system_error(EDEADLK, "shared_lock::try_lock_until: already locked");
  364. __owns_ = __m_->try_lock_shared_until(__t);
  365. return __owns_;
  366. }
  367. template <class _Mutex>
  368. void shared_lock<_Mutex>::unlock() {
  369. if (!__owns_)
  370. __throw_system_error(EPERM, "shared_lock::unlock: not locked");
  371. __m_->unlock_shared();
  372. __owns_ = false;
  373. }
  374. template <class _Mutex>
  375. inline _LIBCPP_HIDE_FROM_ABI void swap(shared_lock<_Mutex>& __x, shared_lock<_Mutex>& __y) _NOEXCEPT {
  376. __x.swap(__y);
  377. }
  378. _LIBCPP_END_NAMESPACE_STD
  379. #endif // _LIBCPP_STD_VER >= 14
  380. _LIBCPP_POP_MACROS
  381. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  382. # include <system_error>
  383. #endif
  384. #endif // _LIBCPP_SHARED_MUTEX