mutex 18 KB

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