mutex 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. // -*- C++ -*-
  2. //===--------------------------- mutex ------------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_MUTEX
  11. #define _LIBCPP_MUTEX
  12. /*
  13. mutex synopsis
  14. namespace std
  15. {
  16. class mutex
  17. {
  18. public:
  19. constexpr mutex() noexcept;
  20. ~mutex();
  21. mutex(const mutex&) = delete;
  22. mutex& operator=(const mutex&) = delete;
  23. void lock();
  24. bool try_lock();
  25. void unlock();
  26. typedef pthread_mutex_t* native_handle_type;
  27. native_handle_type native_handle();
  28. };
  29. class recursive_mutex
  30. {
  31. public:
  32. recursive_mutex();
  33. ~recursive_mutex();
  34. recursive_mutex(const recursive_mutex&) = delete;
  35. recursive_mutex& operator=(const recursive_mutex&) = delete;
  36. void lock();
  37. bool try_lock() noexcept;
  38. void unlock();
  39. typedef pthread_mutex_t* native_handle_type;
  40. native_handle_type native_handle();
  41. };
  42. class timed_mutex
  43. {
  44. public:
  45. timed_mutex();
  46. ~timed_mutex();
  47. timed_mutex(const timed_mutex&) = delete;
  48. timed_mutex& operator=(const timed_mutex&) = delete;
  49. void lock();
  50. bool try_lock();
  51. template <class Rep, class Period>
  52. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
  53. template <class Clock, class Duration>
  54. bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
  55. void unlock();
  56. };
  57. class recursive_timed_mutex
  58. {
  59. public:
  60. recursive_timed_mutex();
  61. ~recursive_timed_mutex();
  62. recursive_timed_mutex(const recursive_timed_mutex&) = delete;
  63. recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
  64. void lock();
  65. bool try_lock() noexcept;
  66. template <class Rep, class Period>
  67. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
  68. template <class Clock, class Duration>
  69. bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
  70. void unlock();
  71. };
  72. struct defer_lock_t {};
  73. struct try_to_lock_t {};
  74. struct adopt_lock_t {};
  75. constexpr defer_lock_t defer_lock{};
  76. constexpr try_to_lock_t try_to_lock{};
  77. constexpr adopt_lock_t adopt_lock{};
  78. template <class Mutex>
  79. class lock_guard
  80. {
  81. public:
  82. typedef Mutex mutex_type;
  83. explicit lock_guard(mutex_type& m);
  84. lock_guard(mutex_type& m, adopt_lock_t);
  85. ~lock_guard();
  86. lock_guard(lock_guard const&) = delete;
  87. lock_guard& operator=(lock_guard const&) = delete;
  88. };
  89. template <class... MutexTypes> // Variadic lock_guard only provided in ABI V2.
  90. class lock_guard
  91. {
  92. public:
  93. explicit lock_guard(MutexTypes&... m);
  94. lock_guard(MutexTypes&... m, adopt_lock_t);
  95. ~lock_guard();
  96. lock_guard(lock_guard const&) = delete;
  97. lock_guard& operator=(lock_guard 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 <__config>
  150. #if defined(_LIBCPP_SGX_CONFIG)
  151. #include <support/sgx/sgx_mutex>
  152. #else // !defined(_LIBCPP_SGX_CONFIG)
  153. #include <__mutex_base>
  154. #include <functional>
  155. #include <memory>
  156. #ifndef _LIBCPP_HAS_NO_VARIADICS
  157. #include <tuple>
  158. #endif
  159. #include <__threading_support>
  160. #include <__undef_min_max>
  161. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  162. #pragma GCC system_header
  163. #endif
  164. _LIBCPP_BEGIN_NAMESPACE_STD
  165. #ifndef _LIBCPP_HAS_NO_THREADS
  166. class _LIBCPP_TYPE_VIS recursive_mutex
  167. {
  168. __libcpp_mutex_t __m_;
  169. public:
  170. recursive_mutex();
  171. ~recursive_mutex();
  172. private:
  173. recursive_mutex(const recursive_mutex&); // = delete;
  174. recursive_mutex& operator=(const recursive_mutex&); // = delete;
  175. public:
  176. void lock();
  177. bool try_lock() _NOEXCEPT;
  178. void unlock() _NOEXCEPT;
  179. typedef __libcpp_mutex_t* native_handle_type;
  180. _LIBCPP_INLINE_VISIBILITY
  181. native_handle_type native_handle() {return &__m_;}
  182. };
  183. class _LIBCPP_TYPE_VIS timed_mutex
  184. {
  185. mutex __m_;
  186. condition_variable __cv_;
  187. bool __locked_;
  188. public:
  189. timed_mutex();
  190. ~timed_mutex();
  191. private:
  192. timed_mutex(const timed_mutex&); // = delete;
  193. timed_mutex& operator=(const timed_mutex&); // = delete;
  194. public:
  195. void lock();
  196. bool try_lock() _NOEXCEPT;
  197. template <class _Rep, class _Period>
  198. _LIBCPP_INLINE_VISIBILITY
  199. bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
  200. {return try_lock_until(chrono::steady_clock::now() + __d);}
  201. template <class _Clock, class _Duration>
  202. bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
  203. void unlock() _NOEXCEPT;
  204. };
  205. template <class _Clock, class _Duration>
  206. bool
  207. timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
  208. {
  209. using namespace chrono;
  210. unique_lock<mutex> __lk(__m_);
  211. bool no_timeout = _Clock::now() < __t;
  212. while (no_timeout && __locked_)
  213. no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
  214. if (!__locked_)
  215. {
  216. __locked_ = true;
  217. return true;
  218. }
  219. return false;
  220. }
  221. class _LIBCPP_TYPE_VIS recursive_timed_mutex
  222. {
  223. mutex __m_;
  224. condition_variable __cv_;
  225. size_t __count_;
  226. __libcpp_thread_id __id_;
  227. public:
  228. recursive_timed_mutex();
  229. ~recursive_timed_mutex();
  230. private:
  231. recursive_timed_mutex(const recursive_timed_mutex&); // = delete;
  232. recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete;
  233. public:
  234. void lock();
  235. bool try_lock() _NOEXCEPT;
  236. template <class _Rep, class _Period>
  237. _LIBCPP_INLINE_VISIBILITY
  238. bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
  239. {return try_lock_until(chrono::steady_clock::now() + __d);}
  240. template <class _Clock, class _Duration>
  241. bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
  242. void unlock() _NOEXCEPT;
  243. };
  244. template <class _Clock, class _Duration>
  245. bool
  246. recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
  247. {
  248. using namespace chrono;
  249. __libcpp_thread_id __id = __libcpp_thread_get_current_id();
  250. unique_lock<mutex> lk(__m_);
  251. if (__libcpp_thread_id_equal(__id, __id_))
  252. {
  253. if (__count_ == numeric_limits<size_t>::max())
  254. return false;
  255. ++__count_;
  256. return true;
  257. }
  258. bool no_timeout = _Clock::now() < __t;
  259. while (no_timeout && __count_ != 0)
  260. no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout;
  261. if (__count_ == 0)
  262. {
  263. __count_ = 1;
  264. __id_ = __id;
  265. return true;
  266. }
  267. return false;
  268. }
  269. template <class _L0, class _L1>
  270. int
  271. try_lock(_L0& __l0, _L1& __l1)
  272. {
  273. unique_lock<_L0> __u0(__l0, try_to_lock);
  274. if (__u0.owns_lock())
  275. {
  276. if (__l1.try_lock())
  277. {
  278. __u0.release();
  279. return -1;
  280. }
  281. else
  282. return 1;
  283. }
  284. return 0;
  285. }
  286. #ifndef _LIBCPP_HAS_NO_VARIADICS
  287. template <class _L0, class _L1, class _L2, class... _L3>
  288. int
  289. try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
  290. {
  291. int __r = 0;
  292. unique_lock<_L0> __u0(__l0, try_to_lock);
  293. if (__u0.owns_lock())
  294. {
  295. __r = try_lock(__l1, __l2, __l3...);
  296. if (__r == -1)
  297. __u0.release();
  298. else
  299. ++__r;
  300. }
  301. return __r;
  302. }
  303. #endif // _LIBCPP_HAS_NO_VARIADICS
  304. template <class _L0, class _L1>
  305. void
  306. lock(_L0& __l0, _L1& __l1)
  307. {
  308. while (true)
  309. {
  310. {
  311. unique_lock<_L0> __u0(__l0);
  312. if (__l1.try_lock())
  313. {
  314. __u0.release();
  315. break;
  316. }
  317. }
  318. __libcpp_thread_yield();
  319. {
  320. unique_lock<_L1> __u1(__l1);
  321. if (__l0.try_lock())
  322. {
  323. __u1.release();
  324. break;
  325. }
  326. }
  327. __libcpp_thread_yield();
  328. }
  329. }
  330. #ifndef _LIBCPP_HAS_NO_VARIADICS
  331. template <class _L0, class _L1, class _L2, class ..._L3>
  332. void
  333. __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
  334. {
  335. while (true)
  336. {
  337. switch (__i)
  338. {
  339. case 0:
  340. {
  341. unique_lock<_L0> __u0(__l0);
  342. __i = try_lock(__l1, __l2, __l3...);
  343. if (__i == -1)
  344. {
  345. __u0.release();
  346. return;
  347. }
  348. }
  349. ++__i;
  350. __libcpp_thread_yield();
  351. break;
  352. case 1:
  353. {
  354. unique_lock<_L1> __u1(__l1);
  355. __i = try_lock(__l2, __l3..., __l0);
  356. if (__i == -1)
  357. {
  358. __u1.release();
  359. return;
  360. }
  361. }
  362. if (__i == sizeof...(_L3) + 1)
  363. __i = 0;
  364. else
  365. __i += 2;
  366. __libcpp_thread_yield();
  367. break;
  368. default:
  369. __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
  370. return;
  371. }
  372. }
  373. }
  374. template <class _L0, class _L1, class _L2, class ..._L3>
  375. inline _LIBCPP_INLINE_VISIBILITY
  376. void
  377. lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
  378. {
  379. __lock_first(0, __l0, __l1, __l2, __l3...);
  380. }
  381. template <class _L0>
  382. inline _LIBCPP_INLINE_VISIBILITY
  383. void __unlock(_L0& __l0) {
  384. __l0.unlock();
  385. }
  386. template <class _L0, class _L1>
  387. inline _LIBCPP_INLINE_VISIBILITY
  388. void __unlock(_L0& __l0, _L1& __l1) {
  389. __l0.unlock();
  390. __l1.unlock();
  391. }
  392. template <class _L0, class _L1, class _L2, class ..._L3>
  393. inline _LIBCPP_INLINE_VISIBILITY
  394. void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
  395. __l0.unlock();
  396. __l1.unlock();
  397. _VSTD::__unlock(__l2, __l3...);
  398. }
  399. #endif // _LIBCPP_HAS_NO_VARIADICS
  400. #endif // !_LIBCPP_HAS_NO_THREADS
  401. struct _LIBCPP_TYPE_VIS_ONLY once_flag;
  402. #ifndef _LIBCPP_HAS_NO_VARIADICS
  403. template<class _Callable, class... _Args>
  404. _LIBCPP_INLINE_VISIBILITY
  405. void call_once(once_flag&, _Callable&&, _Args&&...);
  406. #else // _LIBCPP_HAS_NO_VARIADICS
  407. template<class _Callable>
  408. _LIBCPP_INLINE_VISIBILITY
  409. void call_once(once_flag&, _Callable&);
  410. template<class _Callable>
  411. _LIBCPP_INLINE_VISIBILITY
  412. void call_once(once_flag&, const _Callable&);
  413. #endif // _LIBCPP_HAS_NO_VARIADICS
  414. struct _LIBCPP_TYPE_VIS_ONLY once_flag
  415. {
  416. _LIBCPP_INLINE_VISIBILITY
  417. _LIBCPP_CONSTEXPR
  418. once_flag() _NOEXCEPT : __state_(0) {}
  419. private:
  420. once_flag(const once_flag&); // = delete;
  421. once_flag& operator=(const once_flag&); // = delete;
  422. unsigned long __state_;
  423. #ifndef _LIBCPP_HAS_NO_VARIADICS
  424. template<class _Callable, class... _Args>
  425. friend
  426. void call_once(once_flag&, _Callable&&, _Args&&...);
  427. #else // _LIBCPP_HAS_NO_VARIADICS
  428. template<class _Callable>
  429. friend
  430. void call_once(once_flag&, _Callable&);
  431. template<class _Callable>
  432. friend
  433. void call_once(once_flag&, const _Callable&);
  434. #endif // _LIBCPP_HAS_NO_VARIADICS
  435. };
  436. #ifndef _LIBCPP_HAS_NO_VARIADICS
  437. template <class _Fp>
  438. class __call_once_param
  439. {
  440. _Fp& __f_;
  441. public:
  442. _LIBCPP_INLINE_VISIBILITY
  443. explicit __call_once_param(_Fp& __f) : __f_(__f) {}
  444. _LIBCPP_INLINE_VISIBILITY
  445. void operator()()
  446. {
  447. typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
  448. __execute(_Index());
  449. }
  450. private:
  451. template <size_t ..._Indices>
  452. _LIBCPP_INLINE_VISIBILITY
  453. void __execute(__tuple_indices<_Indices...>)
  454. {
  455. __invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
  456. }
  457. };
  458. #else
  459. template <class _Fp>
  460. class __call_once_param
  461. {
  462. _Fp& __f_;
  463. public:
  464. _LIBCPP_INLINE_VISIBILITY
  465. explicit __call_once_param(_Fp& __f) : __f_(__f) {}
  466. _LIBCPP_INLINE_VISIBILITY
  467. void operator()()
  468. {
  469. __f_();
  470. }
  471. };
  472. #endif
  473. template <class _Fp>
  474. void
  475. __call_once_proxy(void* __vp)
  476. {
  477. __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
  478. (*__p)();
  479. }
  480. _LIBCPP_FUNC_VIS void __call_once(volatile unsigned long&, void*, void(*)(void*));
  481. #ifndef _LIBCPP_HAS_NO_VARIADICS
  482. template<class _Callable, class... _Args>
  483. inline _LIBCPP_INLINE_VISIBILITY
  484. void
  485. call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
  486. {
  487. if (__libcpp_relaxed_load(&__flag.__state_) != ~0ul)
  488. {
  489. typedef tuple<_Callable&&, _Args&&...> _Gp;
  490. _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
  491. __call_once_param<_Gp> __p(__f);
  492. __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
  493. }
  494. }
  495. #else // _LIBCPP_HAS_NO_VARIADICS
  496. template<class _Callable>
  497. inline _LIBCPP_INLINE_VISIBILITY
  498. void
  499. call_once(once_flag& __flag, _Callable& __func)
  500. {
  501. if (__libcpp_relaxed_load(&__flag.__state_) != ~0ul)
  502. {
  503. __call_once_param<_Callable> __p(__func);
  504. __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
  505. }
  506. }
  507. template<class _Callable>
  508. inline _LIBCPP_INLINE_VISIBILITY
  509. void
  510. call_once(once_flag& __flag, const _Callable& __func)
  511. {
  512. if (__flag.__state_ != ~0ul)
  513. {
  514. __call_once_param<const _Callable> __p(__func);
  515. __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
  516. }
  517. }
  518. #endif // _LIBCPP_HAS_NO_VARIADICS
  519. #if defined(_LIBCPP_ABI_VARIADIC_LOCK_GUARD) \
  520. && !defined(_LIBCPP_CXX03_LANG)
  521. template <>
  522. class _LIBCPP_TYPE_VIS_ONLY lock_guard<> {
  523. public:
  524. explicit lock_guard() {}
  525. ~lock_guard() = default;
  526. _LIBCPP_INLINE_VISIBILITY
  527. explicit lock_guard(adopt_lock_t) {}
  528. lock_guard(lock_guard const&) = delete;
  529. lock_guard& operator=(lock_guard const&) = delete;
  530. };
  531. template <class ..._MArgs>
  532. class _LIBCPP_TYPE_VIS_ONLY lock_guard
  533. {
  534. static_assert(sizeof...(_MArgs) >= 2, "At least 2 lock types required");
  535. typedef tuple<_MArgs&...> _MutexTuple;
  536. public:
  537. _LIBCPP_INLINE_VISIBILITY
  538. explicit lock_guard(_MArgs&... __margs)
  539. : __t_(__margs...)
  540. {
  541. _VSTD::lock(__margs...);
  542. }
  543. _LIBCPP_INLINE_VISIBILITY
  544. lock_guard(_MArgs&... __margs, adopt_lock_t)
  545. : __t_(__margs...)
  546. {
  547. }
  548. _LIBCPP_INLINE_VISIBILITY
  549. ~lock_guard() {
  550. typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
  551. __unlock_unpack(_Indices{}, __t_);
  552. }
  553. lock_guard(lock_guard const&) = delete;
  554. lock_guard& operator=(lock_guard const&) = delete;
  555. private:
  556. template <size_t ..._Indx>
  557. _LIBCPP_INLINE_VISIBILITY
  558. static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
  559. _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...);
  560. }
  561. _MutexTuple __t_;
  562. };
  563. #endif // _LIBCPP_ABI_VARIADIC_LOCK_GUARD
  564. _LIBCPP_END_NAMESPACE_STD
  565. #endif // defined(_LIBCPP_SGX_CONFIG)
  566. #endif // _LIBCPP_MUTEX