shared_mutex 15 KB

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