2 //===--------------------------- mutex ------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
22 constexpr mutex() noexcept;
25 mutex(const mutex&) = delete;
26 mutex& operator=(const mutex&) = delete;
32 typedef pthread_mutex_t* native_handle_type;
33 native_handle_type native_handle();
42 recursive_mutex(const recursive_mutex&) = delete;
43 recursive_mutex& operator=(const recursive_mutex&) = delete;
46 bool try_lock() noexcept;
49 typedef pthread_mutex_t* native_handle_type;
50 native_handle_type native_handle();
59 timed_mutex(const timed_mutex&) = delete;
60 timed_mutex& operator=(const timed_mutex&) = delete;
64 template <class Rep, class Period>
65 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
66 template <class Clock, class Duration>
67 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
71 class recursive_timed_mutex
74 recursive_timed_mutex();
75 ~recursive_timed_mutex();
77 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
78 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
81 bool try_lock() noexcept;
82 template <class Rep, class Period>
83 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
84 template <class Clock, class Duration>
85 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
89 struct defer_lock_t {};
90 struct try_to_lock_t {};
91 struct adopt_lock_t {};
93 inline constexpr defer_lock_t defer_lock{};
94 inline constexpr try_to_lock_t try_to_lock{};
95 inline constexpr adopt_lock_t adopt_lock{};
97 template <class Mutex>
101 typedef Mutex mutex_type;
103 explicit lock_guard(mutex_type& m);
104 lock_guard(mutex_type& m, adopt_lock_t);
107 lock_guard(lock_guard const&) = delete;
108 lock_guard& operator=(lock_guard const&) = delete;
111 template <class... MutexTypes>
112 class scoped_lock // C++17
115 using mutex_type = Mutex; // If MutexTypes... consists of the single type Mutex
117 explicit scoped_lock(MutexTypes&... m);
118 scoped_lock(adopt_lock_t, MutexTypes&... m);
120 scoped_lock(scoped_lock const&) = delete;
121 scoped_lock& operator=(scoped_lock const&) = delete;
123 tuple<MutexTypes&...> pm; // exposition only
126 template <class Mutex>
130 typedef Mutex mutex_type;
131 unique_lock() noexcept;
132 explicit unique_lock(mutex_type& m);
133 unique_lock(mutex_type& m, defer_lock_t) noexcept;
134 unique_lock(mutex_type& m, try_to_lock_t);
135 unique_lock(mutex_type& m, adopt_lock_t);
136 template <class Clock, class Duration>
137 unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
138 template <class Rep, class Period>
139 unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
142 unique_lock(unique_lock const&) = delete;
143 unique_lock& operator=(unique_lock const&) = delete;
145 unique_lock(unique_lock&& u) noexcept;
146 unique_lock& operator=(unique_lock&& u) noexcept;
151 template <class Rep, class Period>
152 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
153 template <class Clock, class Duration>
154 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
158 void swap(unique_lock& u) noexcept;
159 mutex_type* release() noexcept;
161 bool owns_lock() const noexcept;
162 explicit operator bool () const noexcept;
163 mutex_type* mutex() const noexcept;
166 template <class Mutex>
167 void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
169 template <class L1, class L2, class... L3>
170 int try_lock(L1&, L2&, L3&...);
171 template <class L1, class L2, class... L3>
172 void lock(L1&, L2&, L3&...);
176 constexpr once_flag() noexcept;
178 once_flag(const once_flag&) = delete;
179 once_flag& operator=(const once_flag&) = delete;
182 template<class Callable, class ...Args>
183 void call_once(once_flag& flag, Callable&& func, Args&&... args);
190 #include <__mutex_base>
192 #include <functional>
194 #ifndef _LIBCPP_CXX03_LANG
198 #include <__threading_support>
200 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
201 #pragma GCC system_header
205 #include <__undef_macros>
208 _LIBCPP_BEGIN_NAMESPACE_STD
210 #ifndef _LIBCPP_HAS_NO_THREADS
212 class _LIBCPP_TYPE_VIS recursive_mutex
214 __libcpp_recursive_mutex_t __m_;
221 recursive_mutex(const recursive_mutex&); // = delete;
222 recursive_mutex& operator=(const recursive_mutex&); // = delete;
226 bool try_lock() _NOEXCEPT;
227 void unlock() _NOEXCEPT;
229 typedef __libcpp_recursive_mutex_t* native_handle_type;
231 _LIBCPP_INLINE_VISIBILITY
232 native_handle_type native_handle() {return &__m_;}
235 class _LIBCPP_TYPE_VIS timed_mutex
238 condition_variable __cv_;
245 timed_mutex(const timed_mutex&); // = delete;
246 timed_mutex& operator=(const timed_mutex&); // = delete;
250 bool try_lock() _NOEXCEPT;
251 template <class _Rep, class _Period>
252 _LIBCPP_INLINE_VISIBILITY
253 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
254 {return try_lock_until(chrono::steady_clock::now() + __d);}
255 template <class _Clock, class _Duration>
256 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
257 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
258 void unlock() _NOEXCEPT;
261 template <class _Clock, class _Duration>
263 timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
265 using namespace chrono;
266 unique_lock<mutex> __lk(__m_);
267 bool no_timeout = _Clock::now() < __t;
268 while (no_timeout && __locked_)
269 no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
278 class _LIBCPP_TYPE_VIS recursive_timed_mutex
281 condition_variable __cv_;
285 recursive_timed_mutex();
286 ~recursive_timed_mutex();
289 recursive_timed_mutex(const recursive_timed_mutex&); // = delete;
290 recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete;
294 bool try_lock() _NOEXCEPT;
295 template <class _Rep, class _Period>
296 _LIBCPP_INLINE_VISIBILITY
297 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
298 {return try_lock_until(chrono::steady_clock::now() + __d);}
299 template <class _Clock, class _Duration>
300 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
301 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
302 void unlock() _NOEXCEPT;
305 template <class _Clock, class _Duration>
307 recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
309 using namespace chrono;
310 __thread_id __id = this_thread::get_id();
311 unique_lock<mutex> lk(__m_);
314 if (__count_ == numeric_limits<size_t>::max())
319 bool no_timeout = _Clock::now() < __t;
320 while (no_timeout && __count_ != 0)
321 no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout;
331 template <class _L0, class _L1>
333 try_lock(_L0& __l0, _L1& __l1)
335 unique_lock<_L0> __u0(__l0, try_to_lock);
336 if (__u0.owns_lock())
349 #ifndef _LIBCPP_CXX03_LANG
351 template <class _L0, class _L1, class _L2, class... _L3>
353 try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
356 unique_lock<_L0> __u0(__l0, try_to_lock);
357 if (__u0.owns_lock())
359 __r = try_lock(__l1, __l2, __l3...);
368 #endif // _LIBCPP_CXX03_LANG
370 template <class _L0, class _L1>
372 lock(_L0& __l0, _L1& __l1)
377 unique_lock<_L0> __u0(__l0);
384 __libcpp_thread_yield();
386 unique_lock<_L1> __u1(__l1);
393 __libcpp_thread_yield();
397 #ifndef _LIBCPP_CXX03_LANG
399 template <class _L0, class _L1, class _L2, class ..._L3>
401 __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
409 unique_lock<_L0> __u0(__l0);
410 __i = try_lock(__l1, __l2, __l3...);
418 __libcpp_thread_yield();
422 unique_lock<_L1> __u1(__l1);
423 __i = try_lock(__l2, __l3..., __l0);
430 if (__i == sizeof...(_L3) + 1)
434 __libcpp_thread_yield();
437 __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
443 template <class _L0, class _L1, class _L2, class ..._L3>
444 inline _LIBCPP_INLINE_VISIBILITY
446 lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
448 __lock_first(0, __l0, __l1, __l2, __l3...);
452 inline _LIBCPP_INLINE_VISIBILITY
453 void __unlock(_L0& __l0) {
457 template <class _L0, class _L1>
458 inline _LIBCPP_INLINE_VISIBILITY
459 void __unlock(_L0& __l0, _L1& __l1) {
464 template <class _L0, class _L1, class _L2, class ..._L3>
465 inline _LIBCPP_INLINE_VISIBILITY
466 void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
469 _VSTD::__unlock(__l2, __l3...);
472 #endif // _LIBCPP_CXX03_LANG
474 #if _LIBCPP_STD_VER > 14
475 template <class ..._Mutexes>
476 class _LIBCPP_TEMPLATE_VIS scoped_lock;
479 class _LIBCPP_TEMPLATE_VIS scoped_lock<> {
481 explicit scoped_lock() {}
482 ~scoped_lock() = default;
484 _LIBCPP_INLINE_VISIBILITY
485 explicit scoped_lock(adopt_lock_t) {}
487 scoped_lock(scoped_lock const&) = delete;
488 scoped_lock& operator=(scoped_lock const&) = delete;
491 template <class _Mutex>
492 class _LIBCPP_TEMPLATE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) scoped_lock<_Mutex> {
494 typedef _Mutex mutex_type;
498 explicit scoped_lock(mutex_type & __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
499 : __m_(__m) {__m_.lock();}
501 ~scoped_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}
503 _LIBCPP_INLINE_VISIBILITY
504 explicit scoped_lock(adopt_lock_t, mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
507 scoped_lock(scoped_lock const&) = delete;
508 scoped_lock& operator=(scoped_lock const&) = delete;
511 template <class ..._MArgs>
512 class _LIBCPP_TEMPLATE_VIS scoped_lock
514 static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required");
515 typedef tuple<_MArgs&...> _MutexTuple;
518 _LIBCPP_INLINE_VISIBILITY
519 explicit scoped_lock(_MArgs&... __margs)
522 _VSTD::lock(__margs...);
525 _LIBCPP_INLINE_VISIBILITY
526 scoped_lock(adopt_lock_t, _MArgs&... __margs)
531 _LIBCPP_INLINE_VISIBILITY
533 typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
534 __unlock_unpack(_Indices{}, __t_);
537 scoped_lock(scoped_lock const&) = delete;
538 scoped_lock& operator=(scoped_lock const&) = delete;
541 template <size_t ..._Indx>
542 _LIBCPP_INLINE_VISIBILITY
543 static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
544 _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...);
550 #endif // _LIBCPP_STD_VER > 14
551 #endif // !_LIBCPP_HAS_NO_THREADS
553 struct _LIBCPP_TEMPLATE_VIS once_flag;
555 #ifndef _LIBCPP_CXX03_LANG
557 template<class _Callable, class... _Args>
558 _LIBCPP_INLINE_VISIBILITY
559 void call_once(once_flag&, _Callable&&, _Args&&...);
561 #else // _LIBCPP_CXX03_LANG
563 template<class _Callable>
564 _LIBCPP_INLINE_VISIBILITY
565 void call_once(once_flag&, _Callable&);
567 template<class _Callable>
568 _LIBCPP_INLINE_VISIBILITY
569 void call_once(once_flag&, const _Callable&);
571 #endif // _LIBCPP_CXX03_LANG
573 struct _LIBCPP_TEMPLATE_VIS once_flag
575 _LIBCPP_INLINE_VISIBILITY
577 once_flag() _NOEXCEPT : __state_(0) {}
579 #if defined(_LIBCPP_ABI_MICROSOFT)
580 typedef uintptr_t _State_type;
582 typedef unsigned long _State_type;
587 once_flag(const once_flag&); // = delete;
588 once_flag& operator=(const once_flag&); // = delete;
590 _State_type __state_;
592 #ifndef _LIBCPP_CXX03_LANG
593 template<class _Callable, class... _Args>
595 void call_once(once_flag&, _Callable&&, _Args&&...);
596 #else // _LIBCPP_CXX03_LANG
597 template<class _Callable>
599 void call_once(once_flag&, _Callable&);
601 template<class _Callable>
603 void call_once(once_flag&, const _Callable&);
604 #endif // _LIBCPP_CXX03_LANG
607 #ifndef _LIBCPP_CXX03_LANG
610 class __call_once_param
614 _LIBCPP_INLINE_VISIBILITY
615 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
617 _LIBCPP_INLINE_VISIBILITY
620 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
625 template <size_t ..._Indices>
626 _LIBCPP_INLINE_VISIBILITY
627 void __execute(__tuple_indices<_Indices...>)
629 __invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
636 class __call_once_param
640 _LIBCPP_INLINE_VISIBILITY
641 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
643 _LIBCPP_INLINE_VISIBILITY
654 __call_once_proxy(void* __vp)
656 __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
660 _LIBCPP_FUNC_VIS void __call_once(volatile once_flag::_State_type&, void*,
663 #ifndef _LIBCPP_CXX03_LANG
665 template<class _Callable, class... _Args>
666 inline _LIBCPP_INLINE_VISIBILITY
668 call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
670 if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0))
672 typedef tuple<_Callable&&, _Args&&...> _Gp;
673 _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
674 __call_once_param<_Gp> __p(__f);
675 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
679 #else // _LIBCPP_CXX03_LANG
681 template<class _Callable>
682 inline _LIBCPP_INLINE_VISIBILITY
684 call_once(once_flag& __flag, _Callable& __func)
686 if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0))
688 __call_once_param<_Callable> __p(__func);
689 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
693 template<class _Callable>
694 inline _LIBCPP_INLINE_VISIBILITY
696 call_once(once_flag& __flag, const _Callable& __func)
698 if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0))
700 __call_once_param<const _Callable> __p(__func);
701 __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
705 #endif // _LIBCPP_CXX03_LANG
707 _LIBCPP_END_NAMESPACE_STD
711 #endif // _LIBCPP_MUTEX