3// Copyright (C) 2003-2024 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#include <bits/requires_hosted.h> // concurrency
36#if __cplusplus < 201103L
37# include <bits/c++0x_warning.h>
40#include <tuple> // std::tuple
41#include <type_traits> // is_same_v
42#include <errno.h> // EAGAIN, EDEADLK
43#include <bits/chrono.h> // duration, time_point, is_clock_v
44#include <bits/functexcept.h> // __throw_system_error
45#include <bits/invoke.h> // __invoke
46#include <bits/move.h> // std::forward
47#include <bits/std_mutex.h>
48#include <bits/unique_lock.h>
49#if ! _GTHREAD_USE_MUTEX_TIMEDLOCK
50# include <condition_variable>
53#include <ext/atomicity.h> // __gnu_cxx::__is_single_threaded
55#if defined _GLIBCXX_HAS_GTHREADS && ! defined _GLIBCXX_HAVE_TLS
56# include <bits/std_function.h> // std::function
59#define __glibcxx_want_scoped_lock
60#include <bits/version.h>
62namespace std _GLIBCXX_VISIBILITY(default)
64_GLIBCXX_BEGIN_NAMESPACE_VERSION
71#ifdef _GLIBCXX_HAS_GTHREADS
72 /// @cond undocumented
74 // Common base class for std::recursive_mutex and std::recursive_timed_mutex
75 class __recursive_mutex_base
78 typedef __gthread_recursive_mutex_t __native_type;
80 __recursive_mutex_base(const __recursive_mutex_base&) = delete;
81 __recursive_mutex_base& operator=(const __recursive_mutex_base&) = delete;
83#ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
84 __native_type _M_mutex = __GTHREAD_RECURSIVE_MUTEX_INIT;
86 __recursive_mutex_base() = default;
88 __native_type _M_mutex;
90 __recursive_mutex_base()
92 // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
93 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
96 ~__recursive_mutex_base()
97 { __gthread_recursive_mutex_destroy(&_M_mutex); }
102 /** The standard recursive mutex type.
104 * A recursive mutex can be locked more than once by the same thread.
105 * Other threads cannot lock the mutex until the owning thread unlocks it
106 * as many times as it was locked.
111 class recursive_mutex : private __recursive_mutex_base
114 typedef __native_type* native_handle_type;
116 recursive_mutex() = default;
117 ~recursive_mutex() = default;
119 recursive_mutex(const recursive_mutex&) = delete;
120 recursive_mutex& operator=(const recursive_mutex&) = delete;
125 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
127 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
129 __throw_system_error(__e);
136 // XXX EINVAL, EAGAIN, EBUSY
137 return !__gthread_recursive_mutex_trylock(&_M_mutex);
143 // XXX EINVAL, EAGAIN, EBUSY
144 __gthread_recursive_mutex_unlock(&_M_mutex);
148 native_handle() noexcept
149 { return &_M_mutex; }
152#if _GTHREAD_USE_MUTEX_TIMEDLOCK
153 /// @cond undocumented
155 template<typename _Derived>
156 class __timed_mutex_impl
159 template<typename _Rep, typename _Period>
161 _M_try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
163#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
164 using __clock = chrono::steady_clock;
166 using __clock = chrono::system_clock;
169 auto __rt = chrono::duration_cast<__clock::duration>(__rtime);
170 if (ratio_greater<__clock::period, _Period>())
172 return _M_try_lock_until(__clock::now() + __rt);
175 template<typename _Duration>
177 _M_try_lock_until(const chrono::time_point<chrono::system_clock,
180 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
181 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
183 __gthread_time_t __ts = {
184 static_cast<std::time_t>(__s.time_since_epoch().count()),
185 static_cast<long>(__ns.count())
188 return static_cast<_Derived*>(this)->_M_timedlock(__ts);
191#ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
192 template<typename _Duration>
194 _M_try_lock_until(const chrono::time_point<chrono::steady_clock,
197 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
198 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
200 __gthread_time_t __ts = {
201 static_cast<std::time_t>(__s.time_since_epoch().count()),
202 static_cast<long>(__ns.count())
205 return static_cast<_Derived*>(this)->_M_clocklock(CLOCK_MONOTONIC,
210 template<typename _Clock, typename _Duration>
212 _M_try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
214#if __cplusplus > 201703L
215 static_assert(chrono::is_clock_v<_Clock>);
217 // The user-supplied clock may not tick at the same rate as
218 // steady_clock, so we must loop in order to guarantee that
219 // the timeout has expired before returning false.
220 auto __now = _Clock::now();
222 auto __rtime = __atime - __now;
223 if (_M_try_lock_for(__rtime))
225 __now = _Clock::now();
226 } while (__atime > __now);
232 /** The standard timed mutex type.
234 * A non-recursive mutex that supports a timeout when trying to acquire the
241 : private __mutex_base, public __timed_mutex_impl<timed_mutex>
244 typedef __native_type* native_handle_type;
246 timed_mutex() = default;
247 ~timed_mutex() = default;
249 timed_mutex(const timed_mutex&) = delete;
250 timed_mutex& operator=(const timed_mutex&) = delete;
255 int __e = __gthread_mutex_lock(&_M_mutex);
257 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
259 __throw_system_error(__e);
266 // XXX EINVAL, EAGAIN, EBUSY
267 return !__gthread_mutex_trylock(&_M_mutex);
270 template <class _Rep, class _Period>
273 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
274 { return _M_try_lock_for(__rtime); }
276 template <class _Clock, class _Duration>
279 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
280 { return _M_try_lock_until(__atime); }
285 // XXX EINVAL, EAGAIN, EBUSY
286 __gthread_mutex_unlock(&_M_mutex);
290 native_handle() noexcept
291 { return &_M_mutex; }
294 friend class __timed_mutex_impl<timed_mutex>;
297 _M_timedlock(const __gthread_time_t& __ts)
298 { return !__gthread_mutex_timedlock(&_M_mutex, &__ts); }
300#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
302 _M_clocklock(clockid_t __clockid, const __gthread_time_t& __ts)
303 { return !pthread_mutex_clocklock(&_M_mutex, __clockid, &__ts); }
307 /** The standard recursive timed mutex type.
309 * A recursive mutex that supports a timeout when trying to acquire the
310 * lock. A recursive mutex can be locked more than once by the same thread.
311 * Other threads cannot lock the mutex until the owning thread unlocks it
312 * as many times as it was locked.
317 class recursive_timed_mutex
318 : private __recursive_mutex_base,
319 public __timed_mutex_impl<recursive_timed_mutex>
322 typedef __native_type* native_handle_type;
324 recursive_timed_mutex() = default;
325 ~recursive_timed_mutex() = default;
327 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
328 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
333 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
335 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
337 __throw_system_error(__e);
344 // XXX EINVAL, EAGAIN, EBUSY
345 return !__gthread_recursive_mutex_trylock(&_M_mutex);
348 template <class _Rep, class _Period>
351 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
352 { return _M_try_lock_for(__rtime); }
354 template <class _Clock, class _Duration>
357 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
358 { return _M_try_lock_until(__atime); }
363 // XXX EINVAL, EAGAIN, EBUSY
364 __gthread_recursive_mutex_unlock(&_M_mutex);
368 native_handle() noexcept
369 { return &_M_mutex; }
372 friend class __timed_mutex_impl<recursive_timed_mutex>;
375 _M_timedlock(const __gthread_time_t& __ts)
376 { return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts); }
378#ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
380 _M_clocklock(clockid_t __clockid, const __gthread_time_t& __ts)
381 { return !pthread_mutex_clocklock(&_M_mutex, __clockid, &__ts); }
385#else // !_GTHREAD_USE_MUTEX_TIMEDLOCK
391 condition_variable _M_cv;
392 bool _M_locked = false;
396 timed_mutex() = default;
397 ~timed_mutex() { __glibcxx_assert( !_M_locked ); }
399 timed_mutex(const timed_mutex&) = delete;
400 timed_mutex& operator=(const timed_mutex&) = delete;
405 unique_lock<mutex> __lk(_M_mut);
406 _M_cv.wait(__lk, [&]{ return !_M_locked; });
414 lock_guard<mutex> __lk(_M_mut);
421 template<typename _Rep, typename _Period>
424 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
426 unique_lock<mutex> __lk(_M_mut);
427 if (!_M_cv.wait_for(__lk, __rtime, [&]{ return !_M_locked; }))
433 template<typename _Clock, typename _Duration>
436 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
438 unique_lock<mutex> __lk(_M_mut);
439 if (!_M_cv.wait_until(__lk, __atime, [&]{ return !_M_locked; }))
448 lock_guard<mutex> __lk(_M_mut);
449 __glibcxx_assert( _M_locked );
455 /// recursive_timed_mutex
456 class recursive_timed_mutex
459 condition_variable _M_cv;
461 unsigned _M_count = 0;
463 // Predicate type that tests whether the current thread can lock a mutex.
466 // Returns true if the mutex is unlocked or is locked by _M_caller.
468 operator()() const noexcept
469 { return _M_mx->_M_count == 0 || _M_mx->_M_owner == _M_caller; }
471 const recursive_timed_mutex* _M_mx;
472 thread::id _M_caller;
477 recursive_timed_mutex() = default;
478 ~recursive_timed_mutex() { __glibcxx_assert( _M_count == 0 ); }
480 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
481 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
486 auto __id = this_thread::get_id();
487 _Can_lock __can_lock{this, __id};
488 unique_lock<mutex> __lk(_M_mut);
489 _M_cv.wait(__lk, __can_lock);
491 __throw_system_error(EAGAIN); // [thread.timedmutex.recursive]/3
500 auto __id = this_thread::get_id();
501 _Can_lock __can_lock{this, __id};
502 lock_guard<mutex> __lk(_M_mut);
512 template<typename _Rep, typename _Period>
515 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
517 auto __id = this_thread::get_id();
518 _Can_lock __can_lock{this, __id};
519 unique_lock<mutex> __lk(_M_mut);
520 if (!_M_cv.wait_for(__lk, __rtime, __can_lock))
529 template<typename _Clock, typename _Duration>
532 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
534 auto __id = this_thread::get_id();
535 _Can_lock __can_lock{this, __id};
536 unique_lock<mutex> __lk(_M_mut);
537 if (!_M_cv.wait_until(__lk, __atime, __can_lock))
549 lock_guard<mutex> __lk(_M_mut);
550 __glibcxx_assert( _M_owner == this_thread::get_id() );
551 __glibcxx_assert( _M_count > 0 );
561#endif // _GLIBCXX_HAS_GTHREADS
563 /// @cond undocumented
566 // Lock the last lockable, after all previous ones are locked.
567 template<typename _Lockable>
569 __try_lock_impl(_Lockable& __l)
571 if (unique_lock<_Lockable> __lock{__l, try_to_lock})
580 // Lock each lockable in turn.
581 // Use iteration if all lockables are the same type, recursion otherwise.
582 template<typename _L0, typename... _Lockables>
584 __try_lock_impl(_L0& __l0, _Lockables&... __lockables)
586#if __cplusplus >= 201703L
587 if constexpr ((is_same_v<_L0, _Lockables> && ...))
589 constexpr int _Np = 1 + sizeof...(_Lockables);
590 unique_lock<_L0> __locks[_Np] = {
591 {__l0, defer_lock}, {__lockables, defer_lock}...
593 for (int __i = 0; __i < _Np; ++__i)
595 if (!__locks[__i].try_lock())
597 const int __failed = __i;
599 __locks[__i].unlock();
603 for (auto& __l : __locks)
609 if (unique_lock<_L0> __lock{__l0, try_to_lock})
611 int __idx = __detail::__try_lock_impl(__lockables...);
623 } // namespace __detail
626 /** @brief Generic try_lock.
627 * @param __l1 Meets Lockable requirements (try_lock() may throw).
628 * @param __l2 Meets Lockable requirements (try_lock() may throw).
629 * @param __l3 Meets Lockable requirements (try_lock() may throw).
630 * @return Returns -1 if all try_lock() calls return true. Otherwise returns
631 * a 0-based index corresponding to the argument that returned false.
632 * @post Either all arguments are locked, or none will be.
634 * Sequentially calls try_lock() on each argument.
636 template<typename _L1, typename _L2, typename... _L3>
639 try_lock(_L1& __l1, _L2& __l2, _L3&... __l3)
641 return __detail::__try_lock_impl(__l1, __l2, __l3...);
644 /// @cond undocumented
647 // This function can recurse up to N levels deep, for N = 1+sizeof...(L1).
648 // On each recursion the lockables are rotated left one position,
649 // e.g. depth 0: l0, l1, l2; depth 1: l1, l2, l0; depth 2: l2, l0, l1.
650 // When a call to l_i.try_lock() fails it recurses/returns to depth=i
651 // so that l_i is the first argument, and then blocks until l_i is locked.
652 template<typename _L0, typename... _L1>
654 __lock_impl(int& __i, int __depth, _L0& __l0, _L1&... __l1)
656 while (__i >= __depth)
660 int __failed = 1; // index that couldn't be locked
662 unique_lock<_L0> __first(__l0);
663 __failed += __detail::__try_lock_impl(__l1...);
666 __i = -1; // finished
671#if defined _GLIBCXX_HAS_GTHREADS && defined _GLIBCXX_USE_SCHED_YIELD
674 constexpr auto __n = 1 + sizeof...(_L1);
675 __i = (__depth + __failed) % __n;
677 else // rotate left until l_i is first.
678 __detail::__lock_impl(__i, __depth + 1, __l1..., __l0);
682 } // namespace __detail
685 /** @brief Generic lock.
686 * @param __l1 Meets Lockable requirements (try_lock() may throw).
687 * @param __l2 Meets Lockable requirements (try_lock() may throw).
688 * @param __l3 Meets Lockable requirements (try_lock() may throw).
689 * @throw An exception thrown by an argument's lock() or try_lock() member.
690 * @post All arguments are locked.
692 * All arguments are locked via a sequence of calls to lock(), try_lock()
693 * and unlock(). If this function exits via an exception any locks that
694 * were obtained will be released.
696 template<typename _L1, typename _L2, typename... _L3>
698 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
700#if __cplusplus >= 201703L
701 if constexpr (is_same_v<_L1, _L2> && (is_same_v<_L1, _L3> && ...))
703 constexpr int _Np = 2 + sizeof...(_L3);
704 unique_lock<_L1> __locks[] = {
705 {__l1, defer_lock}, {__l2, defer_lock}, {__l3, defer_lock}...
709 __locks[__first].lock();
710 for (int __j = 1; __j < _Np; ++__j)
712 const int __idx = (__first + __j) % _Np;
713 if (!__locks[__idx].try_lock())
715 for (int __k = __j; __k != 0; --__k)
716 __locks[(__first + __k - 1) % _Np].unlock();
721 } while (!__locks[__first].owns_lock());
723 for (auto& __l : __locks)
730 __detail::__lock_impl(__i, 0, __l1, __l2, __l3...);
734#ifdef __cpp_lib_scoped_lock // C++ >= 17 && hosted && gthread
735 /** @brief A scoped lock type for multiple lockable objects.
737 * A scoped_lock controls mutex ownership within a scope, releasing
738 * ownership in the destructor.
743 template<typename... _MutexTypes>
749 explicit scoped_lock(_MutexTypes&... __m) : _M_devices(std::tie(__m...))
750 { std::lock(__m...); }
753 explicit scoped_lock(adopt_lock_t, _MutexTypes&... __m) noexcept
754 : _M_devices(std::tie(__m...))
755 { } // calling thread owns mutex
758 { std::apply([](auto&... __m) { (__m.unlock(), ...); }, _M_devices); }
760 scoped_lock(const scoped_lock&) = delete;
761 scoped_lock& operator=(const scoped_lock&) = delete;
764 tuple<_MutexTypes&...> _M_devices;
771 explicit scoped_lock() = default;
772 explicit scoped_lock(adopt_lock_t) noexcept { }
773 ~scoped_lock() = default;
775 scoped_lock(const scoped_lock&) = delete;
776 scoped_lock& operator=(const scoped_lock&) = delete;
779 template<typename _Mutex>
780 class scoped_lock<_Mutex>
783 using mutex_type = _Mutex;
786 explicit scoped_lock(mutex_type& __m) : _M_device(__m)
787 { _M_device.lock(); }
790 explicit scoped_lock(adopt_lock_t, mutex_type& __m) noexcept
792 { } // calling thread owns mutex
795 { _M_device.unlock(); }
797 scoped_lock(const scoped_lock&) = delete;
798 scoped_lock& operator=(const scoped_lock&) = delete;
801 mutex_type& _M_device;
803#endif // __cpp_lib_scoped_lock
805#ifdef _GLIBCXX_HAS_GTHREADS
806 /// Flag type used by std::call_once
809 constexpr once_flag() noexcept = default;
811 /// Deleted copy constructor
812 once_flag(const once_flag&) = delete;
813 /// Deleted assignment operator
814 once_flag& operator=(const once_flag&) = delete;
817 // For gthreads targets a pthread_once_t is used with pthread_once, but
818 // for most targets this doesn't work correctly for exceptional executions.
819 __gthread_once_t _M_once = __GTHREAD_ONCE_INIT;
821 struct _Prepare_execution;
823 template<typename _Callable, typename... _Args>
825 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
828 /// @cond undocumented
829# ifdef _GLIBCXX_HAVE_TLS
830 // If TLS is available use thread-local state for the type-erased callable
831 // that is being run by std::call_once in the current thread.
832 extern __thread void* __once_callable;
833 extern __thread void (*__once_call)();
835 // RAII type to set up state for pthread_once call.
836 struct once_flag::_Prepare_execution
838 template<typename _Callable>
840 _Prepare_execution(_Callable& __c)
842 // Store address in thread-local pointer:
843 __once_callable = std::__addressof(__c);
844 // Trampoline function to invoke the closure via thread-local pointer:
845 __once_call = [] { (*static_cast<_Callable*>(__once_callable))(); };
848 ~_Prepare_execution()
850 // PR libstdc++/82481
851 __once_callable = nullptr;
852 __once_call = nullptr;
855 _Prepare_execution(const _Prepare_execution&) = delete;
856 _Prepare_execution& operator=(const _Prepare_execution&) = delete;
860 // Without TLS use a global std::mutex and store the callable in a
861 // global std::function.
862 extern function<void()> __once_functor;
865 __set_once_functor_lock_ptr(unique_lock<mutex>*);
870 // RAII type to set up state for pthread_once call.
871 struct once_flag::_Prepare_execution
873 template<typename _Callable>
875 _Prepare_execution(_Callable& __c)
877 // Store the callable in the global std::function
878 __once_functor = __c;
879 __set_once_functor_lock_ptr(&_M_functor_lock);
882 ~_Prepare_execution()
885 __set_once_functor_lock_ptr(nullptr);
889 // XXX This deadlocks if used recursively (PR 97949)
890 unique_lock<mutex> _M_functor_lock{__get_once_mutex()};
892 _Prepare_execution(const _Prepare_execution&) = delete;
893 _Prepare_execution& operator=(const _Prepare_execution&) = delete;
898 // This function is passed to pthread_once by std::call_once.
899 // It runs __once_call() or __once_functor().
900 extern "C" void __once_proxy(void);
902 /// Invoke a callable and synchronize with other calls using the same flag
903 template<typename _Callable, typename... _Args>
905 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
907 // Closure type that runs the function
908 auto __callable = [&] {
909 std::__invoke(std::forward<_Callable>(__f),
910 std::forward<_Args>(__args)...);
913 once_flag::_Prepare_execution __exec(__callable);
915 // XXX pthread_once does not reset the flag if an exception is thrown.
916 if (int __e = __gthread_once(&__once._M_once, &__once_proxy))
917 __throw_system_error(__e);
920#else // _GLIBCXX_HAS_GTHREADS
922 /// Flag type used by std::call_once
925 constexpr once_flag() noexcept = default;
927 /// Deleted copy constructor
928 once_flag(const once_flag&) = delete;
929 /// Deleted assignment operator
930 once_flag& operator=(const once_flag&) = delete;
933 // There are two different std::once_flag interfaces, abstracting four
934 // different implementations.
935 // The single-threaded interface uses the _M_activate() and _M_finish(bool)
936 // functions, which start and finish an active execution respectively.
937 // See [thread.once.callonce] in C++11 for the definition of
938 // active/passive/returning/exceptional executions.
939 enum _Bits : int { _Init = 0, _Active = 1, _Done = 2 };
941 int _M_once = _Bits::_Init;
943 // Check to see if all executions will be passive now.
945 _M_passive() const noexcept;
947 // Attempts to begin an active execution.
950 // Must be called to complete an active execution.
951 // The argument is true if the active execution was a returning execution,
952 // false if it was an exceptional execution.
953 void _M_finish(bool __returning) noexcept;
955 // RAII helper to call _M_finish.
956 struct _Active_execution
958 explicit _Active_execution(once_flag& __flag) : _M_flag(__flag) { }
960 ~_Active_execution() { _M_flag._M_finish(_M_returning); }
962 _Active_execution(const _Active_execution&) = delete;
963 _Active_execution& operator=(const _Active_execution&) = delete;
966 bool _M_returning = false;
969 template<typename _Callable, typename... _Args>
971 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
974 // Inline definitions of std::once_flag members for single-threaded targets.
977 once_flag::_M_passive() const noexcept
978 { return _M_once == _Bits::_Done; }
981 once_flag::_M_activate()
983 if (_M_once == _Bits::_Init) [[__likely__]]
985 _M_once = _Bits::_Active;
988 else if (_M_passive()) // Caller should have checked this already.
991 __throw_system_error(EDEADLK);
995 once_flag::_M_finish(bool __returning) noexcept
996 { _M_once = __returning ? _Bits::_Done : _Bits::_Init; }
998 /// Invoke a callable and synchronize with other calls using the same flag
999 template<typename _Callable, typename... _Args>
1001 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
1003 if (__once._M_passive())
1005 else if (__once._M_activate())
1007 once_flag::_Active_execution __exec(__once);
1009 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1010 // 2442. call_once() shouldn't DECAY_COPY()
1011 std::__invoke(std::forward<_Callable>(__f),
1012 std::forward<_Args>(__args)...);
1014 // __f(__args...) did not throw
1015 __exec._M_returning = true;
1018#endif // _GLIBCXX_HAS_GTHREADS
1020 /// @} group mutexes
1021_GLIBCXX_END_NAMESPACE_VERSION
1026#endif // _GLIBCXX_MUTEX