3 // Copyright (C) 2003-2026 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.
29 #ifndef _GLIBCXX_MUTEX
30 #define _GLIBCXX_MUTEX 1
32 #ifdef _GLIBCXX_SYSHDR
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>
64 namespace 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 __gthread_time_t __ts = chrono::__to_timeout_gthread_time_t(__atime);
183 return static_cast<_Derived*>(this)->_M_timedlock(__ts);
186 #if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
187 template<typename _Duration>
189 _M_try_lock_until(const chrono::time_point<chrono::steady_clock,
192 __gthread_time_t __ts = chrono::__to_timeout_gthread_time_t(__atime);
193 return static_cast<_Derived*>(this)->_M_clocklock(CLOCK_MONOTONIC,
198 template<typename _Clock, typename _Duration>
200 _M_try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
202 #if __cplusplus > 201703L
203 static_assert(chrono::is_clock_v<_Clock>);
205 // The user-supplied clock may not tick at the same rate as
206 // steady_clock, so we must loop in order to guarantee that
207 // the timeout has expired before returning false.
208 auto __now = _Clock::now();
210 auto __rtime = __atime - __now;
211 if (_M_try_lock_for(__rtime))
213 __now = _Clock::now();
214 } while (__atime > __now);
220 /** The standard timed mutex type.
222 * A non-recursive mutex that supports a timeout when trying to acquire the
229 : private __mutex_base, public __timed_mutex_impl<timed_mutex>
232 typedef __native_type* native_handle_type;
234 timed_mutex() = default;
235 ~timed_mutex() = default;
237 timed_mutex(const timed_mutex&) = delete;
238 timed_mutex& operator=(const timed_mutex&) = delete;
243 int __e = __gthread_mutex_lock(&_M_mutex);
245 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
247 __throw_system_error(__e);
254 // XXX EINVAL, EAGAIN, EBUSY
255 return !__gthread_mutex_trylock(&_M_mutex);
258 template <class _Rep, class _Period>
261 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
262 { return _M_try_lock_for(__rtime); }
264 template <class _Clock, class _Duration>
267 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
268 { return _M_try_lock_until(__atime); }
273 // XXX EINVAL, EAGAIN, EBUSY
274 __gthread_mutex_unlock(&_M_mutex);
278 native_handle() noexcept
279 { return &_M_mutex; }
282 friend class __timed_mutex_impl<timed_mutex>;
285 _M_timedlock(const __gthread_time_t& __ts)
286 { return !__gthread_mutex_timedlock(&_M_mutex, &__ts); }
288 #if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
290 _M_clocklock(clockid_t __clockid, const __gthread_time_t& __ts)
291 { return !pthread_mutex_clocklock(&_M_mutex, __clockid, &__ts); }
295 /** The standard recursive timed mutex type.
297 * A recursive mutex that supports a timeout when trying to acquire the
298 * lock. A recursive mutex can be locked more than once by the same thread.
299 * Other threads cannot lock the mutex until the owning thread unlocks it
300 * as many times as it was locked.
305 class recursive_timed_mutex
306 : private __recursive_mutex_base,
307 public __timed_mutex_impl<recursive_timed_mutex>
310 typedef __native_type* native_handle_type;
312 recursive_timed_mutex() = default;
313 ~recursive_timed_mutex() = default;
315 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
316 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
321 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
323 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
325 __throw_system_error(__e);
332 // XXX EINVAL, EAGAIN, EBUSY
333 return !__gthread_recursive_mutex_trylock(&_M_mutex);
336 template <class _Rep, class _Period>
339 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
340 { return _M_try_lock_for(__rtime); }
342 template <class _Clock, class _Duration>
345 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
346 { return _M_try_lock_until(__atime); }
351 // XXX EINVAL, EAGAIN, EBUSY
352 __gthread_recursive_mutex_unlock(&_M_mutex);
356 native_handle() noexcept
357 { return &_M_mutex; }
360 friend class __timed_mutex_impl<recursive_timed_mutex>;
363 _M_timedlock(const __gthread_time_t& __ts)
364 { return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts); }
366 #if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
368 _M_clocklock(clockid_t __clockid, const __gthread_time_t& __ts)
369 { return !pthread_mutex_clocklock(&_M_mutex, __clockid, &__ts); }
373 #else // !_GTHREAD_USE_MUTEX_TIMEDLOCK
379 condition_variable _M_cv;
380 bool _M_locked = false;
384 timed_mutex() = default;
385 ~timed_mutex() { __glibcxx_assert( !_M_locked ); }
387 timed_mutex(const timed_mutex&) = delete;
388 timed_mutex& operator=(const timed_mutex&) = delete;
393 unique_lock<mutex> __lk(_M_mut);
394 _M_cv.wait(__lk, [&]{ return !_M_locked; });
402 lock_guard<mutex> __lk(_M_mut);
409 template<typename _Rep, typename _Period>
412 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
414 unique_lock<mutex> __lk(_M_mut);
415 if (!_M_cv.wait_for(__lk, __rtime, [&]{ return !_M_locked; }))
421 template<typename _Clock, typename _Duration>
424 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
426 unique_lock<mutex> __lk(_M_mut);
427 if (!_M_cv.wait_until(__lk, __atime, [&]{ return !_M_locked; }))
436 lock_guard<mutex> __lk(_M_mut);
437 __glibcxx_assert( _M_locked );
443 /// recursive_timed_mutex
444 class recursive_timed_mutex
447 condition_variable _M_cv;
449 unsigned _M_count = 0;
451 // Predicate type that tests whether the current thread can lock a mutex.
454 // Returns true if the mutex is unlocked or is locked by _M_caller.
456 operator()() const noexcept
457 { return _M_mx->_M_count == 0 || _M_mx->_M_owner == _M_caller; }
459 const recursive_timed_mutex* _M_mx;
460 thread::id _M_caller;
465 recursive_timed_mutex() = default;
466 ~recursive_timed_mutex() { __glibcxx_assert( _M_count == 0 ); }
468 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
469 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
474 auto __id = this_thread::get_id();
475 _Can_lock __can_lock{this, __id};
476 unique_lock<mutex> __lk(_M_mut);
477 _M_cv.wait(__lk, __can_lock);
479 __throw_system_error(EAGAIN); // [thread.timedmutex.recursive]/3
488 auto __id = this_thread::get_id();
489 _Can_lock __can_lock{this, __id};
490 lock_guard<mutex> __lk(_M_mut);
500 template<typename _Rep, typename _Period>
503 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
505 auto __id = this_thread::get_id();
506 _Can_lock __can_lock{this, __id};
507 unique_lock<mutex> __lk(_M_mut);
508 if (!_M_cv.wait_for(__lk, __rtime, __can_lock))
517 template<typename _Clock, typename _Duration>
520 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
522 auto __id = this_thread::get_id();
523 _Can_lock __can_lock{this, __id};
524 unique_lock<mutex> __lk(_M_mut);
525 if (!_M_cv.wait_until(__lk, __atime, __can_lock))
537 lock_guard<mutex> __lk(_M_mut);
538 __glibcxx_assert( _M_owner == this_thread::get_id() );
539 __glibcxx_assert( _M_count > 0 );
549 #endif // _GLIBCXX_HAS_GTHREADS
551 /// @cond undocumented
554 // Lock the last lockable, after all previous ones are locked.
555 template<typename _Lockable>
557 __try_lock_impl(_Lockable& __l)
559 if (unique_lock<_Lockable> __lock{__l, try_to_lock})
568 // Lock each lockable in turn.
569 // Use iteration if all lockables are the same type, recursion otherwise.
570 template<typename _L0, typename... _Lockables>
572 __try_lock_impl(_L0& __l0, _Lockables&... __lockables)
574 #if __cplusplus >= 201703L
575 if constexpr ((is_same_v<_L0, _Lockables> && ...))
577 constexpr int _Np = 1 + sizeof...(_Lockables);
578 unique_lock<_L0> __locks[_Np] = {
579 {__l0, defer_lock}, {__lockables, defer_lock}...
581 for (int __i = 0; __i < _Np; ++__i)
583 if (!__locks[__i].try_lock())
585 const int __failed = __i;
587 __locks[__i].unlock();
591 for (auto& __l : __locks)
597 if (unique_lock<_L0> __lock{__l0, try_to_lock})
599 int __idx = __detail::__try_lock_impl(__lockables...);
611 } // namespace __detail
614 /** @brief Generic try_lock.
615 * @param __l1 Meets Lockable requirements (try_lock() may throw).
616 * @param __l2 Meets Lockable requirements (try_lock() may throw).
617 * @param __l3 Meets Lockable requirements (try_lock() may throw).
618 * @return Returns -1 if all try_lock() calls return true. Otherwise returns
619 * a 0-based index corresponding to the argument that returned false.
620 * @post Either all arguments are locked, or none will be.
622 * Sequentially calls try_lock() on each argument.
624 template<typename _L1, typename _L2, typename... _L3>
627 try_lock(_L1& __l1, _L2& __l2, _L3&... __l3)
629 return __detail::__try_lock_impl(__l1, __l2, __l3...);
632 /// @cond undocumented
635 // This function can recurse up to N levels deep, for N = 1+sizeof...(L1).
636 // On each recursion the lockables are rotated left one position,
637 // e.g. depth 0: l0, l1, l2; depth 1: l1, l2, l0; depth 2: l2, l0, l1.
638 // When a call to l_i.try_lock() fails it recurses/returns to depth=i
639 // so that l_i is the first argument, and then blocks until l_i is locked.
640 template<typename _L0, typename... _L1>
642 __lock_impl(int& __i, int __depth, _L0& __l0, _L1&... __l1)
644 while (__i >= __depth)
648 int __failed = 1; // index that couldn't be locked
650 unique_lock<_L0> __first(__l0);
651 __failed += __detail::__try_lock_impl(__l1...);
654 __i = -1; // finished
659 #if defined _GLIBCXX_HAS_GTHREADS && defined _GLIBCXX_USE_SCHED_YIELD
662 constexpr auto __n = 1 + sizeof...(_L1);
663 __i = (__depth + __failed) % __n;
665 else // rotate left until l_i is first.
666 __detail::__lock_impl(__i, __depth + 1, __l1..., __l0);
670 } // namespace __detail
673 /** @brief Generic lock.
674 * @param __l1 Meets Lockable requirements (try_lock() may throw).
675 * @param __l2 Meets Lockable requirements (try_lock() may throw).
676 * @param __l3 Meets Lockable requirements (try_lock() may throw).
677 * @throw An exception thrown by an argument's lock() or try_lock() member.
678 * @post All arguments are locked.
680 * All arguments are locked via a sequence of calls to lock(), try_lock()
681 * and unlock(). If this function exits via an exception any locks that
682 * were obtained will be released.
684 template<typename _L1, typename _L2, typename... _L3>
686 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
688 #if __cplusplus >= 201703L
689 if constexpr (is_same_v<_L1, _L2> && (is_same_v<_L1, _L3> && ...))
691 constexpr int _Np = 2 + sizeof...(_L3);
692 unique_lock<_L1> __locks[] = {
693 {__l1, defer_lock}, {__l2, defer_lock}, {__l3, defer_lock}...
697 __locks[__first].lock();
698 for (int __j = 1; __j < _Np; ++__j)
700 const int __idx = (__first + __j) % _Np;
701 if (!__locks[__idx].try_lock())
703 for (int __k = __j; __k != 0; --__k)
704 __locks[(__first + __k - 1) % _Np].unlock();
709 } while (!__locks[__first].owns_lock());
711 for (auto& __l : __locks)
718 __detail::__lock_impl(__i, 0, __l1, __l2, __l3...);
722 #ifdef __cpp_lib_scoped_lock // C++ >= 17
723 /** @brief A scoped lock type for multiple lockable objects.
725 * A scoped_lock controls mutex ownership within a scope, releasing
726 * ownership in the destructor.
731 template<typename... _MutexTypes>
737 explicit scoped_lock(_MutexTypes&... __m) : _M_devices(std::tie(__m...))
738 { std::lock(__m...); }
741 explicit scoped_lock(adopt_lock_t, _MutexTypes&... __m) noexcept
742 : _M_devices(std::tie(__m...))
743 { } // calling thread owns mutex
746 { std::apply([](auto&... __m) { (__m.unlock(), ...); }, _M_devices); }
748 scoped_lock(const scoped_lock&) = delete;
749 scoped_lock& operator=(const scoped_lock&) = delete;
752 tuple<_MutexTypes&...> _M_devices;
759 explicit scoped_lock() = default;
760 explicit scoped_lock(adopt_lock_t) noexcept { }
761 ~scoped_lock() = default;
763 scoped_lock(const scoped_lock&) = delete;
764 scoped_lock& operator=(const scoped_lock&) = delete;
767 template<typename _Mutex>
768 class scoped_lock<_Mutex>
771 using mutex_type = _Mutex;
774 explicit scoped_lock(mutex_type& __m) : _M_device(__m)
775 { _M_device.lock(); }
778 explicit scoped_lock(adopt_lock_t, mutex_type& __m) noexcept
780 { } // calling thread owns mutex
783 { _M_device.unlock(); }
785 scoped_lock(const scoped_lock&) = delete;
786 scoped_lock& operator=(const scoped_lock&) = delete;
789 mutex_type& _M_device;
791 #endif // __cpp_lib_scoped_lock
793 #ifdef _GLIBCXX_HAS_GTHREADS
794 /// Flag type used by std::call_once
797 constexpr once_flag() noexcept = default;
799 /// Deleted copy constructor
800 once_flag(const once_flag&) = delete;
801 /// Deleted assignment operator
802 once_flag& operator=(const once_flag&) = delete;
805 // For gthreads targets a pthread_once_t is used with pthread_once, but
806 // for most targets this doesn't work correctly for exceptional executions.
807 __gthread_once_t _M_once = __GTHREAD_ONCE_INIT;
809 struct _Prepare_execution;
811 template<typename _Callable, typename... _Args>
813 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
816 /// @cond undocumented
817 # ifdef _GLIBCXX_HAVE_TLS
818 // If TLS is available use thread-local state for the type-erased callable
819 // that is being run by std::call_once in the current thread.
820 # ifdef _GLIBCXX_NO_EXTERN_THREAD_LOCAL
823 __get_once_callable() noexcept;
825 std::add_lvalue_reference<void (*)()>::type
826 __get_once_call() noexcept;
828 // These macros mean that all the code below uses the same syntax:
829 #define __once_callable __get_once_callable()
830 #define __once_call __get_once_call()
832 # else // ! _GLIBCXX_NO_EXTERN_THREAD_LOCAL
834 extern __thread void* __once_callable;
835 extern __thread void (*__once_call)();
837 # endif // ! _GLIBCXX_NO_EXTERN_THREAD_LOCAL
839 // RAII type to set up state for pthread_once call.
840 struct once_flag::_Prepare_execution
842 template<typename _Callable>
844 _Prepare_execution(_Callable& __c)
846 // Store address in thread-local pointer:
847 __once_callable = std::__addressof(__c);
848 // Trampoline function to invoke the closure via thread-local pointer:
849 __once_call = [] { (*static_cast<_Callable*>(__once_callable))(); };
852 ~_Prepare_execution()
854 // PR libstdc++/82481
855 __once_callable = nullptr;
856 __once_call = nullptr;
859 _Prepare_execution(const _Prepare_execution&) = delete;
860 _Prepare_execution& operator=(const _Prepare_execution&) = delete;
863 #undef __once_callable
867 // Without TLS use a global std::mutex and store the callable in a
868 // global std::function.
869 extern function<void()> __once_functor;
872 __set_once_functor_lock_ptr(unique_lock<mutex>*);
877 // RAII type to set up state for pthread_once call.
878 struct once_flag::_Prepare_execution
880 template<typename _Callable>
882 _Prepare_execution(_Callable& __c)
884 // Store the callable in the global std::function
885 __once_functor = __c;
886 __set_once_functor_lock_ptr(&_M_functor_lock);
889 ~_Prepare_execution()
892 __set_once_functor_lock_ptr(nullptr);
896 // XXX This deadlocks if used recursively (PR 97949)
897 unique_lock<mutex> _M_functor_lock{__get_once_mutex()};
899 _Prepare_execution(const _Prepare_execution&) = delete;
900 _Prepare_execution& operator=(const _Prepare_execution&) = delete;
905 // This function is passed to pthread_once by std::call_once.
906 // It runs __once_call() or __once_functor().
907 extern "C" void __once_proxy(void);
909 /// Invoke a callable and synchronize with other calls using the same flag
910 template<typename _Callable, typename... _Args>
912 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
914 // Closure type that runs the function
915 auto __callable = [&] {
916 std::__invoke(std::forward<_Callable>(__f),
917 std::forward<_Args>(__args)...);
920 once_flag::_Prepare_execution __exec(__callable);
922 // XXX pthread_once does not reset the flag if an exception is thrown.
923 if (int __e = __gthread_once(&__once._M_once, &__once_proxy))
924 __throw_system_error(__e);
927 #else // _GLIBCXX_HAS_GTHREADS
929 /// Flag type used by std::call_once
932 constexpr once_flag() noexcept = default;
934 /// Deleted copy constructor
935 once_flag(const once_flag&) = delete;
936 /// Deleted assignment operator
937 once_flag& operator=(const once_flag&) = delete;
940 // There are two different std::once_flag interfaces, abstracting four
941 // different implementations.
942 // The single-threaded interface uses the _M_activate() and _M_finish(bool)
943 // functions, which start and finish an active execution respectively.
944 // See [thread.once.callonce] in C++11 for the definition of
945 // active/passive/returning/exceptional executions.
946 enum _Bits : int { _Init = 0, _Active = 1, _Done = 2 };
948 int _M_once = _Bits::_Init;
950 // Check to see if all executions will be passive now.
952 _M_passive() const noexcept;
954 // Attempts to begin an active execution.
957 // Must be called to complete an active execution.
958 // The argument is true if the active execution was a returning execution,
959 // false if it was an exceptional execution.
960 void _M_finish(bool __returning) noexcept;
962 // RAII helper to call _M_finish.
963 struct _Active_execution
965 explicit _Active_execution(once_flag& __flag) : _M_flag(__flag) { }
967 ~_Active_execution() { _M_flag._M_finish(_M_returning); }
969 _Active_execution(const _Active_execution&) = delete;
970 _Active_execution& operator=(const _Active_execution&) = delete;
973 bool _M_returning = false;
976 template<typename _Callable, typename... _Args>
978 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
981 // Inline definitions of std::once_flag members for single-threaded targets.
984 once_flag::_M_passive() const noexcept
985 { return _M_once == _Bits::_Done; }
988 once_flag::_M_activate()
990 if (_M_once == _Bits::_Init) [[__likely__]]
992 _M_once = _Bits::_Active;
995 else if (_M_passive()) // Caller should have checked this already.
998 __throw_system_error(EDEADLK);
1002 once_flag::_M_finish(bool __returning) noexcept
1003 { _M_once = __returning ? _Bits::_Done : _Bits::_Init; }
1005 /// Invoke a callable and synchronize with other calls using the same flag
1006 template<typename _Callable, typename... _Args>
1008 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
1010 if (__once._M_passive())
1012 else if (__once._M_activate())
1014 once_flag::_Active_execution __exec(__once);
1016 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1017 // 2442. call_once() shouldn't DECAY_COPY()
1018 std::__invoke(std::forward<_Callable>(__f),
1019 std::forward<_Args>(__args)...);
1021 // __f(__args...) did not throw
1022 __exec._M_returning = true;
1025 #endif // _GLIBCXX_HAS_GTHREADS
1027 /// @} group mutexes
1028 _GLIBCXX_END_NAMESPACE_VERSION
1033 #endif // _GLIBCXX_MUTEX