shared_mutex 15 KB

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