mutex 17 KB

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