mutex 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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_MUTEX
  10. #define _LIBCPP_MUTEX
  11. /*
  12. mutex synopsis
  13. namespace std
  14. {
  15. class mutex
  16. {
  17. public:
  18. constexpr mutex() noexcept;
  19. ~mutex();
  20. mutex(const mutex&) = delete;
  21. mutex& operator=(const mutex&) = delete;
  22. void lock();
  23. bool try_lock();
  24. void unlock();
  25. typedef pthread_mutex_t* native_handle_type;
  26. native_handle_type native_handle();
  27. };
  28. class recursive_mutex
  29. {
  30. public:
  31. recursive_mutex();
  32. ~recursive_mutex();
  33. recursive_mutex(const recursive_mutex&) = delete;
  34. recursive_mutex& operator=(const recursive_mutex&) = delete;
  35. void lock();
  36. bool try_lock() noexcept;
  37. void unlock();
  38. typedef pthread_mutex_t* native_handle_type;
  39. native_handle_type native_handle();
  40. };
  41. class timed_mutex
  42. {
  43. public:
  44. timed_mutex();
  45. ~timed_mutex();
  46. timed_mutex(const timed_mutex&) = delete;
  47. timed_mutex& operator=(const timed_mutex&) = delete;
  48. void lock();
  49. bool try_lock();
  50. template <class Rep, class Period>
  51. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
  52. template <class Clock, class Duration>
  53. bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
  54. void unlock();
  55. };
  56. class recursive_timed_mutex
  57. {
  58. public:
  59. recursive_timed_mutex();
  60. ~recursive_timed_mutex();
  61. recursive_timed_mutex(const recursive_timed_mutex&) = delete;
  62. recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
  63. void lock();
  64. bool try_lock() noexcept;
  65. template <class Rep, class Period>
  66. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
  67. template <class Clock, class Duration>
  68. bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
  69. void unlock();
  70. };
  71. struct defer_lock_t { explicit defer_lock_t() = default; };
  72. struct try_to_lock_t { explicit try_to_lock_t() = default; };
  73. struct adopt_lock_t { explicit adopt_lock_t() = default; };
  74. inline constexpr defer_lock_t defer_lock{};
  75. inline constexpr try_to_lock_t try_to_lock{};
  76. inline constexpr adopt_lock_t adopt_lock{};
  77. template <class Mutex>
  78. class lock_guard
  79. {
  80. public:
  81. typedef Mutex mutex_type;
  82. explicit lock_guard(mutex_type& m);
  83. lock_guard(mutex_type& m, adopt_lock_t);
  84. ~lock_guard();
  85. lock_guard(lock_guard const&) = delete;
  86. lock_guard& operator=(lock_guard const&) = delete;
  87. };
  88. template <class... MutexTypes>
  89. class scoped_lock // C++17
  90. {
  91. public:
  92. using mutex_type = Mutex; // Only if sizeof...(MutexTypes) == 1
  93. explicit scoped_lock(MutexTypes&... m);
  94. scoped_lock(adopt_lock_t, MutexTypes&... m);
  95. ~scoped_lock();
  96. scoped_lock(scoped_lock const&) = delete;
  97. scoped_lock& operator=(scoped_lock const&) = delete;
  98. private:
  99. tuple<MutexTypes&...> pm; // exposition only
  100. };
  101. template <class Mutex>
  102. class unique_lock
  103. {
  104. public:
  105. typedef Mutex mutex_type;
  106. unique_lock() noexcept;
  107. explicit unique_lock(mutex_type& m);
  108. unique_lock(mutex_type& m, defer_lock_t) noexcept;
  109. unique_lock(mutex_type& m, try_to_lock_t);
  110. unique_lock(mutex_type& m, adopt_lock_t);
  111. template <class Clock, class Duration>
  112. unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
  113. template <class Rep, class Period>
  114. unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
  115. ~unique_lock();
  116. unique_lock(unique_lock const&) = delete;
  117. unique_lock& operator=(unique_lock const&) = delete;
  118. unique_lock(unique_lock&& u) noexcept;
  119. unique_lock& operator=(unique_lock&& u) noexcept;
  120. void lock();
  121. bool try_lock();
  122. template <class Rep, class Period>
  123. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
  124. template <class Clock, class Duration>
  125. bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
  126. void unlock();
  127. void swap(unique_lock& u) noexcept;
  128. mutex_type* release() noexcept;
  129. bool owns_lock() const noexcept;
  130. explicit operator bool () const noexcept;
  131. mutex_type* mutex() const noexcept;
  132. };
  133. template <class Mutex>
  134. void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
  135. template <class L1, class L2, class... L3>
  136. int try_lock(L1&, L2&, L3&...);
  137. template <class L1, class L2, class... L3>
  138. void lock(L1&, L2&, L3&...);
  139. struct once_flag
  140. {
  141. constexpr once_flag() noexcept;
  142. once_flag(const once_flag&) = delete;
  143. once_flag& operator=(const once_flag&) = delete;
  144. };
  145. template<class Callable, class ...Args>
  146. void call_once(once_flag& flag, Callable&& func, Args&&... args);
  147. } // std
  148. */
  149. #include <__chrono/steady_clock.h>
  150. #include <__chrono/time_point.h>
  151. #include <__condition_variable/condition_variable.h>
  152. #include <__config>
  153. #include <__memory/shared_ptr.h>
  154. #include <__mutex/lock_guard.h>
  155. #include <__mutex/mutex.h>
  156. #include <__mutex/once_flag.h>
  157. #include <__mutex/tag_types.h>
  158. #include <__mutex/unique_lock.h>
  159. #include <__thread/id.h>
  160. #include <__thread/support.h>
  161. #include <__utility/forward.h>
  162. #include <cstddef>
  163. #include <limits>
  164. #ifndef _LIBCPP_CXX03_LANG
  165. # include <tuple>
  166. #endif
  167. #include <version>
  168. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  169. # pragma GCC system_header
  170. #endif
  171. _LIBCPP_PUSH_MACROS
  172. #include <__undef_macros>
  173. _LIBCPP_BEGIN_NAMESPACE_STD
  174. #ifndef _LIBCPP_HAS_NO_THREADS
  175. class _LIBCPP_EXPORTED_FROM_ABI recursive_mutex {
  176. __libcpp_recursive_mutex_t __m_;
  177. public:
  178. recursive_mutex();
  179. ~recursive_mutex();
  180. recursive_mutex(const recursive_mutex&) = delete;
  181. recursive_mutex& operator=(const recursive_mutex&) = delete;
  182. void lock();
  183. bool try_lock() _NOEXCEPT;
  184. void unlock() _NOEXCEPT;
  185. typedef __libcpp_recursive_mutex_t* native_handle_type;
  186. _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return &__m_; }
  187. };
  188. class _LIBCPP_EXPORTED_FROM_ABI timed_mutex {
  189. mutex __m_;
  190. condition_variable __cv_;
  191. bool __locked_;
  192. public:
  193. timed_mutex();
  194. ~timed_mutex();
  195. timed_mutex(const timed_mutex&) = delete;
  196. timed_mutex& operator=(const timed_mutex&) = delete;
  197. public:
  198. void lock();
  199. bool try_lock() _NOEXCEPT;
  200. template <class _Rep, class _Period>
  201. _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
  202. return try_lock_until(chrono::steady_clock::now() + __d);
  203. }
  204. template <class _Clock, class _Duration>
  205. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
  206. try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
  207. void unlock() _NOEXCEPT;
  208. };
  209. template <class _Clock, class _Duration>
  210. bool timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
  211. using namespace chrono;
  212. unique_lock<mutex> __lk(__m_);
  213. bool __no_timeout = _Clock::now() < __t;
  214. while (__no_timeout && __locked_)
  215. __no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
  216. if (!__locked_) {
  217. __locked_ = true;
  218. return true;
  219. }
  220. return false;
  221. }
  222. class _LIBCPP_EXPORTED_FROM_ABI recursive_timed_mutex {
  223. mutex __m_;
  224. condition_variable __cv_;
  225. size_t __count_;
  226. __thread_id __id_;
  227. public:
  228. recursive_timed_mutex();
  229. ~recursive_timed_mutex();
  230. recursive_timed_mutex(const recursive_timed_mutex&) = delete;
  231. recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
  232. void lock();
  233. bool try_lock() _NOEXCEPT;
  234. template <class _Rep, class _Period>
  235. _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
  236. return try_lock_until(chrono::steady_clock::now() + __d);
  237. }
  238. template <class _Clock, class _Duration>
  239. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
  240. try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
  241. void unlock() _NOEXCEPT;
  242. };
  243. template <class _Clock, class _Duration>
  244. bool recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
  245. using namespace chrono;
  246. __thread_id __id = this_thread::get_id();
  247. unique_lock<mutex> __lk(__m_);
  248. if (__id == __id_) {
  249. if (__count_ == numeric_limits<size_t>::max())
  250. return false;
  251. ++__count_;
  252. return true;
  253. }
  254. bool __no_timeout = _Clock::now() < __t;
  255. while (__no_timeout && __count_ != 0)
  256. __no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
  257. if (__count_ == 0) {
  258. __count_ = 1;
  259. __id_ = __id;
  260. return true;
  261. }
  262. return false;
  263. }
  264. template <class _L0, class _L1>
  265. _LIBCPP_HIDE_FROM_ABI int try_lock(_L0& __l0, _L1& __l1) {
  266. unique_lock<_L0> __u0(__l0, try_to_lock_t());
  267. if (__u0.owns_lock()) {
  268. if (__l1.try_lock()) {
  269. __u0.release();
  270. return -1;
  271. } else
  272. return 1;
  273. }
  274. return 0;
  275. }
  276. # ifndef _LIBCPP_CXX03_LANG
  277. template <class _L0, class _L1, class _L2, class... _L3>
  278. _LIBCPP_HIDE_FROM_ABI int try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
  279. int __r = 0;
  280. unique_lock<_L0> __u0(__l0, try_to_lock);
  281. if (__u0.owns_lock()) {
  282. __r = std::try_lock(__l1, __l2, __l3...);
  283. if (__r == -1)
  284. __u0.release();
  285. else
  286. ++__r;
  287. }
  288. return __r;
  289. }
  290. # endif // _LIBCPP_CXX03_LANG
  291. template <class _L0, class _L1>
  292. _LIBCPP_HIDE_FROM_ABI void lock(_L0& __l0, _L1& __l1) {
  293. while (true) {
  294. {
  295. unique_lock<_L0> __u0(__l0);
  296. if (__l1.try_lock()) {
  297. __u0.release();
  298. break;
  299. }
  300. }
  301. __libcpp_thread_yield();
  302. {
  303. unique_lock<_L1> __u1(__l1);
  304. if (__l0.try_lock()) {
  305. __u1.release();
  306. break;
  307. }
  308. }
  309. __libcpp_thread_yield();
  310. }
  311. }
  312. # ifndef _LIBCPP_CXX03_LANG
  313. template <class _L0, class _L1, class _L2, class... _L3>
  314. void __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
  315. while (true) {
  316. switch (__i) {
  317. case 0: {
  318. unique_lock<_L0> __u0(__l0);
  319. __i = std::try_lock(__l1, __l2, __l3...);
  320. if (__i == -1) {
  321. __u0.release();
  322. return;
  323. }
  324. }
  325. ++__i;
  326. __libcpp_thread_yield();
  327. break;
  328. case 1: {
  329. unique_lock<_L1> __u1(__l1);
  330. __i = std::try_lock(__l2, __l3..., __l0);
  331. if (__i == -1) {
  332. __u1.release();
  333. return;
  334. }
  335. }
  336. if (__i == sizeof...(_L3) + 1)
  337. __i = 0;
  338. else
  339. __i += 2;
  340. __libcpp_thread_yield();
  341. break;
  342. default:
  343. std::__lock_first(__i - 2, __l2, __l3..., __l0, __l1);
  344. return;
  345. }
  346. }
  347. }
  348. template <class _L0, class _L1, class _L2, class... _L3>
  349. inline _LIBCPP_HIDE_FROM_ABI void lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
  350. std::__lock_first(0, __l0, __l1, __l2, __l3...);
  351. }
  352. template <class _L0>
  353. inline _LIBCPP_HIDE_FROM_ABI void __unlock(_L0& __l0) {
  354. __l0.unlock();
  355. }
  356. template <class _L0, class _L1>
  357. inline _LIBCPP_HIDE_FROM_ABI void __unlock(_L0& __l0, _L1& __l1) {
  358. __l0.unlock();
  359. __l1.unlock();
  360. }
  361. template <class _L0, class _L1, class _L2, class... _L3>
  362. inline _LIBCPP_HIDE_FROM_ABI void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
  363. __l0.unlock();
  364. __l1.unlock();
  365. std::__unlock(__l2, __l3...);
  366. }
  367. # endif // _LIBCPP_CXX03_LANG
  368. # if _LIBCPP_STD_VER >= 17
  369. template <class... _Mutexes>
  370. class _LIBCPP_TEMPLATE_VIS scoped_lock;
  371. template <>
  372. class _LIBCPP_TEMPLATE_VIS scoped_lock<> {
  373. public:
  374. explicit scoped_lock() {}
  375. ~scoped_lock() = default;
  376. _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(adopt_lock_t) {}
  377. scoped_lock(scoped_lock const&) = delete;
  378. scoped_lock& operator=(scoped_lock const&) = delete;
  379. };
  380. template <class _Mutex>
  381. class _LIBCPP_TEMPLATE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) scoped_lock<_Mutex> {
  382. public:
  383. typedef _Mutex mutex_type;
  384. private:
  385. mutex_type& __m_;
  386. public:
  387. explicit scoped_lock(mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m)) : __m_(__m) {
  388. __m_.lock();
  389. }
  390. ~scoped_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) { __m_.unlock(); }
  391. _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(adopt_lock_t, mutex_type& __m)
  392. _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
  393. : __m_(__m) {}
  394. scoped_lock(scoped_lock const&) = delete;
  395. scoped_lock& operator=(scoped_lock const&) = delete;
  396. };
  397. template <class... _MArgs>
  398. class _LIBCPP_TEMPLATE_VIS scoped_lock {
  399. static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required");
  400. typedef tuple<_MArgs&...> _MutexTuple;
  401. public:
  402. _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(_MArgs&... __margs) : __t_(__margs...) { std::lock(__margs...); }
  403. _LIBCPP_HIDE_FROM_ABI scoped_lock(adopt_lock_t, _MArgs&... __margs) : __t_(__margs...) {}
  404. _LIBCPP_HIDE_FROM_ABI ~scoped_lock() {
  405. typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
  406. __unlock_unpack(_Indices{}, __t_);
  407. }
  408. scoped_lock(scoped_lock const&) = delete;
  409. scoped_lock& operator=(scoped_lock const&) = delete;
  410. private:
  411. template <size_t... _Indx>
  412. _LIBCPP_HIDE_FROM_ABI static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
  413. std::__unlock(std::get<_Indx>(__mt)...);
  414. }
  415. _MutexTuple __t_;
  416. };
  417. _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(scoped_lock);
  418. # endif // _LIBCPP_STD_VER >= 17
  419. #endif // !_LIBCPP_HAS_NO_THREADS
  420. _LIBCPP_END_NAMESPACE_STD
  421. _LIBCPP_POP_MACROS
  422. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  423. # include <atomic>
  424. # include <concepts>
  425. # include <cstdlib>
  426. # include <cstring>
  427. # include <ctime>
  428. # include <initializer_list>
  429. # include <iosfwd>
  430. # include <new>
  431. # include <stdexcept>
  432. # include <system_error>
  433. # include <type_traits>
  434. # include <typeinfo>
  435. #endif
  436. #endif // _LIBCPP_MUTEX