3// Copyright (C) 2003-2025 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
33#pragma GCC system_header
36#include <bits/requires_hosted.h> // concurrency
38#if __cplusplus < 201103L
39# include <bits/c++0x_warning.h>
42#include <tuple> // std::tuple
43#include <type_traits> // is_same_v
44#include <errno.h> // EAGAIN, EDEADLK
45#include <bits/chrono.h> // duration, time_point, is_clock_v
46#include <bits/functexcept.h> // __throw_system_error
47#include <bits/invoke.h> // __invoke
48#include <bits/move.h> // std::forward
49#include <bits/std_mutex.h>
50#include <bits/unique_lock.h>
51#if ! _GTHREAD_USE_MUTEX_TIMEDLOCK
52# include <condition_variable>
55#include <ext/atomicity.h> // __gnu_cxx::__is_single_threaded
57#if defined _GLIBCXX_HAS_GTHREADS && ! defined _GLIBCXX_HAVE_TLS
58# include <bits/std_function.h> // std::function
61#define __glibcxx_want_scoped_lock
62#include <bits/version.h>
64namespace std _GLIBCXX_VISIBILITY(default)
66_GLIBCXX_BEGIN_NAMESPACE_VERSION
73#ifdef _GLIBCXX_HAS_GTHREADS
74 /// @cond undocumented
76 // Common base class for std::recursive_mutex and std::recursive_timed_mutex
77 class __recursive_mutex_base
80 typedef __gthread_recursive_mutex_t __native_type;
82 __recursive_mutex_base(const __recursive_mutex_base&) = delete;
83 __recursive_mutex_base& operator=(const __recursive_mutex_base&) = delete;
85#ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
86 __native_type _M_mutex = __GTHREAD_RECURSIVE_MUTEX_INIT;
88 __recursive_mutex_base() = default;
90 __native_type _M_mutex;
92 __recursive_mutex_base()
94 // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
95 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
98 ~__recursive_mutex_base()
99 { __gthread_recursive_mutex_destroy(&_M_mutex); }
104 /** The standard recursive mutex type.
106 * A recursive mutex can be locked more than once by the same thread.
107 * Other threads cannot lock the mutex until the owning thread unlocks it
108 * as many times as it was locked.
113 class recursive_mutex : private __recursive_mutex_base
116 typedef __native_type* native_handle_type;
118 recursive_mutex() = default;
119 ~recursive_mutex() = default;
121 recursive_mutex(const recursive_mutex&) = delete;
122 recursive_mutex& operator=(const recursive_mutex&) = delete;
127 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
129 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
131 __throw_system_error(__e);
138 // XXX EINVAL, EAGAIN, EBUSY
139 return !__gthread_recursive_mutex_trylock(&_M_mutex);
145 // XXX EINVAL, EAGAIN, EBUSY
146 __gthread_recursive_mutex_unlock(&_M_mutex);
150 native_handle() noexcept
151 { return &_M_mutex; }
154#if _GTHREAD_USE_MUTEX_TIMEDLOCK
155 /// @cond undocumented
157 template<typename _Derived>
158 class __timed_mutex_impl
161 template<typename _Rep, typename _Period>
163 _M_try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
165#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
166 using __clock = chrono::steady_clock;
168 using __clock = chrono::system_clock;
171 auto __rt = chrono::duration_cast<__clock::duration>(__rtime);
172 if (ratio_greater<__clock::period, _Period>())
174 return _M_try_lock_until(__clock::now() + __rt);
177 template<typename _Duration>
179 _M_try_lock_until(const chrono::time_point<chrono::system_clock,
182 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
183 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
185 __gthread_time_t __ts = {
186 static_cast<std::time_t>(__s.time_since_epoch().count()),
187 static_cast<long>(__ns.count())
190 return static_cast<_Derived*>(this)->_M_timedlock(__ts);
193#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
194 template<typename _Duration>
196 _M_try_lock_until(const chrono::time_point<chrono::steady_clock,
199 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
200 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
202 __gthread_time_t __ts = {
203 static_cast<std::time_t>(__s.time_since_epoch().count()),
204 static_cast<long>(__ns.count())
207 return static_cast<_Derived*>(this)->_M_clocklock(CLOCK_MONOTONIC,
212 template<typename _Clock, typename _Duration>
214 _M_try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
216#if __cplusplus > 201703L
217 static_assert(chrono::is_clock_v<_Clock>);
219 // The user-supplied clock may not tick at the same rate as
220 // steady_clock, so we must loop in order to guarantee that
221 // the timeout has expired before returning false.
222 auto __now = _Clock::now();
224 auto __rtime = __atime - __now;
225 if (_M_try_lock_for(__rtime))
227 __now = _Clock::now();
228 } while (__atime > __now);
234 /** The standard timed mutex type.
236 * A non-recursive mutex that supports a timeout when trying to acquire the
243 : private __mutex_base, public __timed_mutex_impl<timed_mutex>
246 typedef __native_type* native_handle_type;
248 timed_mutex() = default;
249 ~timed_mutex() = default;
251 timed_mutex(const timed_mutex&) = delete;
252 timed_mutex& operator=(const timed_mutex&) = delete;
257 int __e = __gthread_mutex_lock(&_M_mutex);
259 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
261 __throw_system_error(__e);
268 // XXX EINVAL, EAGAIN, EBUSY
269 return !__gthread_mutex_trylock(&_M_mutex);
272 template <class _Rep, class _Period>
275 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
276 { return _M_try_lock_for(__rtime); }
278 template <class _Clock, class _Duration>
281 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
282 { return _M_try_lock_until(__atime); }
287 // XXX EINVAL, EAGAIN, EBUSY
288 __gthread_mutex_unlock(&_M_mutex);
292 native_handle() noexcept
293 { return &_M_mutex; }
296 friend class __timed_mutex_impl<timed_mutex>;
299 _M_timedlock(const __gthread_time_t& __ts)
300 { return !__gthread_mutex_timedlock(&_M_mutex, &__ts); }
302#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
304 _M_clocklock(clockid_t __clockid, const __gthread_time_t& __ts)
305 { return !pthread_mutex_clocklock(&_M_mutex, __clockid, &__ts); }
309 /** The standard recursive timed mutex type.
311 * A recursive mutex that supports a timeout when trying to acquire the
312 * lock. A recursive mutex can be locked more than once by the same thread.
313 * Other threads cannot lock the mutex until the owning thread unlocks it
314 * as many times as it was locked.
319 class recursive_timed_mutex
320 : private __recursive_mutex_base,
321 public __timed_mutex_impl<recursive_timed_mutex>
324 typedef __native_type* native_handle_type;
326 recursive_timed_mutex() = default;
327 ~recursive_timed_mutex() = default;
329 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
330 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
335 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
337 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
339 __throw_system_error(__e);
346 // XXX EINVAL, EAGAIN, EBUSY
347 return !__gthread_recursive_mutex_trylock(&_M_mutex);
350 template <class _Rep, class _Period>
353 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
354 { return _M_try_lock_for(__rtime); }
356 template <class _Clock, class _Duration>
359 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
360 { return _M_try_lock_until(__atime); }
365 // XXX EINVAL, EAGAIN, EBUSY
366 __gthread_recursive_mutex_unlock(&_M_mutex);
370 native_handle() noexcept
371 { return &_M_mutex; }
374 friend class __timed_mutex_impl<recursive_timed_mutex>;
377 _M_timedlock(const __gthread_time_t& __ts)
378 { return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts); }
380#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
382 _M_clocklock(clockid_t __clockid, const __gthread_time_t& __ts)
383 { return !pthread_mutex_clocklock(&_M_mutex, __clockid, &__ts); }
387#else // !_GTHREAD_USE_MUTEX_TIMEDLOCK
393 condition_variable _M_cv;
394 bool _M_locked = false;
398 timed_mutex() = default;
399 ~timed_mutex() { __glibcxx_assert( !_M_locked ); }
401 timed_mutex(const timed_mutex&) = delete;
402 timed_mutex& operator=(const timed_mutex&) = delete;
407 unique_lock<mutex> __lk(_M_mut);
408 _M_cv.wait(__lk, [&]{ return !_M_locked; });
416 lock_guard<mutex> __lk(_M_mut);
423 template<typename _Rep, typename _Period>
426 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
428 unique_lock<mutex> __lk(_M_mut);
429 if (!_M_cv.wait_for(__lk, __rtime, [&]{ return !_M_locked; }))
435 template<typename _Clock, typename _Duration>
438 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
440 unique_lock<mutex> __lk(_M_mut);
441 if (!_M_cv.wait_until(__lk, __atime, [&]{ return !_M_locked; }))
450 lock_guard<mutex> __lk(_M_mut);
451 __glibcxx_assert( _M_locked );
457 /// recursive_timed_mutex
458 class recursive_timed_mutex
461 condition_variable _M_cv;
463 unsigned _M_count = 0;
465 // Predicate type that tests whether the current thread can lock a mutex.
468 // Returns true if the mutex is unlocked or is locked by _M_caller.
470 operator()() const noexcept
471 { return _M_mx->_M_count == 0 || _M_mx->_M_owner == _M_caller; }
473 const recursive_timed_mutex* _M_mx;
474 thread::id _M_caller;
479 recursive_timed_mutex() = default;
480 ~recursive_timed_mutex() { __glibcxx_assert( _M_count == 0 ); }
482 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
483 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
488 auto __id = this_thread::get_id();
489 _Can_lock __can_lock{this, __id};
490 unique_lock<mutex> __lk(_M_mut);
491 _M_cv.wait(__lk, __can_lock);
493 __throw_system_error(EAGAIN); // [thread.timedmutex.recursive]/3
502 auto __id = this_thread::get_id();
503 _Can_lock __can_lock{this, __id};
504 lock_guard<mutex> __lk(_M_mut);
514 template<typename _Rep, typename _Period>
517 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
519 auto __id = this_thread::get_id();
520 _Can_lock __can_lock{this, __id};
521 unique_lock<mutex> __lk(_M_mut);
522 if (!_M_cv.wait_for(__lk, __rtime, __can_lock))
531 template<typename _Clock, typename _Duration>
534 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
536 auto __id = this_thread::get_id();
537 _Can_lock __can_lock{this, __id};
538 unique_lock<mutex> __lk(_M_mut);
539 if (!_M_cv.wait_until(__lk, __atime, __can_lock))
551 lock_guard<mutex> __lk(_M_mut);
552 __glibcxx_assert( _M_owner == this_thread::get_id() );
553 __glibcxx_assert( _M_count > 0 );
563#endif // _GLIBCXX_HAS_GTHREADS
565 /// @cond undocumented
568 // Lock the last lockable, after all previous ones are locked.
569 template<typename _Lockable>
571 __try_lock_impl(_Lockable& __l)
573 if (unique_lock<_Lockable> __lock{__l, try_to_lock})
582 // Lock each lockable in turn.
583 // Use iteration if all lockables are the same type, recursion otherwise.
584 template<typename _L0, typename... _Lockables>
586 __try_lock_impl(_L0& __l0, _Lockables&... __lockables)
588#if __cplusplus >= 201703L
589 if constexpr ((is_same_v<_L0, _Lockables> && ...))
591 constexpr int _Np = 1 + sizeof...(_Lockables);
592 unique_lock<_L0> __locks[_Np] = {
593 {__l0, defer_lock}, {__lockables, defer_lock}...
595 for (int __i = 0; __i < _Np; ++__i)
597 if (!__locks[__i].try_lock())
599 const int __failed = __i;
601 __locks[__i].unlock();
605 for (auto& __l : __locks)
611 if (unique_lock<_L0> __lock{__l0, try_to_lock})
613 int __idx = __detail::__try_lock_impl(__lockables...);
625 } // namespace __detail
628 /** @brief Generic try_lock.
629 * @param __l1 Meets Lockable requirements (try_lock() may throw).
630 * @param __l2 Meets Lockable requirements (try_lock() may throw).
631 * @param __l3 Meets Lockable requirements (try_lock() may throw).
632 * @return Returns -1 if all try_lock() calls return true. Otherwise returns
633 * a 0-based index corresponding to the argument that returned false.
634 * @post Either all arguments are locked, or none will be.
636 * Sequentially calls try_lock() on each argument.
638 template<typename _L1, typename _L2, typename... _L3>
641 try_lock(_L1& __l1, _L2& __l2, _L3&... __l3)
643 return __detail::__try_lock_impl(__l1, __l2, __l3...);
646 /// @cond undocumented
649 // This function can recurse up to N levels deep, for N = 1+sizeof...(L1).
650 // On each recursion the lockables are rotated left one position,
651 // e.g. depth 0: l0, l1, l2; depth 1: l1, l2, l0; depth 2: l2, l0, l1.
652 // When a call to l_i.try_lock() fails it recurses/returns to depth=i
653 // so that l_i is the first argument, and then blocks until l_i is locked.
654 template<typename _L0, typename... _L1>
656 __lock_impl(int& __i, int __depth, _L0& __l0, _L1&... __l1)
658 while (__i >= __depth)
662 int __failed = 1; // index that couldn't be locked
664 unique_lock<_L0> __first(__l0);
665 __failed += __detail::__try_lock_impl(__l1...);
668 __i = -1; // finished
673#if defined _GLIBCXX_HAS_GTHREADS && defined _GLIBCXX_USE_SCHED_YIELD
676 constexpr auto __n = 1 + sizeof...(_L1);
677 __i = (__depth + __failed) % __n;
679 else // rotate left until l_i is first.
680 __detail::__lock_impl(__i, __depth + 1, __l1..., __l0);
684 } // namespace __detail
687 /** @brief Generic lock.
688 * @param __l1 Meets Lockable requirements (try_lock() may throw).
689 * @param __l2 Meets Lockable requirements (try_lock() may throw).
690 * @param __l3 Meets Lockable requirements (try_lock() may throw).
691 * @throw An exception thrown by an argument's lock() or try_lock() member.
692 * @post All arguments are locked.
694 * All arguments are locked via a sequence of calls to lock(), try_lock()
695 * and unlock(). If this function exits via an exception any locks that
696 * were obtained will be released.
698 template<typename _L1, typename _L2, typename... _L3>
700 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
702#if __cplusplus >= 201703L
703 if constexpr (is_same_v<_L1, _L2> && (is_same_v<_L1, _L3> && ...))
705 constexpr int _Np = 2 + sizeof...(_L3);
706 unique_lock<_L1> __locks[] = {
707 {__l1, defer_lock}, {__l2, defer_lock}, {__l3, defer_lock}...
711 __locks[__first].lock();
712 for (int __j = 1; __j < _Np; ++__j)
714 const int __idx = (__first + __j) % _Np;
715 if (!__locks[__idx].try_lock())
717 for (int __k = __j; __k != 0; --__k)
718 __locks[(__first + __k - 1) % _Np].unlock();
723 } while (!__locks[__first].owns_lock());
725 for (auto& __l : __locks)
732 __detail::__lock_impl(__i, 0, __l1, __l2, __l3...);
736#ifdef __cpp_lib_scoped_lock // C++ >= 17
737 /** @brief A scoped lock type for multiple lockable objects.
739 * A scoped_lock controls mutex ownership within a scope, releasing
740 * ownership in the destructor.
745 template<typename... _MutexTypes>
751 explicit scoped_lock(_MutexTypes&... __m) : _M_devices(std::tie(__m...))
752 { std::lock(__m...); }
755 explicit scoped_lock(adopt_lock_t, _MutexTypes&... __m) noexcept
756 : _M_devices(std::tie(__m...))
757 { } // calling thread owns mutex
760 { std::apply([](auto&... __m) { (__m.unlock(), ...); }, _M_devices); }
762 scoped_lock(const scoped_lock&) = delete;
763 scoped_lock& operator=(const scoped_lock&) = delete;
766 tuple<_MutexTypes&...> _M_devices;
773 explicit scoped_lock() = default;
774 explicit scoped_lock(adopt_lock_t) noexcept { }
775 ~scoped_lock() = default;
777 scoped_lock(const scoped_lock&) = delete;
778 scoped_lock& operator=(const scoped_lock&) = delete;
781 template<typename _Mutex>
782 class scoped_lock<_Mutex>
785 using mutex_type = _Mutex;
788 explicit scoped_lock(mutex_type& __m) : _M_device(__m)
789 { _M_device.lock(); }
792 explicit scoped_lock(adopt_lock_t, mutex_type& __m) noexcept
794 { } // calling thread owns mutex
797 { _M_device.unlock(); }
799 scoped_lock(const scoped_lock&) = delete;
800 scoped_lock& operator=(const scoped_lock&) = delete;
803 mutex_type& _M_device;
805#endif // __cpp_lib_scoped_lock
807#ifdef _GLIBCXX_HAS_GTHREADS
808 /// Flag type used by std::call_once
811 constexpr once_flag() noexcept = default;
813 /// Deleted copy constructor
814 once_flag(const once_flag&) = delete;
815 /// Deleted assignment operator
816 once_flag& operator=(const once_flag&) = delete;
819 // For gthreads targets a pthread_once_t is used with pthread_once, but
820 // for most targets this doesn't work correctly for exceptional executions.
821 __gthread_once_t _M_once = __GTHREAD_ONCE_INIT;
823 struct _Prepare_execution;
825 template<typename _Callable, typename... _Args>
827 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
830 /// @cond undocumented
831# ifdef _GLIBCXX_HAVE_TLS
832 // If TLS is available use thread-local state for the type-erased callable
833 // that is being run by std::call_once in the current thread.
834 extern __thread void* __once_callable;
835 extern __thread void (*__once_call)();
837 // RAII type to set up state for pthread_once call.
838 struct once_flag::_Prepare_execution
840 template<typename _Callable>
842 _Prepare_execution(_Callable& __c)
844 // Store address in thread-local pointer:
845 __once_callable = std::__addressof(__c);
846 // Trampoline function to invoke the closure via thread-local pointer:
847 __once_call = [] { (*static_cast<_Callable*>(__once_callable))(); };
850 ~_Prepare_execution()
852 // PR libstdc++/82481
853 __once_callable = nullptr;
854 __once_call = nullptr;
857 _Prepare_execution(const _Prepare_execution&) = delete;
858 _Prepare_execution& operator=(const _Prepare_execution&) = delete;
862 // Without TLS use a global std::mutex and store the callable in a
863 // global std::function.
864 extern function<void()> __once_functor;
867 __set_once_functor_lock_ptr(unique_lock<mutex>*);
872 // RAII type to set up state for pthread_once call.
873 struct once_flag::_Prepare_execution
875 template<typename _Callable>
877 _Prepare_execution(_Callable& __c)
879 // Store the callable in the global std::function
880 __once_functor = __c;
881 __set_once_functor_lock_ptr(&_M_functor_lock);
884 ~_Prepare_execution()
887 __set_once_functor_lock_ptr(nullptr);
891 // XXX This deadlocks if used recursively (PR 97949)
892 unique_lock<mutex> _M_functor_lock{__get_once_mutex()};
894 _Prepare_execution(const _Prepare_execution&) = delete;
895 _Prepare_execution& operator=(const _Prepare_execution&) = delete;
900 // This function is passed to pthread_once by std::call_once.
901 // It runs __once_call() or __once_functor().
902 extern "C" void __once_proxy(void);
904 /// Invoke a callable and synchronize with other calls using the same flag
905 template<typename _Callable, typename... _Args>
907 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
909 // Closure type that runs the function
910 auto __callable = [&] {
911 std::__invoke(std::forward<_Callable>(__f),
912 std::forward<_Args>(__args)...);
915 once_flag::_Prepare_execution __exec(__callable);
917 // XXX pthread_once does not reset the flag if an exception is thrown.
918 if (int __e = __gthread_once(&__once._M_once, &__once_proxy))
919 __throw_system_error(__e);
922#else // _GLIBCXX_HAS_GTHREADS
924 /// Flag type used by std::call_once
927 constexpr once_flag() noexcept = default;
929 /// Deleted copy constructor
930 once_flag(const once_flag&) = delete;
931 /// Deleted assignment operator
932 once_flag& operator=(const once_flag&) = delete;
935 // There are two different std::once_flag interfaces, abstracting four
936 // different implementations.
937 // The single-threaded interface uses the _M_activate() and _M_finish(bool)
938 // functions, which start and finish an active execution respectively.
939 // See [thread.once.callonce] in C++11 for the definition of
940 // active/passive/returning/exceptional executions.
941 enum _Bits : int { _Init = 0, _Active = 1, _Done = 2 };
943 int _M_once = _Bits::_Init;
945 // Check to see if all executions will be passive now.
947 _M_passive() const noexcept;
949 // Attempts to begin an active execution.
952 // Must be called to complete an active execution.
953 // The argument is true if the active execution was a returning execution,
954 // false if it was an exceptional execution.
955 void _M_finish(bool __returning) noexcept;
957 // RAII helper to call _M_finish.
958 struct _Active_execution
960 explicit _Active_execution(once_flag& __flag) : _M_flag(__flag) { }
962 ~_Active_execution() { _M_flag._M_finish(_M_returning); }
964 _Active_execution(const _Active_execution&) = delete;
965 _Active_execution& operator=(const _Active_execution&) = delete;
968 bool _M_returning = false;
971 template<typename _Callable, typename... _Args>
973 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
976 // Inline definitions of std::once_flag members for single-threaded targets.
979 once_flag::_M_passive() const noexcept
980 { return _M_once == _Bits::_Done; }
983 once_flag::_M_activate()
985 if (_M_once == _Bits::_Init) [[__likely__]]
987 _M_once = _Bits::_Active;
990 else if (_M_passive()) // Caller should have checked this already.
993 __throw_system_error(EDEADLK);
997 once_flag::_M_finish(bool __returning) noexcept
998 { _M_once = __returning ? _Bits::_Done : _Bits::_Init; }
1000 /// Invoke a callable and synchronize with other calls using the same flag
1001 template<typename _Callable, typename... _Args>
1003 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
1005 if (__once._M_passive())
1007 else if (__once._M_activate())
1009 once_flag::_Active_execution __exec(__once);
1011 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1012 // 2442. call_once() shouldn't DECAY_COPY()
1013 std::__invoke(std::forward<_Callable>(__f),
1014 std::forward<_Args>(__args)...);
1016 // __f(__args...) did not throw
1017 __exec._M_returning = true;
1020#endif // _GLIBCXX_HAS_GTHREADS
1022 /// @} group mutexes
1023_GLIBCXX_END_NAMESPACE_VERSION
1028#endif // _GLIBCXX_MUTEX