mutex 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 <__assert> // all public C++ headers provide the assertion handler
  150. #include <__chrono/steady_clock.h>
  151. #include <__chrono/time_point.h>
  152. #include <__condition_variable/condition_variable.h>
  153. #include <__config>
  154. #include <__memory/shared_ptr.h>
  155. #include <__mutex/lock_guard.h>
  156. #include <__mutex/mutex.h>
  157. #include <__mutex/once_flag.h>
  158. #include <__mutex/tag_types.h>
  159. #include <__mutex/unique_lock.h>
  160. #include <__thread/id.h>
  161. #include <__threading_support>
  162. #include <__utility/forward.h>
  163. #include <cstdint>
  164. #include <limits>
  165. #ifndef _LIBCPP_CXX03_LANG
  166. # include <tuple>
  167. #endif
  168. #include <version>
  169. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  170. # pragma GCC system_header
  171. #endif
  172. _LIBCPP_PUSH_MACROS
  173. #include <__undef_macros>
  174. _LIBCPP_BEGIN_NAMESPACE_STD
  175. #ifndef _LIBCPP_HAS_NO_THREADS
  176. class _LIBCPP_EXPORTED_FROM_ABI recursive_mutex
  177. {
  178. __libcpp_recursive_mutex_t __m_;
  179. public:
  180. recursive_mutex();
  181. ~recursive_mutex();
  182. recursive_mutex(const recursive_mutex&) = delete;
  183. recursive_mutex& operator=(const recursive_mutex&) = delete;
  184. void lock();
  185. bool try_lock() _NOEXCEPT;
  186. void unlock() _NOEXCEPT;
  187. typedef __libcpp_recursive_mutex_t* native_handle_type;
  188. _LIBCPP_INLINE_VISIBILITY
  189. native_handle_type native_handle() {return &__m_;}
  190. };
  191. class _LIBCPP_EXPORTED_FROM_ABI timed_mutex
  192. {
  193. mutex __m_;
  194. condition_variable __cv_;
  195. bool __locked_;
  196. public:
  197. timed_mutex();
  198. ~timed_mutex();
  199. timed_mutex(const timed_mutex&) = delete;
  200. timed_mutex& operator=(const timed_mutex&) = delete;
  201. public:
  202. void lock();
  203. bool try_lock() _NOEXCEPT;
  204. template <class _Rep, class _Period>
  205. _LIBCPP_INLINE_VISIBILITY
  206. bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
  207. {return try_lock_until(chrono::steady_clock::now() + __d);}
  208. template <class _Clock, class _Duration>
  209. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
  210. bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
  211. void unlock() _NOEXCEPT;
  212. };
  213. template <class _Clock, class _Duration>
  214. bool
  215. timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
  216. {
  217. using namespace chrono;
  218. unique_lock<mutex> __lk(__m_);
  219. bool __no_timeout = _Clock::now() < __t;
  220. while (__no_timeout && __locked_)
  221. __no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
  222. if (!__locked_)
  223. {
  224. __locked_ = true;
  225. return true;
  226. }
  227. return false;
  228. }
  229. class _LIBCPP_EXPORTED_FROM_ABI recursive_timed_mutex
  230. {
  231. mutex __m_;
  232. condition_variable __cv_;
  233. size_t __count_;
  234. __thread_id __id_;
  235. public:
  236. recursive_timed_mutex();
  237. ~recursive_timed_mutex();
  238. recursive_timed_mutex(const recursive_timed_mutex&) = delete;
  239. recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
  240. void lock();
  241. bool try_lock() _NOEXCEPT;
  242. template <class _Rep, class _Period>
  243. _LIBCPP_INLINE_VISIBILITY
  244. bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
  245. {return try_lock_until(chrono::steady_clock::now() + __d);}
  246. template <class _Clock, class _Duration>
  247. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
  248. bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
  249. void unlock() _NOEXCEPT;
  250. };
  251. template <class _Clock, class _Duration>
  252. bool
  253. recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
  254. {
  255. using namespace chrono;
  256. __thread_id __id = this_thread::get_id();
  257. unique_lock<mutex> __lk(__m_);
  258. if (__id == __id_)
  259. {
  260. if (__count_ == numeric_limits<size_t>::max())
  261. return false;
  262. ++__count_;
  263. return true;
  264. }
  265. bool __no_timeout = _Clock::now() < __t;
  266. while (__no_timeout && __count_ != 0)
  267. __no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
  268. if (__count_ == 0)
  269. {
  270. __count_ = 1;
  271. __id_ = __id;
  272. return true;
  273. }
  274. return false;
  275. }
  276. template <class _L0, class _L1>
  277. _LIBCPP_HIDE_FROM_ABI int
  278. try_lock(_L0& __l0, _L1& __l1)
  279. {
  280. unique_lock<_L0> __u0(__l0, try_to_lock_t());
  281. if (__u0.owns_lock())
  282. {
  283. if (__l1.try_lock())
  284. {
  285. __u0.release();
  286. return -1;
  287. }
  288. else
  289. return 1;
  290. }
  291. return 0;
  292. }
  293. #ifndef _LIBCPP_CXX03_LANG
  294. template <class _L0, class _L1, class _L2, class... _L3>
  295. _LIBCPP_HIDE_FROM_ABI int
  296. try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
  297. {
  298. int __r = 0;
  299. unique_lock<_L0> __u0(__l0, try_to_lock);
  300. if (__u0.owns_lock())
  301. {
  302. __r = std::try_lock(__l1, __l2, __l3...);
  303. if (__r == -1)
  304. __u0.release();
  305. else
  306. ++__r;
  307. }
  308. return __r;
  309. }
  310. #endif // _LIBCPP_CXX03_LANG
  311. template <class _L0, class _L1>
  312. _LIBCPP_HIDE_FROM_ABI void
  313. lock(_L0& __l0, _L1& __l1)
  314. {
  315. while (true)
  316. {
  317. {
  318. unique_lock<_L0> __u0(__l0);
  319. if (__l1.try_lock())
  320. {
  321. __u0.release();
  322. break;
  323. }
  324. }
  325. __libcpp_thread_yield();
  326. {
  327. unique_lock<_L1> __u1(__l1);
  328. if (__l0.try_lock())
  329. {
  330. __u1.release();
  331. break;
  332. }
  333. }
  334. __libcpp_thread_yield();
  335. }
  336. }
  337. #ifndef _LIBCPP_CXX03_LANG
  338. template <class _L0, class _L1, class _L2, class ..._L3>
  339. void
  340. __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
  341. {
  342. while (true)
  343. {
  344. switch (__i)
  345. {
  346. case 0:
  347. {
  348. unique_lock<_L0> __u0(__l0);
  349. __i = std::try_lock(__l1, __l2, __l3...);
  350. if (__i == -1)
  351. {
  352. __u0.release();
  353. return;
  354. }
  355. }
  356. ++__i;
  357. __libcpp_thread_yield();
  358. break;
  359. case 1:
  360. {
  361. unique_lock<_L1> __u1(__l1);
  362. __i = std::try_lock(__l2, __l3..., __l0);
  363. if (__i == -1)
  364. {
  365. __u1.release();
  366. return;
  367. }
  368. }
  369. if (__i == sizeof...(_L3) + 1)
  370. __i = 0;
  371. else
  372. __i += 2;
  373. __libcpp_thread_yield();
  374. break;
  375. default:
  376. std::__lock_first(__i - 2, __l2, __l3..., __l0, __l1);
  377. return;
  378. }
  379. }
  380. }
  381. template <class _L0, class _L1, class _L2, class ..._L3>
  382. inline _LIBCPP_INLINE_VISIBILITY
  383. void
  384. lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
  385. {
  386. std::__lock_first(0, __l0, __l1, __l2, __l3...);
  387. }
  388. template <class _L0>
  389. inline _LIBCPP_INLINE_VISIBILITY
  390. void __unlock(_L0& __l0) {
  391. __l0.unlock();
  392. }
  393. template <class _L0, class _L1>
  394. inline _LIBCPP_INLINE_VISIBILITY
  395. void __unlock(_L0& __l0, _L1& __l1) {
  396. __l0.unlock();
  397. __l1.unlock();
  398. }
  399. template <class _L0, class _L1, class _L2, class ..._L3>
  400. inline _LIBCPP_INLINE_VISIBILITY
  401. void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
  402. __l0.unlock();
  403. __l1.unlock();
  404. _VSTD::__unlock(__l2, __l3...);
  405. }
  406. #endif // _LIBCPP_CXX03_LANG
  407. #if _LIBCPP_STD_VER >= 17
  408. template <class ..._Mutexes>
  409. class _LIBCPP_TEMPLATE_VIS scoped_lock;
  410. template <>
  411. class _LIBCPP_TEMPLATE_VIS scoped_lock<> {
  412. public:
  413. explicit scoped_lock() {}
  414. ~scoped_lock() = default;
  415. _LIBCPP_INLINE_VISIBILITY
  416. explicit scoped_lock(adopt_lock_t) {}
  417. scoped_lock(scoped_lock const&) = delete;
  418. scoped_lock& operator=(scoped_lock const&) = delete;
  419. };
  420. template <class _Mutex>
  421. class _LIBCPP_TEMPLATE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) scoped_lock<_Mutex> {
  422. public:
  423. typedef _Mutex mutex_type;
  424. private:
  425. mutex_type& __m_;
  426. public:
  427. explicit scoped_lock(mutex_type & __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
  428. : __m_(__m) {__m_.lock();}
  429. ~scoped_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}
  430. _LIBCPP_INLINE_VISIBILITY
  431. explicit scoped_lock(adopt_lock_t, mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
  432. : __m_(__m) {}
  433. scoped_lock(scoped_lock const&) = delete;
  434. scoped_lock& operator=(scoped_lock const&) = delete;
  435. };
  436. template <class ..._MArgs>
  437. class _LIBCPP_TEMPLATE_VIS scoped_lock
  438. {
  439. static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required");
  440. typedef tuple<_MArgs&...> _MutexTuple;
  441. public:
  442. _LIBCPP_INLINE_VISIBILITY
  443. explicit scoped_lock(_MArgs&... __margs)
  444. : __t_(__margs...)
  445. {
  446. _VSTD::lock(__margs...);
  447. }
  448. _LIBCPP_INLINE_VISIBILITY
  449. scoped_lock(adopt_lock_t, _MArgs&... __margs)
  450. : __t_(__margs...)
  451. {
  452. }
  453. _LIBCPP_INLINE_VISIBILITY
  454. ~scoped_lock() {
  455. typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
  456. __unlock_unpack(_Indices{}, __t_);
  457. }
  458. scoped_lock(scoped_lock const&) = delete;
  459. scoped_lock& operator=(scoped_lock const&) = delete;
  460. private:
  461. template <size_t ..._Indx>
  462. _LIBCPP_INLINE_VISIBILITY
  463. static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
  464. _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...);
  465. }
  466. _MutexTuple __t_;
  467. };
  468. _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(scoped_lock);
  469. #endif // _LIBCPP_STD_VER >= 17
  470. #endif // !_LIBCPP_HAS_NO_THREADS
  471. _LIBCPP_END_NAMESPACE_STD
  472. _LIBCPP_POP_MACROS
  473. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  474. # include <atomic>
  475. # include <concepts>
  476. # include <cstdlib>
  477. # include <cstring>
  478. # include <ctime>
  479. # include <initializer_list>
  480. # include <new>
  481. # include <stdexcept>
  482. # include <system_error>
  483. # include <type_traits>
  484. # include <typeinfo>
  485. #endif
  486. #endif // _LIBCPP_MUTEX