2 //===--------------------------- mutex ------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
23 constexpr mutex() noexcept;
26 mutex(const mutex&) = delete;
27 mutex& operator=(const mutex&) = delete;
33 typedef pthread_mutex_t* native_handle_type;
34 native_handle_type native_handle();
43 recursive_mutex(const recursive_mutex&) = delete;
44 recursive_mutex& operator=(const recursive_mutex&) = delete;
47 bool try_lock() noexcept;
50 typedef pthread_mutex_t* native_handle_type;
51 native_handle_type native_handle();
60 timed_mutex(const timed_mutex&) = delete;
61 timed_mutex& operator=(const timed_mutex&) = delete;
65 template <class Rep, class Period>
66 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
67 template <class Clock, class Duration>
68 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
72 class recursive_timed_mutex
75 recursive_timed_mutex();
76 ~recursive_timed_mutex();
78 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
79 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
82 bool try_lock() noexcept;
83 template <class Rep, class Period>
84 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
85 template <class Clock, class Duration>
86 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
90 struct defer_lock_t {};
91 struct try_to_lock_t {};
92 struct adopt_lock_t {};
94 constexpr defer_lock_t defer_lock{};
95 constexpr try_to_lock_t try_to_lock{};
96 constexpr adopt_lock_t adopt_lock{};
98 template <class Mutex>
102 typedef Mutex mutex_type;
104 explicit lock_guard(mutex_type& m);
105 lock_guard(mutex_type& m, adopt_lock_t);
108 lock_guard(lock_guard const&) = delete;
109 lock_guard& operator=(lock_guard const&) = delete;
112 template <class... MutexTypes>
113 class scoped_lock // C++17
116 using mutex_type = Mutex; // If MutexTypes... consists of the single type Mutex
118 explicit scoped_lock(MutexTypes&... m);
119 scoped_lock(MutexTypes&... m, adopt_lock_t);
121 scoped_lock(scoped_lock const&) = delete;
122 scoped_lock& operator=(scoped_lock const&) = delete;
124 tuple<MutexTypes&...> pm; // exposition only
127 template <class Mutex>
131 typedef Mutex mutex_type;
132 unique_lock() noexcept;
133 explicit unique_lock(mutex_type& m);
134 unique_lock(mutex_type& m, defer_lock_t) noexcept;
135 unique_lock(mutex_type& m, try_to_lock_t);
136 unique_lock(mutex_type& m, adopt_lock_t);
137 template <class Clock, class Duration>
138 unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
139 template <class Rep, class Period>
140 unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
143 unique_lock(unique_lock const&) = delete;
144 unique_lock& operator=(unique_lock const&) = delete;
146 unique_lock(unique_lock&& u) noexcept;
147 unique_lock& operator=(unique_lock&& u) noexcept;
152 template <class Rep, class Period>
153 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
154 template <class Clock, class Duration>
155 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
159 void swap(unique_lock& u) noexcept;
160 mutex_type* release() noexcept;
162 bool owns_lock() const noexcept;
163 explicit operator bool () const noexcept;
164 mutex_type* mutex() const noexcept;
167 template <class Mutex>
168 void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
170 template <class L1, class L2, class... L3>
171 int try_lock(L1&, L2&, L3&...);
172 template <class L1, class L2, class... L3>
173 void lock(L1&, L2&, L3&...);
177 constexpr once_flag() noexcept;
179 once_flag(const once_flag&) = delete;
180 once_flag& operator=(const once_flag&) = delete;
183 template<class Callable, class ...Args>
184 void call_once(once_flag& flag, Callable&& func, Args&&... args);
191 #include <__mutex_base>
192 #include <functional>
194 #ifndef _LIBCPP_CXX03_LANG
197 #include <__threading_support>
199 #include <__undef_min_max>
201 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
202 #pragma GCC system_header
205 _LIBCPP_BEGIN_NAMESPACE_STD
207 #ifndef _LIBCPP_HAS_NO_THREADS
209 class _LIBCPP_TYPE_VIS recursive_mutex
211 __libcpp_recursive_mutex_t __m_;
218 recursive_mutex(const recursive_mutex&); // = delete;
219 recursive_mutex& operator=(const recursive_mutex&); // = delete;
223 bool try_lock() _NOEXCEPT;
224 void unlock() _NOEXCEPT;
226 typedef __libcpp_recursive_mutex_t* native_handle_type;
228 _LIBCPP_INLINE_VISIBILITY
229 native_handle_type native_handle() {return &__m_;}
232 class _LIBCPP_TYPE_VIS timed_mutex
235 condition_variable __cv_;
242 timed_mutex(const timed_mutex&); // = delete;
243 timed_mutex& operator=(const timed_mutex&); // = delete;
247 bool try_lock() _NOEXCEPT;
248 template <class _Rep, class _Period>
249 _LIBCPP_INLINE_VISIBILITY
250 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
251 {return try_lock_until(chrono::steady_clock::now() + __d);}
252 template <class _Clock, class _Duration>
253 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
254 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
255 void unlock() _NOEXCEPT;
258 template <class _Clock, class _Duration>
260 timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
262 using namespace chrono;
263 unique_lock<mutex> __lk(__m_);
264 bool no_timeout = _Clock::now() < __t;
265 while (no_timeout && __locked_)
266 no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
275 class _LIBCPP_TYPE_VIS recursive_timed_mutex
278 condition_variable __cv_;
280 __libcpp_thread_id __id_;
282 recursive_timed_mutex();
283 ~recursive_timed_mutex();
286 recursive_timed_mutex(const recursive_timed_mutex&); // = delete;
287 recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete;
291 bool try_lock() _NOEXCEPT;
292 template <class _Rep, class _Period>
293 _LIBCPP_INLINE_VISIBILITY
294 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
295 {return try_lock_until(chrono::steady_clock::now() + __d);}
296 template <class _Clock, class _Duration>
297 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
298 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
299 void unlock() _NOEXCEPT;
302 template <class _Clock, class _Duration>
304 recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
306 using namespace chrono;
307 __libcpp_thread_id __id = __libcpp_thread_get_current_id();
308 unique_lock<mutex> lk(__m_);
309 if (__libcpp_thread_id_equal(__id, __id_))
311 if (__count_ == numeric_limits<size_t>::max())
316 bool no_timeout = _Clock::now() < __t;
317 while (no_timeout && __count_ != 0)
318 no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout;
328 template <class _L0, class _L1>
330 try_lock(_L0& __l0, _L1& __l1)
332 unique_lock<_L0> __u0(__l0, try_to_lock);
333 if (__u0.owns_lock())
346 #ifndef _LIBCPP_CXX03_LANG
348 template <class _L0, class _L1, class _L2, class... _L3>
350 try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
353 unique_lock<_L0> __u0(__l0, try_to_lock);
354 if (__u0.owns_lock())
356 __r = try_lock(__l1, __l2, __l3...);
365 #endif // _LIBCPP_CXX03_LANG
367 template <class _L0, class _L1>
369 lock(_L0& __l0, _L1& __l1)
374 unique_lock<_L0> __u0(__l0);
381 __libcpp_thread_yield();
383 unique_lock<_L1> __u1(__l1);
390 __libcpp_thread_yield();
394 #ifndef _LIBCPP_CXX03_LANG
396 template <class _L0, class _L1, class _L2, class ..._L3>
398 __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
406 unique_lock<_L0> __u0(__l0);
407 __i = try_lock(__l1, __l2, __l3...);
415 __libcpp_thread_yield();
419 unique_lock<_L1> __u1(__l1);
420 __i = try_lock(__l2, __l3..., __l0);
427 if (__i == sizeof...(_L3) + 1)
431 __libcpp_thread_yield();
434 __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
440 template <class _L0, class _L1, class _L2, class ..._L3>
441 inline _LIBCPP_INLINE_VISIBILITY
443 lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
445 __lock_first(0, __l0, __l1, __l2, __l3...);
449 inline _LIBCPP_INLINE_VISIBILITY
450 void __unlock(_L0& __l0) {
454 template <class _L0, class _L1>
455 inline _LIBCPP_INLINE_VISIBILITY
456 void __unlock(_L0& __l0, _L1& __l1) {
461 template <class _L0, class _L1, class _L2, class ..._L3>
462 inline _LIBCPP_INLINE_VISIBILITY
463 void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
466 _VSTD::__unlock(__l2, __l3...);
469 #endif // _LIBCPP_CXX03_LANG
471 #if _LIBCPP_STD_VER > 14
472 template <class ..._Mutexes>
473 class _LIBCPP_TEMPLATE_VIS scoped_lock;
476 class _LIBCPP_TEMPLATE_VIS scoped_lock<> {
478 explicit scoped_lock() {}
479 ~scoped_lock() = default;
481 _LIBCPP_INLINE_VISIBILITY
482 explicit scoped_lock(adopt_lock_t) {}
484 scoped_lock(scoped_lock const&) = delete;
485 scoped_lock& operator=(scoped_lock const&) = delete;
488 template <class _Mutex>
489 class _LIBCPP_TEMPLATE_VIS scoped_lock<_Mutex> {
491 typedef _Mutex mutex_type;
495 explicit scoped_lock(mutex_type & __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
496 : __m_(__m) {__m_.lock();}
498 ~scoped_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}
500 _LIBCPP_INLINE_VISIBILITY
501 explicit scoped_lock(mutex_type& __m, adopt_lock_t) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
505 scoped_lock(scoped_lock const&) = delete;
506 scoped_lock& operator=(scoped_lock const&) = delete;
509 template <class ..._MArgs>
510 class _LIBCPP_TEMPLATE_VIS scoped_lock
512 static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required");
513 typedef tuple<_MArgs&...> _MutexTuple;
516 _LIBCPP_INLINE_VISIBILITY
517 explicit scoped_lock(_MArgs&... __margs)
520 _VSTD::lock(__margs...);
523 _LIBCPP_INLINE_VISIBILITY
524 scoped_lock(_MArgs&... __margs, adopt_lock_t)
529 _LIBCPP_INLINE_VISIBILITY
531 typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
532 __unlock_unpack(_Indices{}, __t_);
535 scoped_lock(scoped_lock const&) = delete;
536 scoped_lock& operator=(scoped_lock const&) = delete;
539 template <size_t ..._Indx>
540 _LIBCPP_INLINE_VISIBILITY
541 static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
542 _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...);
548 #endif // _LIBCPP_STD_VER > 14
549 #endif // !_LIBCPP_HAS_NO_THREADS
551 struct _LIBCPP_TEMPLATE_VIS once_flag;
553 #ifndef _LIBCPP_CXX03_LANG
555 template<class _Callable, class... _Args>
556 _LIBCPP_INLINE_VISIBILITY
557 void call_once(once_flag&, _Callable&&, _Args&&...);
559 #else // _LIBCPP_CXX03_LANG
561 template<class _Callable>
562 _LIBCPP_INLINE_VISIBILITY
563 void call_once(once_flag&, _Callable&);
565 template<class _Callable>
566 _LIBCPP_INLINE_VISIBILITY
567 void call_once(once_flag&, const _Callable&);
569 #endif // _LIBCPP_CXX03_LANG
571 struct _LIBCPP_TEMPLATE_VIS once_flag
573 _LIBCPP_INLINE_VISIBILITY
575 once_flag() _NOEXCEPT : __state_(0) {}
578 once_flag(const once_flag&); // = delete;
579 once_flag& operator=(const once_flag&); // = delete;
581 unsigned long __state_;
583 #ifndef _LIBCPP_CXX03_LANG
584 template<class _Callable, class... _Args>
586 void call_once(once_flag&, _Callable&&, _Args&&...);
587 #else // _LIBCPP_CXX03_LANG
588 template<class _Callable>
590 void call_once(once_flag&, _Callable&);
592 template<class _Callable>
594 void call_once(once_flag&, const _Callable&);
595 #endif // _LIBCPP_CXX03_LANG
598 #ifndef _LIBCPP_CXX03_LANG
601 class __call_once_param
605 _LIBCPP_INLINE_VISIBILITY
606 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
608 _LIBCPP_INLINE_VISIBILITY
611 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
616 template <size_t ..._Indices>
617 _LIBCPP_INLINE_VISIBILITY
618 void __execute(__tuple_indices<_Indices...>)
620 __invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
627 class __call_once_param
631 _LIBCPP_INLINE_VISIBILITY
632 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
634 _LIBCPP_INLINE_VISIBILITY
645 __call_once_proxy(void* __vp)
647 __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
651 _LIBCPP_FUNC_VIS void __call_once(volatile unsigned long&, void*, void(*)(void*));
653 #ifndef _LIBCPP_CXX03_LANG
655 template<class _Callable, class... _Args>
656 inline _LIBCPP_INLINE_VISIBILITY
658 call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
660 if (__libcpp_acquire_load(&__flag.__state_) != ~0ul)
662 typedef tuple<_Callable&&, _Args&&...> _Gp;
663 _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
664 __call_once_param<_Gp> __p(__f);
665 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
669 #else // _LIBCPP_CXX03_LANG
671 template<class _Callable>
672 inline _LIBCPP_INLINE_VISIBILITY
674 call_once(once_flag& __flag, _Callable& __func)
676 if (__libcpp_acquire_load(&__flag.__state_) != ~0ul)
678 __call_once_param<_Callable> __p(__func);
679 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
683 template<class _Callable>
684 inline _LIBCPP_INLINE_VISIBILITY
686 call_once(once_flag& __flag, const _Callable& __func)
688 if (__libcpp_acquire_load(&__flag.__state_) != ~0ul)
690 __call_once_param<const _Callable> __p(__func);
691 __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
695 #endif // _LIBCPP_CXX03_LANG
697 _LIBCPP_END_NAMESPACE_STD
699 #endif // _LIBCPP_MUTEX