3// Copyright (C) 2003-2022 Free Software Foundation, Inc.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
25/** @file include/mutex
26 * This is a Standard C++ Library header.
30#define _GLIBCXX_MUTEX 1
32#pragma GCC system_header
34#if __cplusplus < 201103L
35# include <bits/c++0x_warning.h>
41#include <system_error>
42#include <bits/chrono.h>
43#include <bits/std_mutex.h>
44#include <bits/unique_lock.h>
45#if ! _GTHREAD_USE_MUTEX_TIMEDLOCK
46# include <condition_variable>
49#include <ext/atomicity.h> // __gnu_cxx::__is_single_threaded
51#if defined _GLIBCXX_HAS_GTHREADS && ! defined _GLIBCXX_HAVE_TLS
52# include <bits/std_function.h> // std::function
55namespace std _GLIBCXX_VISIBILITY(default)
57_GLIBCXX_BEGIN_NAMESPACE_VERSION
64#ifdef _GLIBCXX_HAS_GTHREADS
65 /// @cond undocumented
67 // Common base class for std::recursive_mutex and std::recursive_timed_mutex
68 class __recursive_mutex_base
71 typedef __gthread_recursive_mutex_t __native_type;
73 __recursive_mutex_base(const __recursive_mutex_base&) = delete;
74 __recursive_mutex_base& operator=(const __recursive_mutex_base&) = delete;
76#ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
77 __native_type _M_mutex = __GTHREAD_RECURSIVE_MUTEX_INIT;
79 __recursive_mutex_base() = default;
81 __native_type _M_mutex;
83 __recursive_mutex_base()
85 // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
86 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
89 ~__recursive_mutex_base()
90 { __gthread_recursive_mutex_destroy(&_M_mutex); }
95 /** The standard recursive mutex type.
97 * A recursive mutex can be locked more than once by the same thread.
98 * Other threads cannot lock the mutex until the owning thread unlocks it
99 * as many times as it was locked.
104 class recursive_mutex : private __recursive_mutex_base
107 typedef __native_type* native_handle_type;
109 recursive_mutex() = default;
110 ~recursive_mutex() = default;
112 recursive_mutex(const recursive_mutex&) = delete;
113 recursive_mutex& operator=(const recursive_mutex&) = delete;
118 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
120 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
122 __throw_system_error(__e);
128 // XXX EINVAL, EAGAIN, EBUSY
129 return !__gthread_recursive_mutex_trylock(&_M_mutex);
135 // XXX EINVAL, EAGAIN, EBUSY
136 __gthread_recursive_mutex_unlock(&_M_mutex);
140 native_handle() noexcept
141 { return &_M_mutex; }
144#if _GTHREAD_USE_MUTEX_TIMEDLOCK
145 /// @cond undocumented
147 template<typename _Derived>
148 class __timed_mutex_impl
151 template<typename _Rep, typename _Period>
153 _M_try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
155#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
156 using __clock = chrono::steady_clock;
158 using __clock = chrono::system_clock;
161 auto __rt = chrono::duration_cast<__clock::duration>(__rtime);
162 if (ratio_greater<__clock::period, _Period>())
164 return _M_try_lock_until(__clock::now() + __rt);
167 template<typename _Duration>
169 _M_try_lock_until(const chrono::time_point<chrono::system_clock,
172 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
173 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
175 __gthread_time_t __ts = {
176 static_cast<std::time_t>(__s.time_since_epoch().count()),
177 static_cast<long>(__ns.count())
180 return static_cast<_Derived*>(this)->_M_timedlock(__ts);
183#ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
184 template<typename _Duration>
186 _M_try_lock_until(const chrono::time_point<chrono::steady_clock,
189 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
190 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
192 __gthread_time_t __ts = {
193 static_cast<std::time_t>(__s.time_since_epoch().count()),
194 static_cast<long>(__ns.count())
197 return static_cast<_Derived*>(this)->_M_clocklock(CLOCK_MONOTONIC,
202 template<typename _Clock, typename _Duration>
204 _M_try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
206#if __cplusplus > 201703L
207 static_assert(chrono::is_clock_v<_Clock>);
209 // The user-supplied clock may not tick at the same rate as
210 // steady_clock, so we must loop in order to guarantee that
211 // the timeout has expired before returning false.
212 auto __now = _Clock::now();
214 auto __rtime = __atime - __now;
215 if (_M_try_lock_for(__rtime))
217 __now = _Clock::now();
218 } while (__atime > __now);
224 /** The standard timed mutex type.
226 * A non-recursive mutex that supports a timeout when trying to acquire the
233 : private __mutex_base, public __timed_mutex_impl<timed_mutex>
236 typedef __native_type* native_handle_type;
238 timed_mutex() = default;
239 ~timed_mutex() = default;
241 timed_mutex(const timed_mutex&) = delete;
242 timed_mutex& operator=(const timed_mutex&) = delete;
247 int __e = __gthread_mutex_lock(&_M_mutex);
249 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
251 __throw_system_error(__e);
257 // XXX EINVAL, EAGAIN, EBUSY
258 return !__gthread_mutex_trylock(&_M_mutex);
261 template <class _Rep, class _Period>
263 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
264 { return _M_try_lock_for(__rtime); }
266 template <class _Clock, class _Duration>
268 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
269 { return _M_try_lock_until(__atime); }
274 // XXX EINVAL, EAGAIN, EBUSY
275 __gthread_mutex_unlock(&_M_mutex);
279 native_handle() noexcept
280 { return &_M_mutex; }
283 friend class __timed_mutex_impl<timed_mutex>;
286 _M_timedlock(const __gthread_time_t& __ts)
287 { return !__gthread_mutex_timedlock(&_M_mutex, &__ts); }
289#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
291 _M_clocklock(clockid_t __clockid, const __gthread_time_t& __ts)
292 { return !pthread_mutex_clocklock(&_M_mutex, __clockid, &__ts); }
296 /** The standard recursive timed mutex type.
298 * A recursive mutex that supports a timeout when trying to acquire the
299 * lock. A recursive mutex can be locked more than once by the same thread.
300 * Other threads cannot lock the mutex until the owning thread unlocks it
301 * as many times as it was locked.
306 class recursive_timed_mutex
307 : private __recursive_mutex_base,
308 public __timed_mutex_impl<recursive_timed_mutex>
311 typedef __native_type* native_handle_type;
313 recursive_timed_mutex() = default;
314 ~recursive_timed_mutex() = default;
316 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
317 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
322 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
324 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
326 __throw_system_error(__e);
332 // XXX EINVAL, EAGAIN, EBUSY
333 return !__gthread_recursive_mutex_trylock(&_M_mutex);
336 template <class _Rep, class _Period>
338 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
339 { return _M_try_lock_for(__rtime); }
341 template <class _Clock, class _Duration>
343 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
344 { return _M_try_lock_until(__atime); }
349 // XXX EINVAL, EAGAIN, EBUSY
350 __gthread_recursive_mutex_unlock(&_M_mutex);
354 native_handle() noexcept
355 { return &_M_mutex; }
358 friend class __timed_mutex_impl<recursive_timed_mutex>;
361 _M_timedlock(const __gthread_time_t& __ts)
362 { return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts); }
364#ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
366 _M_clocklock(clockid_t __clockid, const __gthread_time_t& __ts)
367 { return !pthread_mutex_clocklock(&_M_mutex, __clockid, &__ts); }
371#else // !_GTHREAD_USE_MUTEX_TIMEDLOCK
377 condition_variable _M_cv;
378 bool _M_locked = false;
382 timed_mutex() = default;
383 ~timed_mutex() { __glibcxx_assert( !_M_locked ); }
385 timed_mutex(const timed_mutex&) = delete;
386 timed_mutex& operator=(const timed_mutex&) = delete;
391 unique_lock<mutex> __lk(_M_mut);
392 _M_cv.wait(__lk, [&]{ return !_M_locked; });
399 lock_guard<mutex> __lk(_M_mut);
406 template<typename _Rep, typename _Period>
408 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
410 unique_lock<mutex> __lk(_M_mut);
411 if (!_M_cv.wait_for(__lk, __rtime, [&]{ return !_M_locked; }))
417 template<typename _Clock, typename _Duration>
419 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
421 unique_lock<mutex> __lk(_M_mut);
422 if (!_M_cv.wait_until(__lk, __atime, [&]{ return !_M_locked; }))
431 lock_guard<mutex> __lk(_M_mut);
432 __glibcxx_assert( _M_locked );
438 /// recursive_timed_mutex
439 class recursive_timed_mutex
442 condition_variable _M_cv;
444 unsigned _M_count = 0;
446 // Predicate type that tests whether the current thread can lock a mutex.
449 // Returns true if the mutex is unlocked or is locked by _M_caller.
451 operator()() const noexcept
452 { return _M_mx->_M_count == 0 || _M_mx->_M_owner == _M_caller; }
454 const recursive_timed_mutex* _M_mx;
455 thread::id _M_caller;
460 recursive_timed_mutex() = default;
461 ~recursive_timed_mutex() { __glibcxx_assert( _M_count == 0 ); }
463 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
464 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
469 auto __id = this_thread::get_id();
470 _Can_lock __can_lock{this, __id};
471 unique_lock<mutex> __lk(_M_mut);
472 _M_cv.wait(__lk, __can_lock);
474 __throw_system_error(EAGAIN); // [thread.timedmutex.recursive]/3
482 auto __id = this_thread::get_id();
483 _Can_lock __can_lock{this, __id};
484 lock_guard<mutex> __lk(_M_mut);
494 template<typename _Rep, typename _Period>
496 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
498 auto __id = this_thread::get_id();
499 _Can_lock __can_lock{this, __id};
500 unique_lock<mutex> __lk(_M_mut);
501 if (!_M_cv.wait_for(__lk, __rtime, __can_lock))
510 template<typename _Clock, typename _Duration>
512 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
514 auto __id = this_thread::get_id();
515 _Can_lock __can_lock{this, __id};
516 unique_lock<mutex> __lk(_M_mut);
517 if (!_M_cv.wait_until(__lk, __atime, __can_lock))
529 lock_guard<mutex> __lk(_M_mut);
530 __glibcxx_assert( _M_owner == this_thread::get_id() );
531 __glibcxx_assert( _M_count > 0 );
541#endif // _GLIBCXX_HAS_GTHREADS
543 /// @cond undocumented
546 // Lock the last lockable, after all previous ones are locked.
547 template<typename _Lockable>
549 __try_lock_impl(_Lockable& __l)
551 if (unique_lock<_Lockable> __lock{__l, try_to_lock})
560 // Lock each lockable in turn.
561 // Use iteration if all lockables are the same type, recursion otherwise.
562 template<typename _L0, typename... _Lockables>
564 __try_lock_impl(_L0& __l0, _Lockables&... __lockables)
566#if __cplusplus >= 201703L
567 if constexpr ((is_same_v<_L0, _Lockables> && ...))
569 constexpr int _Np = 1 + sizeof...(_Lockables);
570 unique_lock<_L0> __locks[_Np] = {
571 {__l0, defer_lock}, {__lockables, defer_lock}...
573 for (int __i = 0; __i < _Np; ++__i)
575 if (!__locks[__i].try_lock())
577 const int __failed = __i;
579 __locks[__i].unlock();
583 for (auto& __l : __locks)
589 if (unique_lock<_L0> __lock{__l0, try_to_lock})
591 int __idx = __detail::__try_lock_impl(__lockables...);
603 } // namespace __detail
606 /** @brief Generic try_lock.
607 * @param __l1 Meets Lockable requirements (try_lock() may throw).
608 * @param __l2 Meets Lockable requirements (try_lock() may throw).
609 * @param __l3 Meets Lockable requirements (try_lock() may throw).
610 * @return Returns -1 if all try_lock() calls return true. Otherwise returns
611 * a 0-based index corresponding to the argument that returned false.
612 * @post Either all arguments are locked, or none will be.
614 * Sequentially calls try_lock() on each argument.
616 template<typename _L1, typename _L2, typename... _L3>
618 try_lock(_L1& __l1, _L2& __l2, _L3&... __l3)
620 return __detail::__try_lock_impl(__l1, __l2, __l3...);
623 /// @cond undocumented
626 // This function can recurse up to N levels deep, for N = 1+sizeof...(L1).
627 // On each recursion the lockables are rotated left one position,
628 // e.g. depth 0: l0, l1, l2; depth 1: l1, l2, l0; depth 2: l2, l0, l1.
629 // When a call to l_i.try_lock() fails it recurses/returns to depth=i
630 // so that l_i is the first argument, and then blocks until l_i is locked.
631 template<typename _L0, typename... _L1>
633 __lock_impl(int& __i, int __depth, _L0& __l0, _L1&... __l1)
635 while (__i >= __depth)
639 int __failed = 1; // index that couldn't be locked
641 unique_lock<_L0> __first(__l0);
642 __failed += __detail::__try_lock_impl(__l1...);
645 __i = -1; // finished
650#if defined _GLIBCXX_HAS_GTHREADS && defined _GLIBCXX_USE_SCHED_YIELD
653 constexpr auto __n = 1 + sizeof...(_L1);
654 __i = (__depth + __failed) % __n;
656 else // rotate left until l_i is first.
657 __detail::__lock_impl(__i, __depth + 1, __l1..., __l0);
661 } // namespace __detail
664 /** @brief Generic lock.
665 * @param __l1 Meets Lockable requirements (try_lock() may throw).
666 * @param __l2 Meets Lockable requirements (try_lock() may throw).
667 * @param __l3 Meets Lockable requirements (try_lock() may throw).
668 * @throw An exception thrown by an argument's lock() or try_lock() member.
669 * @post All arguments are locked.
671 * All arguments are locked via a sequence of calls to lock(), try_lock()
672 * and unlock(). If this function exits via an exception any locks that
673 * were obtained will be released.
675 template<typename _L1, typename _L2, typename... _L3>
677 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
679#if __cplusplus >= 201703L
680 if constexpr (is_same_v<_L1, _L2> && (is_same_v<_L1, _L3> && ...))
682 constexpr int _Np = 2 + sizeof...(_L3);
683 unique_lock<_L1> __locks[] = {
684 {__l1, defer_lock}, {__l2, defer_lock}, {__l3, defer_lock}...
688 __locks[__first].lock();
689 for (int __j = 1; __j < _Np; ++__j)
691 const int __idx = (__first + __j) % _Np;
692 if (!__locks[__idx].try_lock())
694 for (int __k = __j; __k != 0; --__k)
695 __locks[(__first + __k - 1) % _Np].unlock();
700 } while (!__locks[__first].owns_lock());
702 for (auto& __l : __locks)
709 __detail::__lock_impl(__i, 0, __l1, __l2, __l3...);
713#if __cplusplus >= 201703L
714#define __cpp_lib_scoped_lock 201703L
715 /** @brief A scoped lock type for multiple lockable objects.
717 * A scoped_lock controls mutex ownership within a scope, releasing
718 * ownership in the destructor.
723 template<typename... _MutexTypes>
727 explicit scoped_lock(_MutexTypes&... __m) : _M_devices(std::tie(__m...))
728 { std::lock(__m...); }
730 explicit scoped_lock(adopt_lock_t, _MutexTypes&... __m) noexcept
731 : _M_devices(std::tie(__m...))
732 { } // calling thread owns mutex
735 { std::apply([](auto&... __m) { (__m.unlock(), ...); }, _M_devices); }
737 scoped_lock(const scoped_lock&) = delete;
738 scoped_lock& operator=(const scoped_lock&) = delete;
741 tuple<_MutexTypes&...> _M_devices;
748 explicit scoped_lock() = default;
749 explicit scoped_lock(adopt_lock_t) noexcept { }
750 ~scoped_lock() = default;
752 scoped_lock(const scoped_lock&) = delete;
753 scoped_lock& operator=(const scoped_lock&) = delete;
756 template<typename _Mutex>
757 class scoped_lock<_Mutex>
760 using mutex_type = _Mutex;
762 explicit scoped_lock(mutex_type& __m) : _M_device(__m)
763 { _M_device.lock(); }
765 explicit scoped_lock(adopt_lock_t, mutex_type& __m) noexcept
767 { } // calling thread owns mutex
770 { _M_device.unlock(); }
772 scoped_lock(const scoped_lock&) = delete;
773 scoped_lock& operator=(const scoped_lock&) = delete;
776 mutex_type& _M_device;
780#ifdef _GLIBCXX_HAS_GTHREADS
781 /// Flag type used by std::call_once
784 constexpr once_flag() noexcept = default;
786 /// Deleted copy constructor
787 once_flag(const once_flag&) = delete;
788 /// Deleted assignment operator
789 once_flag& operator=(const once_flag&) = delete;
792 // For gthreads targets a pthread_once_t is used with pthread_once, but
793 // for most targets this doesn't work correctly for exceptional executions.
794 __gthread_once_t _M_once = __GTHREAD_ONCE_INIT;
796 struct _Prepare_execution;
798 template<typename _Callable, typename... _Args>
800 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
803 /// @cond undocumented
804# ifdef _GLIBCXX_HAVE_TLS
805 // If TLS is available use thread-local state for the type-erased callable
806 // that is being run by std::call_once in the current thread.
807 extern __thread void* __once_callable;
808 extern __thread void (*__once_call)();
810 // RAII type to set up state for pthread_once call.
811 struct once_flag::_Prepare_execution
813 template<typename _Callable>
815 _Prepare_execution(_Callable& __c)
817 // Store address in thread-local pointer:
818 __once_callable = std::__addressof(__c);
819 // Trampoline function to invoke the closure via thread-local pointer:
820 __once_call = [] { (*static_cast<_Callable*>(__once_callable))(); };
823 ~_Prepare_execution()
825 // PR libstdc++/82481
826 __once_callable = nullptr;
827 __once_call = nullptr;
830 _Prepare_execution(const _Prepare_execution&) = delete;
831 _Prepare_execution& operator=(const _Prepare_execution&) = delete;
835 // Without TLS use a global std::mutex and store the callable in a
836 // global std::function.
837 extern function<void()> __once_functor;
840 __set_once_functor_lock_ptr(unique_lock<mutex>*);
845 // RAII type to set up state for pthread_once call.
846 struct once_flag::_Prepare_execution
848 template<typename _Callable>
850 _Prepare_execution(_Callable& __c)
852 // Store the callable in the global std::function
853 __once_functor = __c;
854 __set_once_functor_lock_ptr(&_M_functor_lock);
857 ~_Prepare_execution()
860 __set_once_functor_lock_ptr(nullptr);
864 // XXX This deadlocks if used recursively (PR 97949)
865 unique_lock<mutex> _M_functor_lock{__get_once_mutex()};
867 _Prepare_execution(const _Prepare_execution&) = delete;
868 _Prepare_execution& operator=(const _Prepare_execution&) = delete;
873 // This function is passed to pthread_once by std::call_once.
874 // It runs __once_call() or __once_functor().
875 extern "C" void __once_proxy(void);
877 /// Invoke a callable and synchronize with other calls using the same flag
878 template<typename _Callable, typename... _Args>
880 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
882 // Closure type that runs the function
883 auto __callable = [&] {
884 std::__invoke(std::forward<_Callable>(__f),
885 std::forward<_Args>(__args)...);
888 once_flag::_Prepare_execution __exec(__callable);
890 // XXX pthread_once does not reset the flag if an exception is thrown.
891 if (int __e = __gthread_once(&__once._M_once, &__once_proxy))
892 __throw_system_error(__e);
895#else // _GLIBCXX_HAS_GTHREADS
897 /// Flag type used by std::call_once
900 constexpr once_flag() noexcept = default;
902 /// Deleted copy constructor
903 once_flag(const once_flag&) = delete;
904 /// Deleted assignment operator
905 once_flag& operator=(const once_flag&) = delete;
908 // There are two different std::once_flag interfaces, abstracting four
909 // different implementations.
910 // The single-threaded interface uses the _M_activate() and _M_finish(bool)
911 // functions, which start and finish an active execution respectively.
912 // See [thread.once.callonce] in C++11 for the definition of
913 // active/passive/returning/exceptional executions.
914 enum _Bits : int { _Init = 0, _Active = 1, _Done = 2 };
916 int _M_once = _Bits::_Init;
918 // Check to see if all executions will be passive now.
920 _M_passive() const noexcept;
922 // Attempts to begin an active execution.
925 // Must be called to complete an active execution.
926 // The argument is true if the active execution was a returning execution,
927 // false if it was an exceptional execution.
928 void _M_finish(bool __returning) noexcept;
930 // RAII helper to call _M_finish.
931 struct _Active_execution
933 explicit _Active_execution(once_flag& __flag) : _M_flag(__flag) { }
935 ~_Active_execution() { _M_flag._M_finish(_M_returning); }
937 _Active_execution(const _Active_execution&) = delete;
938 _Active_execution& operator=(const _Active_execution&) = delete;
941 bool _M_returning = false;
944 template<typename _Callable, typename... _Args>
946 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
949 // Inline definitions of std::once_flag members for single-threaded targets.
952 once_flag::_M_passive() const noexcept
953 { return _M_once == _Bits::_Done; }
956 once_flag::_M_activate()
958 if (_M_once == _Bits::_Init) [[__likely__]]
960 _M_once = _Bits::_Active;
963 else if (_M_passive()) // Caller should have checked this already.
966 __throw_system_error(EDEADLK);
970 once_flag::_M_finish(bool __returning) noexcept
971 { _M_once = __returning ? _Bits::_Done : _Bits::_Init; }
973 /// Invoke a callable and synchronize with other calls using the same flag
974 template<typename _Callable, typename... _Args>
976 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
978 if (__once._M_passive())
980 else if (__once._M_activate())
982 once_flag::_Active_execution __exec(__once);
984 // _GLIBCXX_RESOLVE_LIB_DEFECTS
985 // 2442. call_once() shouldn't DECAY_COPY()
986 std::__invoke(std::forward<_Callable>(__f),
987 std::forward<_Args>(__args)...);
989 // __f(__args...) did not throw
990 __exec._M_returning = true;
993#endif // _GLIBCXX_HAS_GTHREADS
996_GLIBCXX_END_NAMESPACE_VERSION
1001#endif // _GLIBCXX_MUTEX