shared_mutex 15 KB

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