mutex 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. // <mutex> -*- C++ -*-
  2. // Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/mutex
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_MUTEX
  24. #define _GLIBCXX_MUTEX 1
  25. #pragma GCC system_header
  26. #if __cplusplus < 201103L
  27. # include <bits/c++0x_warning.h>
  28. #else
  29. #include <tuple>
  30. #include <chrono>
  31. #include <exception>
  32. #include <type_traits>
  33. #include <functional>
  34. #include <system_error>
  35. #include <bits/functexcept.h>
  36. #include <bits/gthr.h>
  37. #include <bits/move.h> // for std::swap
  38. #include <bits/cxxabi_forced.h>
  39. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  40. namespace std _GLIBCXX_VISIBILITY(default)
  41. {
  42. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  43. #ifdef _GLIBCXX_HAS_GTHREADS
  44. // Common base class for std::mutex and std::timed_mutex
  45. class __mutex_base
  46. {
  47. protected:
  48. typedef __gthread_mutex_t __native_type;
  49. #ifdef __GTHREAD_MUTEX_INIT
  50. __native_type _M_mutex = __GTHREAD_MUTEX_INIT;
  51. constexpr __mutex_base() noexcept = default;
  52. #else
  53. __native_type _M_mutex;
  54. __mutex_base() noexcept
  55. {
  56. // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
  57. __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
  58. }
  59. ~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); }
  60. #endif
  61. __mutex_base(const __mutex_base&) = delete;
  62. __mutex_base& operator=(const __mutex_base&) = delete;
  63. };
  64. // Common base class for std::recursive_mutex and std::recursive_timed_mutex
  65. class __recursive_mutex_base
  66. {
  67. protected:
  68. typedef __gthread_recursive_mutex_t __native_type;
  69. __recursive_mutex_base(const __recursive_mutex_base&) = delete;
  70. __recursive_mutex_base& operator=(const __recursive_mutex_base&) = delete;
  71. #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
  72. __native_type _M_mutex = __GTHREAD_RECURSIVE_MUTEX_INIT;
  73. __recursive_mutex_base() = default;
  74. #else
  75. __native_type _M_mutex;
  76. __recursive_mutex_base()
  77. {
  78. // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
  79. __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
  80. }
  81. ~__recursive_mutex_base()
  82. { __gthread_recursive_mutex_destroy(&_M_mutex); }
  83. #endif
  84. };
  85. /**
  86. * @defgroup mutexes Mutexes
  87. * @ingroup concurrency
  88. *
  89. * Classes for mutex support.
  90. * @{
  91. */
  92. /// mutex
  93. class mutex : private __mutex_base
  94. {
  95. public:
  96. typedef __native_type* native_handle_type;
  97. #ifdef __GTHREAD_MUTEX_INIT
  98. constexpr
  99. #endif
  100. mutex() noexcept = default;
  101. ~mutex() = default;
  102. mutex(const mutex&) = delete;
  103. mutex& operator=(const mutex&) = delete;
  104. void
  105. lock()
  106. {
  107. int __e = __gthread_mutex_lock(&_M_mutex);
  108. // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
  109. if (__e)
  110. __throw_system_error(__e);
  111. }
  112. bool
  113. try_lock() noexcept
  114. {
  115. // XXX EINVAL, EAGAIN, EBUSY
  116. return !__gthread_mutex_trylock(&_M_mutex);
  117. }
  118. void
  119. unlock()
  120. {
  121. // XXX EINVAL, EAGAIN, EPERM
  122. __gthread_mutex_unlock(&_M_mutex);
  123. }
  124. native_handle_type
  125. native_handle()
  126. { return &_M_mutex; }
  127. };
  128. /// recursive_mutex
  129. class recursive_mutex : private __recursive_mutex_base
  130. {
  131. public:
  132. typedef __native_type* native_handle_type;
  133. recursive_mutex() = default;
  134. ~recursive_mutex() = default;
  135. recursive_mutex(const recursive_mutex&) = delete;
  136. recursive_mutex& operator=(const recursive_mutex&) = delete;
  137. void
  138. lock()
  139. {
  140. int __e = __gthread_recursive_mutex_lock(&_M_mutex);
  141. // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
  142. if (__e)
  143. __throw_system_error(__e);
  144. }
  145. bool
  146. try_lock() noexcept
  147. {
  148. // XXX EINVAL, EAGAIN, EBUSY
  149. return !__gthread_recursive_mutex_trylock(&_M_mutex);
  150. }
  151. void
  152. unlock()
  153. {
  154. // XXX EINVAL, EAGAIN, EBUSY
  155. __gthread_recursive_mutex_unlock(&_M_mutex);
  156. }
  157. native_handle_type
  158. native_handle()
  159. { return &_M_mutex; }
  160. };
  161. #if _GTHREAD_USE_MUTEX_TIMEDLOCK
  162. template<typename _Derived>
  163. class __timed_mutex_impl
  164. {
  165. protected:
  166. typedef chrono::high_resolution_clock __clock_t;
  167. template<typename _Rep, typename _Period>
  168. bool
  169. _M_try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
  170. {
  171. using chrono::steady_clock;
  172. auto __rt = chrono::duration_cast<steady_clock::duration>(__rtime);
  173. if (ratio_greater<steady_clock::period, _Period>())
  174. ++__rt;
  175. return _M_try_lock_until(steady_clock::now() + __rt);
  176. }
  177. template<typename _Duration>
  178. bool
  179. _M_try_lock_until(const chrono::time_point<__clock_t,
  180. _Duration>& __atime)
  181. {
  182. auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
  183. auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
  184. __gthread_time_t __ts = {
  185. static_cast<std::time_t>(__s.time_since_epoch().count()),
  186. static_cast<long>(__ns.count())
  187. };
  188. auto __mutex = static_cast<_Derived*>(this)->native_handle();
  189. return !__gthread_mutex_timedlock(__mutex, &__ts);
  190. }
  191. template<typename _Clock, typename _Duration>
  192. bool
  193. _M_try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
  194. {
  195. auto __rtime = __atime - _Clock::now();
  196. return _M_try_lock_until(__clock_t::now() + __rtime);
  197. }
  198. };
  199. /// timed_mutex
  200. class timed_mutex
  201. : private __mutex_base, public __timed_mutex_impl<timed_mutex>
  202. {
  203. public:
  204. typedef __native_type* native_handle_type;
  205. timed_mutex() = default;
  206. ~timed_mutex() = default;
  207. timed_mutex(const timed_mutex&) = delete;
  208. timed_mutex& operator=(const timed_mutex&) = delete;
  209. void
  210. lock()
  211. {
  212. int __e = __gthread_mutex_lock(&_M_mutex);
  213. // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
  214. if (__e)
  215. __throw_system_error(__e);
  216. }
  217. bool
  218. try_lock() noexcept
  219. {
  220. // XXX EINVAL, EAGAIN, EBUSY
  221. return !__gthread_mutex_trylock(&_M_mutex);
  222. }
  223. template <class _Rep, class _Period>
  224. bool
  225. try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
  226. { return _M_try_lock_for(__rtime); }
  227. template <class _Clock, class _Duration>
  228. bool
  229. try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
  230. { return _M_try_lock_until(__atime); }
  231. void
  232. unlock()
  233. {
  234. // XXX EINVAL, EAGAIN, EBUSY
  235. __gthread_mutex_unlock(&_M_mutex);
  236. }
  237. native_handle_type
  238. native_handle()
  239. { return &_M_mutex; }
  240. };
  241. /// recursive_timed_mutex
  242. class recursive_timed_mutex
  243. : private __recursive_mutex_base,
  244. public __timed_mutex_impl<recursive_timed_mutex>
  245. {
  246. public:
  247. typedef __native_type* native_handle_type;
  248. recursive_timed_mutex() = default;
  249. ~recursive_timed_mutex() = default;
  250. recursive_timed_mutex(const recursive_timed_mutex&) = delete;
  251. recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
  252. void
  253. lock()
  254. {
  255. int __e = __gthread_recursive_mutex_lock(&_M_mutex);
  256. // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
  257. if (__e)
  258. __throw_system_error(__e);
  259. }
  260. bool
  261. try_lock() noexcept
  262. {
  263. // XXX EINVAL, EAGAIN, EBUSY
  264. return !__gthread_recursive_mutex_trylock(&_M_mutex);
  265. }
  266. template <class _Rep, class _Period>
  267. bool
  268. try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
  269. { return _M_try_lock_for(__rtime); }
  270. template <class _Clock, class _Duration>
  271. bool
  272. try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
  273. { return _M_try_lock_until(__atime); }
  274. void
  275. unlock()
  276. {
  277. // XXX EINVAL, EAGAIN, EBUSY
  278. __gthread_recursive_mutex_unlock(&_M_mutex);
  279. }
  280. native_handle_type
  281. native_handle()
  282. { return &_M_mutex; }
  283. };
  284. #endif
  285. #endif // _GLIBCXX_HAS_GTHREADS
  286. /// Do not acquire ownership of the mutex.
  287. struct defer_lock_t { };
  288. /// Try to acquire ownership of the mutex without blocking.
  289. struct try_to_lock_t { };
  290. /// Assume the calling thread has already obtained mutex ownership
  291. /// and manage it.
  292. struct adopt_lock_t { };
  293. constexpr defer_lock_t defer_lock { };
  294. constexpr try_to_lock_t try_to_lock { };
  295. constexpr adopt_lock_t adopt_lock { };
  296. /// @brief Scoped lock idiom.
  297. // Acquire the mutex here with a constructor call, then release with
  298. // the destructor call in accordance with RAII style.
  299. template<typename _Mutex>
  300. class lock_guard
  301. {
  302. public:
  303. typedef _Mutex mutex_type;
  304. explicit lock_guard(mutex_type& __m) : _M_device(__m)
  305. { _M_device.lock(); }
  306. lock_guard(mutex_type& __m, adopt_lock_t) : _M_device(__m)
  307. { } // calling thread owns mutex
  308. ~lock_guard()
  309. { _M_device.unlock(); }
  310. lock_guard(const lock_guard&) = delete;
  311. lock_guard& operator=(const lock_guard&) = delete;
  312. private:
  313. mutex_type& _M_device;
  314. };
  315. /// unique_lock
  316. template<typename _Mutex>
  317. class unique_lock
  318. {
  319. public:
  320. typedef _Mutex mutex_type;
  321. unique_lock() noexcept
  322. : _M_device(0), _M_owns(false)
  323. { }
  324. explicit unique_lock(mutex_type& __m)
  325. : _M_device(std::__addressof(__m)), _M_owns(false)
  326. {
  327. lock();
  328. _M_owns = true;
  329. }
  330. unique_lock(mutex_type& __m, defer_lock_t) noexcept
  331. : _M_device(std::__addressof(__m)), _M_owns(false)
  332. { }
  333. unique_lock(mutex_type& __m, try_to_lock_t)
  334. : _M_device(std::__addressof(__m)), _M_owns(_M_device->try_lock())
  335. { }
  336. unique_lock(mutex_type& __m, adopt_lock_t)
  337. : _M_device(std::__addressof(__m)), _M_owns(true)
  338. {
  339. // XXX calling thread owns mutex
  340. }
  341. template<typename _Clock, typename _Duration>
  342. unique_lock(mutex_type& __m,
  343. const chrono::time_point<_Clock, _Duration>& __atime)
  344. : _M_device(std::__addressof(__m)),
  345. _M_owns(_M_device->try_lock_until(__atime))
  346. { }
  347. template<typename _Rep, typename _Period>
  348. unique_lock(mutex_type& __m,
  349. const chrono::duration<_Rep, _Period>& __rtime)
  350. : _M_device(std::__addressof(__m)),
  351. _M_owns(_M_device->try_lock_for(__rtime))
  352. { }
  353. ~unique_lock()
  354. {
  355. if (_M_owns)
  356. unlock();
  357. }
  358. unique_lock(const unique_lock&) = delete;
  359. unique_lock& operator=(const unique_lock&) = delete;
  360. unique_lock(unique_lock&& __u) noexcept
  361. : _M_device(__u._M_device), _M_owns(__u._M_owns)
  362. {
  363. __u._M_device = 0;
  364. __u._M_owns = false;
  365. }
  366. unique_lock& operator=(unique_lock&& __u) noexcept
  367. {
  368. if(_M_owns)
  369. unlock();
  370. unique_lock(std::move(__u)).swap(*this);
  371. __u._M_device = 0;
  372. __u._M_owns = false;
  373. return *this;
  374. }
  375. void
  376. lock()
  377. {
  378. if (!_M_device)
  379. __throw_system_error(int(errc::operation_not_permitted));
  380. else if (_M_owns)
  381. __throw_system_error(int(errc::resource_deadlock_would_occur));
  382. else
  383. {
  384. _M_device->lock();
  385. _M_owns = true;
  386. }
  387. }
  388. bool
  389. try_lock()
  390. {
  391. if (!_M_device)
  392. __throw_system_error(int(errc::operation_not_permitted));
  393. else if (_M_owns)
  394. __throw_system_error(int(errc::resource_deadlock_would_occur));
  395. else
  396. {
  397. _M_owns = _M_device->try_lock();
  398. return _M_owns;
  399. }
  400. }
  401. template<typename _Clock, typename _Duration>
  402. bool
  403. try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
  404. {
  405. if (!_M_device)
  406. __throw_system_error(int(errc::operation_not_permitted));
  407. else if (_M_owns)
  408. __throw_system_error(int(errc::resource_deadlock_would_occur));
  409. else
  410. {
  411. _M_owns = _M_device->try_lock_until(__atime);
  412. return _M_owns;
  413. }
  414. }
  415. template<typename _Rep, typename _Period>
  416. bool
  417. try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
  418. {
  419. if (!_M_device)
  420. __throw_system_error(int(errc::operation_not_permitted));
  421. else if (_M_owns)
  422. __throw_system_error(int(errc::resource_deadlock_would_occur));
  423. else
  424. {
  425. _M_owns = _M_device->try_lock_for(__rtime);
  426. return _M_owns;
  427. }
  428. }
  429. void
  430. unlock()
  431. {
  432. if (!_M_owns)
  433. __throw_system_error(int(errc::operation_not_permitted));
  434. else if (_M_device)
  435. {
  436. _M_device->unlock();
  437. _M_owns = false;
  438. }
  439. }
  440. void
  441. swap(unique_lock& __u) noexcept
  442. {
  443. std::swap(_M_device, __u._M_device);
  444. std::swap(_M_owns, __u._M_owns);
  445. }
  446. mutex_type*
  447. release() noexcept
  448. {
  449. mutex_type* __ret = _M_device;
  450. _M_device = 0;
  451. _M_owns = false;
  452. return __ret;
  453. }
  454. bool
  455. owns_lock() const noexcept
  456. { return _M_owns; }
  457. explicit operator bool() const noexcept
  458. { return owns_lock(); }
  459. mutex_type*
  460. mutex() const noexcept
  461. { return _M_device; }
  462. private:
  463. mutex_type* _M_device;
  464. bool _M_owns; // XXX use atomic_bool
  465. };
  466. /// Swap overload for unique_lock objects.
  467. template<typename _Mutex>
  468. inline void
  469. swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) noexcept
  470. { __x.swap(__y); }
  471. template<typename _Lock>
  472. inline unique_lock<_Lock>
  473. __try_to_lock(_Lock& __l)
  474. { return unique_lock<_Lock>{__l, try_to_lock}; }
  475. template<int _Idx, bool _Continue = true>
  476. struct __try_lock_impl
  477. {
  478. template<typename... _Lock>
  479. static void
  480. __do_try_lock(tuple<_Lock&...>& __locks, int& __idx)
  481. {
  482. __idx = _Idx;
  483. auto __lock = std::__try_to_lock(std::get<_Idx>(__locks));
  484. if (__lock.owns_lock())
  485. {
  486. constexpr bool __cont = _Idx + 2 < sizeof...(_Lock);
  487. using __try_locker = __try_lock_impl<_Idx + 1, __cont>;
  488. __try_locker::__do_try_lock(__locks, __idx);
  489. if (__idx == -1)
  490. __lock.release();
  491. }
  492. }
  493. };
  494. template<int _Idx>
  495. struct __try_lock_impl<_Idx, false>
  496. {
  497. template<typename... _Lock>
  498. static void
  499. __do_try_lock(tuple<_Lock&...>& __locks, int& __idx)
  500. {
  501. __idx = _Idx;
  502. auto __lock = std::__try_to_lock(std::get<_Idx>(__locks));
  503. if (__lock.owns_lock())
  504. {
  505. __idx = -1;
  506. __lock.release();
  507. }
  508. }
  509. };
  510. /** @brief Generic try_lock.
  511. * @param __l1 Meets Mutex requirements (try_lock() may throw).
  512. * @param __l2 Meets Mutex requirements (try_lock() may throw).
  513. * @param __l3 Meets Mutex requirements (try_lock() may throw).
  514. * @return Returns -1 if all try_lock() calls return true. Otherwise returns
  515. * a 0-based index corresponding to the argument that returned false.
  516. * @post Either all arguments are locked, or none will be.
  517. *
  518. * Sequentially calls try_lock() on each argument.
  519. */
  520. template<typename _Lock1, typename _Lock2, typename... _Lock3>
  521. int
  522. try_lock(_Lock1& __l1, _Lock2& __l2, _Lock3&... __l3)
  523. {
  524. int __idx;
  525. auto __locks = std::tie(__l1, __l2, __l3...);
  526. __try_lock_impl<0>::__do_try_lock(__locks, __idx);
  527. return __idx;
  528. }
  529. /** @brief Generic lock.
  530. * @param __l1 Meets Mutex requirements (try_lock() may throw).
  531. * @param __l2 Meets Mutex requirements (try_lock() may throw).
  532. * @param __l3 Meets Mutex requirements (try_lock() may throw).
  533. * @throw An exception thrown by an argument's lock() or try_lock() member.
  534. * @post All arguments are locked.
  535. *
  536. * All arguments are locked via a sequence of calls to lock(), try_lock()
  537. * and unlock(). If the call exits via an exception any locks that were
  538. * obtained will be released.
  539. */
  540. template<typename _L1, typename _L2, typename... _L3>
  541. void
  542. lock(_L1& __l1, _L2& __l2, _L3&... __l3)
  543. {
  544. while (true)
  545. {
  546. using __try_locker = __try_lock_impl<0, sizeof...(_L3) != 0>;
  547. unique_lock<_L1> __first(__l1);
  548. int __idx;
  549. auto __locks = std::tie(__l2, __l3...);
  550. __try_locker::__do_try_lock(__locks, __idx);
  551. if (__idx == -1)
  552. {
  553. __first.release();
  554. return;
  555. }
  556. }
  557. }
  558. #ifdef _GLIBCXX_HAS_GTHREADS
  559. /// once_flag
  560. struct once_flag
  561. {
  562. private:
  563. typedef __gthread_once_t __native_type;
  564. __native_type _M_once = __GTHREAD_ONCE_INIT;
  565. public:
  566. /// Constructor
  567. constexpr once_flag() noexcept = default;
  568. /// Deleted copy constructor
  569. once_flag(const once_flag&) = delete;
  570. /// Deleted assignment operator
  571. once_flag& operator=(const once_flag&) = delete;
  572. template<typename _Callable, typename... _Args>
  573. friend void
  574. call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
  575. };
  576. #ifdef _GLIBCXX_HAVE_TLS
  577. extern __thread void* __once_callable;
  578. extern __thread void (*__once_call)();
  579. template<typename _Callable>
  580. inline void
  581. __once_call_impl()
  582. {
  583. (*(_Callable*)__once_callable)();
  584. }
  585. #else
  586. extern function<void()> __once_functor;
  587. extern void
  588. __set_once_functor_lock_ptr(unique_lock<mutex>*);
  589. extern mutex&
  590. __get_once_mutex();
  591. #endif
  592. extern "C" void __once_proxy(void);
  593. /// call_once
  594. template<typename _Callable, typename... _Args>
  595. void
  596. call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
  597. {
  598. #ifdef _GLIBCXX_HAVE_TLS
  599. auto __bound_functor = std::__bind_simple(std::forward<_Callable>(__f),
  600. std::forward<_Args>(__args)...);
  601. __once_callable = std::__addressof(__bound_functor);
  602. __once_call = &__once_call_impl<decltype(__bound_functor)>;
  603. #else
  604. unique_lock<mutex> __functor_lock(__get_once_mutex());
  605. auto __callable = std::__bind_simple(std::forward<_Callable>(__f),
  606. std::forward<_Args>(__args)...);
  607. __once_functor = [&]() { __callable(); };
  608. __set_once_functor_lock_ptr(&__functor_lock);
  609. #endif
  610. int __e = __gthread_once(&__once._M_once, &__once_proxy);
  611. #ifndef _GLIBCXX_HAVE_TLS
  612. if (__functor_lock)
  613. __set_once_functor_lock_ptr(0);
  614. #endif
  615. if (__e)
  616. __throw_system_error(__e);
  617. }
  618. #endif // _GLIBCXX_HAS_GTHREADS
  619. // @} group mutexes
  620. _GLIBCXX_END_NAMESPACE_VERSION
  621. } // namespace
  622. #endif // _GLIBCXX_USE_C99_STDINT_TR1
  623. #endif // C++11
  624. #endif // _GLIBCXX_MUTEX