mutex 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 <__config>
  151. #include <__mutex_base>
  152. #include <__threading_support>
  153. #include <__utility/forward.h>
  154. #include <cstdint>
  155. #include <memory>
  156. #ifndef _LIBCPP_CXX03_LANG
  157. # include <tuple>
  158. #endif
  159. #include <version>
  160. #ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
  161. # include <functional>
  162. #endif
  163. #if !defined(_LIBCPP_HAS_NO_THREADS) && !defined(_LIBCPP_CXX03_LANG)
  164. #include <atomic>
  165. #endif
  166. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  167. # pragma GCC system_header
  168. #endif
  169. _LIBCPP_PUSH_MACROS
  170. #include <__undef_macros>
  171. _LIBCPP_BEGIN_NAMESPACE_STD
  172. #ifndef _LIBCPP_HAS_NO_THREADS
  173. class _LIBCPP_TYPE_VIS recursive_mutex
  174. {
  175. __libcpp_recursive_mutex_t __m_;
  176. public:
  177. recursive_mutex();
  178. ~recursive_mutex();
  179. recursive_mutex(const recursive_mutex&) = delete;
  180. recursive_mutex& operator=(const recursive_mutex&) = delete;
  181. void lock();
  182. bool try_lock() _NOEXCEPT;
  183. void unlock() _NOEXCEPT;
  184. typedef __libcpp_recursive_mutex_t* native_handle_type;
  185. _LIBCPP_INLINE_VISIBILITY
  186. native_handle_type native_handle() {return &__m_;}
  187. };
  188. class _LIBCPP_TYPE_VIS timed_mutex
  189. {
  190. mutex __m_;
  191. condition_variable __cv_;
  192. bool __locked_;
  193. public:
  194. timed_mutex();
  195. ~timed_mutex();
  196. timed_mutex(const timed_mutex&) = delete;
  197. timed_mutex& operator=(const timed_mutex&) = delete;
  198. public:
  199. void lock();
  200. bool try_lock() _NOEXCEPT;
  201. template <class _Rep, class _Period>
  202. _LIBCPP_INLINE_VISIBILITY
  203. bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
  204. {return try_lock_until(chrono::steady_clock::now() + __d);}
  205. template <class _Clock, class _Duration>
  206. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
  207. bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
  208. void unlock() _NOEXCEPT;
  209. };
  210. template <class _Clock, class _Duration>
  211. bool
  212. timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
  213. {
  214. using namespace chrono;
  215. unique_lock<mutex> __lk(__m_);
  216. bool no_timeout = _Clock::now() < __t;
  217. while (no_timeout && __locked_)
  218. no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
  219. if (!__locked_)
  220. {
  221. __locked_ = true;
  222. return true;
  223. }
  224. return false;
  225. }
  226. class _LIBCPP_TYPE_VIS recursive_timed_mutex
  227. {
  228. mutex __m_;
  229. condition_variable __cv_;
  230. size_t __count_;
  231. __thread_id __id_;
  232. public:
  233. recursive_timed_mutex();
  234. ~recursive_timed_mutex();
  235. recursive_timed_mutex(const recursive_timed_mutex&) = delete;
  236. recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
  237. void lock();
  238. bool try_lock() _NOEXCEPT;
  239. template <class _Rep, class _Period>
  240. _LIBCPP_INLINE_VISIBILITY
  241. bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
  242. {return try_lock_until(chrono::steady_clock::now() + __d);}
  243. template <class _Clock, class _Duration>
  244. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
  245. bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
  246. void unlock() _NOEXCEPT;
  247. };
  248. template <class _Clock, class _Duration>
  249. bool
  250. recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
  251. {
  252. using namespace chrono;
  253. __thread_id __id = this_thread::get_id();
  254. unique_lock<mutex> lk(__m_);
  255. if (__id == __id_)
  256. {
  257. if (__count_ == numeric_limits<size_t>::max())
  258. return false;
  259. ++__count_;
  260. return true;
  261. }
  262. bool no_timeout = _Clock::now() < __t;
  263. while (no_timeout && __count_ != 0)
  264. no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout;
  265. if (__count_ == 0)
  266. {
  267. __count_ = 1;
  268. __id_ = __id;
  269. return true;
  270. }
  271. return false;
  272. }
  273. template <class _L0, class _L1>
  274. int
  275. try_lock(_L0& __l0, _L1& __l1)
  276. {
  277. unique_lock<_L0> __u0(__l0, try_to_lock);
  278. if (__u0.owns_lock())
  279. {
  280. if (__l1.try_lock())
  281. {
  282. __u0.release();
  283. return -1;
  284. }
  285. else
  286. return 1;
  287. }
  288. return 0;
  289. }
  290. #ifndef _LIBCPP_CXX03_LANG
  291. template <class _L0, class _L1, class _L2, class... _L3>
  292. int
  293. try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
  294. {
  295. int __r = 0;
  296. unique_lock<_L0> __u0(__l0, try_to_lock);
  297. if (__u0.owns_lock())
  298. {
  299. __r = try_lock(__l1, __l2, __l3...);
  300. if (__r == -1)
  301. __u0.release();
  302. else
  303. ++__r;
  304. }
  305. return __r;
  306. }
  307. #endif // _LIBCPP_CXX03_LANG
  308. template <class _L0, class _L1>
  309. void
  310. lock(_L0& __l0, _L1& __l1)
  311. {
  312. while (true)
  313. {
  314. {
  315. unique_lock<_L0> __u0(__l0);
  316. if (__l1.try_lock())
  317. {
  318. __u0.release();
  319. break;
  320. }
  321. }
  322. __libcpp_thread_yield();
  323. {
  324. unique_lock<_L1> __u1(__l1);
  325. if (__l0.try_lock())
  326. {
  327. __u1.release();
  328. break;
  329. }
  330. }
  331. __libcpp_thread_yield();
  332. }
  333. }
  334. #ifndef _LIBCPP_CXX03_LANG
  335. template <class _L0, class _L1, class _L2, class ..._L3>
  336. void
  337. __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
  338. {
  339. while (true)
  340. {
  341. switch (__i)
  342. {
  343. case 0:
  344. {
  345. unique_lock<_L0> __u0(__l0);
  346. __i = try_lock(__l1, __l2, __l3...);
  347. if (__i == -1)
  348. {
  349. __u0.release();
  350. return;
  351. }
  352. }
  353. ++__i;
  354. __libcpp_thread_yield();
  355. break;
  356. case 1:
  357. {
  358. unique_lock<_L1> __u1(__l1);
  359. __i = try_lock(__l2, __l3..., __l0);
  360. if (__i == -1)
  361. {
  362. __u1.release();
  363. return;
  364. }
  365. }
  366. if (__i == sizeof...(_L3) + 1)
  367. __i = 0;
  368. else
  369. __i += 2;
  370. __libcpp_thread_yield();
  371. break;
  372. default:
  373. __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
  374. return;
  375. }
  376. }
  377. }
  378. template <class _L0, class _L1, class _L2, class ..._L3>
  379. inline _LIBCPP_INLINE_VISIBILITY
  380. void
  381. lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
  382. {
  383. __lock_first(0, __l0, __l1, __l2, __l3...);
  384. }
  385. template <class _L0>
  386. inline _LIBCPP_INLINE_VISIBILITY
  387. void __unlock(_L0& __l0) {
  388. __l0.unlock();
  389. }
  390. template <class _L0, class _L1>
  391. inline _LIBCPP_INLINE_VISIBILITY
  392. void __unlock(_L0& __l0, _L1& __l1) {
  393. __l0.unlock();
  394. __l1.unlock();
  395. }
  396. template <class _L0, class _L1, class _L2, class ..._L3>
  397. inline _LIBCPP_INLINE_VISIBILITY
  398. void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
  399. __l0.unlock();
  400. __l1.unlock();
  401. _VSTD::__unlock(__l2, __l3...);
  402. }
  403. #endif // _LIBCPP_CXX03_LANG
  404. #if _LIBCPP_STD_VER > 14
  405. template <class ..._Mutexes>
  406. class _LIBCPP_TEMPLATE_VIS scoped_lock;
  407. template <>
  408. class _LIBCPP_TEMPLATE_VIS scoped_lock<> {
  409. public:
  410. explicit scoped_lock() {}
  411. ~scoped_lock() = default;
  412. _LIBCPP_INLINE_VISIBILITY
  413. explicit scoped_lock(adopt_lock_t) {}
  414. scoped_lock(scoped_lock const&) = delete;
  415. scoped_lock& operator=(scoped_lock const&) = delete;
  416. };
  417. template <class _Mutex>
  418. class _LIBCPP_TEMPLATE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) scoped_lock<_Mutex> {
  419. public:
  420. typedef _Mutex mutex_type;
  421. private:
  422. mutex_type& __m_;
  423. public:
  424. explicit scoped_lock(mutex_type & __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
  425. : __m_(__m) {__m_.lock();}
  426. ~scoped_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}
  427. _LIBCPP_INLINE_VISIBILITY
  428. explicit scoped_lock(adopt_lock_t, mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
  429. : __m_(__m) {}
  430. scoped_lock(scoped_lock const&) = delete;
  431. scoped_lock& operator=(scoped_lock const&) = delete;
  432. };
  433. template <class ..._MArgs>
  434. class _LIBCPP_TEMPLATE_VIS scoped_lock
  435. {
  436. static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required");
  437. typedef tuple<_MArgs&...> _MutexTuple;
  438. public:
  439. _LIBCPP_INLINE_VISIBILITY
  440. explicit scoped_lock(_MArgs&... __margs)
  441. : __t_(__margs...)
  442. {
  443. _VSTD::lock(__margs...);
  444. }
  445. _LIBCPP_INLINE_VISIBILITY
  446. scoped_lock(adopt_lock_t, _MArgs&... __margs)
  447. : __t_(__margs...)
  448. {
  449. }
  450. _LIBCPP_INLINE_VISIBILITY
  451. ~scoped_lock() {
  452. typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
  453. __unlock_unpack(_Indices{}, __t_);
  454. }
  455. scoped_lock(scoped_lock const&) = delete;
  456. scoped_lock& operator=(scoped_lock const&) = delete;
  457. private:
  458. template <size_t ..._Indx>
  459. _LIBCPP_INLINE_VISIBILITY
  460. static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
  461. _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...);
  462. }
  463. _MutexTuple __t_;
  464. };
  465. #endif // _LIBCPP_STD_VER > 14
  466. #endif // !_LIBCPP_HAS_NO_THREADS
  467. struct _LIBCPP_TEMPLATE_VIS once_flag;
  468. #ifndef _LIBCPP_CXX03_LANG
  469. template<class _Callable, class... _Args>
  470. _LIBCPP_INLINE_VISIBILITY
  471. void call_once(once_flag&, _Callable&&, _Args&&...);
  472. #else // _LIBCPP_CXX03_LANG
  473. template<class _Callable>
  474. _LIBCPP_INLINE_VISIBILITY
  475. void call_once(once_flag&, _Callable&);
  476. template<class _Callable>
  477. _LIBCPP_INLINE_VISIBILITY
  478. void call_once(once_flag&, const _Callable&);
  479. #endif // _LIBCPP_CXX03_LANG
  480. struct _LIBCPP_TEMPLATE_VIS once_flag
  481. {
  482. _LIBCPP_INLINE_VISIBILITY
  483. _LIBCPP_CONSTEXPR
  484. once_flag() _NOEXCEPT : __state_(0) {}
  485. once_flag(const once_flag&) = delete;
  486. once_flag& operator=(const once_flag&) = delete;
  487. #if defined(_LIBCPP_ABI_MICROSOFT)
  488. typedef uintptr_t _State_type;
  489. #else
  490. typedef unsigned long _State_type;
  491. #endif
  492. private:
  493. #if defined(_LIBCPP_ABI_MICROSOFT)
  494. atomic<_State_type> __state_;
  495. #else
  496. _State_type __state_;
  497. #endif
  498. #ifndef _LIBCPP_CXX03_LANG
  499. template<class _Callable, class... _Args>
  500. friend
  501. void call_once(once_flag&, _Callable&&, _Args&&...);
  502. #else // _LIBCPP_CXX03_LANG
  503. template<class _Callable>
  504. friend
  505. void call_once(once_flag&, _Callable&);
  506. template<class _Callable>
  507. friend
  508. void call_once(once_flag&, const _Callable&);
  509. #endif // _LIBCPP_CXX03_LANG
  510. };
  511. #ifndef _LIBCPP_CXX03_LANG
  512. template <class _Fp>
  513. class __call_once_param
  514. {
  515. _Fp& __f_;
  516. public:
  517. _LIBCPP_INLINE_VISIBILITY
  518. explicit __call_once_param(_Fp& __f) : __f_(__f) {}
  519. _LIBCPP_INLINE_VISIBILITY
  520. void operator()()
  521. {
  522. typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
  523. __execute(_Index());
  524. }
  525. private:
  526. template <size_t ..._Indices>
  527. _LIBCPP_INLINE_VISIBILITY
  528. void __execute(__tuple_indices<_Indices...>)
  529. {
  530. _VSTD::__invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
  531. }
  532. };
  533. #else
  534. template <class _Fp>
  535. class __call_once_param
  536. {
  537. _Fp& __f_;
  538. public:
  539. _LIBCPP_INLINE_VISIBILITY
  540. explicit __call_once_param(_Fp& __f) : __f_(__f) {}
  541. _LIBCPP_INLINE_VISIBILITY
  542. void operator()()
  543. {
  544. __f_();
  545. }
  546. };
  547. #endif
  548. template <class _Fp>
  549. void _LIBCPP_INLINE_VISIBILITY
  550. __call_once_proxy(void* __vp)
  551. {
  552. __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
  553. (*__p)();
  554. }
  555. #ifdef _LIBCPP_ABI_MICROSOFT
  556. _LIBCPP_FUNC_VIS void __call_once(volatile atomic<once_flag::_State_type>&, void*,
  557. void (*)(void*));
  558. #else
  559. _LIBCPP_FUNC_VIS void __call_once(volatile once_flag::_State_type&, void*,
  560. void (*)(void*));
  561. #endif
  562. #ifndef _LIBCPP_CXX03_LANG
  563. template<class _Callable, class... _Args>
  564. inline _LIBCPP_INLINE_VISIBILITY
  565. void
  566. call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
  567. {
  568. #if defined(_LIBCPP_ABI_MICROSOFT)
  569. if (__flag.__state_.load(memory_order_acquire) != ~once_flag::_State_type(0))
  570. #else
  571. if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0))
  572. #endif
  573. {
  574. typedef tuple<_Callable&&, _Args&&...> _Gp;
  575. _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
  576. __call_once_param<_Gp> __p(__f);
  577. __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
  578. }
  579. }
  580. #else // _LIBCPP_CXX03_LANG
  581. template<class _Callable>
  582. inline _LIBCPP_INLINE_VISIBILITY
  583. void
  584. call_once(once_flag& __flag, _Callable& __func)
  585. {
  586. #if defined(_LIBCPP_ABI_MICROSOFT)
  587. if (__flag.__state_.load(memory_order_acquire) != ~once_flag::_State_type(0))
  588. #else
  589. if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0))
  590. #endif
  591. {
  592. __call_once_param<_Callable> __p(__func);
  593. __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
  594. }
  595. }
  596. template<class _Callable>
  597. inline _LIBCPP_INLINE_VISIBILITY
  598. void
  599. call_once(once_flag& __flag, const _Callable& __func)
  600. {
  601. #if defined(_LIBCPP_ABI_MICROSOFT)
  602. if (__flag.__state_.load(memory_order_relaxed) != ~once_flag::_State_type(0))
  603. #else
  604. if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0))
  605. #endif
  606. {
  607. __call_once_param<const _Callable> __p(__func);
  608. __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
  609. }
  610. }
  611. #endif // _LIBCPP_CXX03_LANG
  612. _LIBCPP_END_NAMESPACE_STD
  613. _LIBCPP_POP_MACROS
  614. #endif // _LIBCPP_MUTEX