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> // Variadic lock_guard only provided in ABI V2.
116 explicit lock_guard(MutexTypes&... m);
117 lock_guard(MutexTypes&... m, adopt_lock_t);
119 lock_guard(lock_guard const&) = delete;
120 lock_guard& operator=(lock_guard const&) = delete;
122 tuple<MutexTypes&...> pm; // exposition only
125 template <class Mutex>
129 typedef Mutex mutex_type;
130 unique_lock() noexcept;
131 explicit unique_lock(mutex_type& m);
132 unique_lock(mutex_type& m, defer_lock_t) noexcept;
133 unique_lock(mutex_type& m, try_to_lock_t);
134 unique_lock(mutex_type& m, adopt_lock_t);
135 template <class Clock, class Duration>
136 unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
137 template <class Rep, class Period>
138 unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
141 unique_lock(unique_lock const&) = delete;
142 unique_lock& operator=(unique_lock const&) = delete;
144 unique_lock(unique_lock&& u) noexcept;
145 unique_lock& operator=(unique_lock&& u) noexcept;
150 template <class Rep, class Period>
151 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
152 template <class Clock, class Duration>
153 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
157 void swap(unique_lock& u) noexcept;
158 mutex_type* release() noexcept;
160 bool owns_lock() const noexcept;
161 explicit operator bool () const noexcept;
162 mutex_type* mutex() const noexcept;
165 template <class Mutex>
166 void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
168 template <class L1, class L2, class... L3>
169 int try_lock(L1&, L2&, L3&...);
170 template <class L1, class L2, class... L3>
171 void lock(L1&, L2&, L3&...);
175 constexpr once_flag() noexcept;
177 once_flag(const once_flag&) = delete;
178 once_flag& operator=(const once_flag&) = delete;
181 template<class Callable, class ...Args>
182 void call_once(once_flag& flag, Callable&& func, Args&&... args);
189 #include <__mutex_base>
190 #include <functional>
192 #ifndef _LIBCPP_HAS_NO_VARIADICS
195 #include <__threading_support>
197 #include <__undef_min_max>
199 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
200 #pragma GCC system_header
203 _LIBCPP_BEGIN_NAMESPACE_STD
205 #ifndef _LIBCPP_HAS_NO_THREADS
207 class _LIBCPP_TYPE_VIS recursive_mutex
209 __libcpp_recursive_mutex_t __m_;
216 recursive_mutex(const recursive_mutex&); // = delete;
217 recursive_mutex& operator=(const recursive_mutex&); // = delete;
221 bool try_lock() _NOEXCEPT;
222 void unlock() _NOEXCEPT;
224 typedef __libcpp_recursive_mutex_t* native_handle_type;
226 _LIBCPP_INLINE_VISIBILITY
227 native_handle_type native_handle() {return &__m_;}
230 class _LIBCPP_TYPE_VIS timed_mutex
233 condition_variable __cv_;
240 timed_mutex(const timed_mutex&); // = delete;
241 timed_mutex& operator=(const timed_mutex&); // = delete;
245 bool try_lock() _NOEXCEPT;
246 template <class _Rep, class _Period>
247 _LIBCPP_INLINE_VISIBILITY
248 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
249 {return try_lock_until(chrono::steady_clock::now() + __d);}
250 template <class _Clock, class _Duration>
251 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
252 void unlock() _NOEXCEPT;
255 template <class _Clock, class _Duration>
257 timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
259 using namespace chrono;
260 unique_lock<mutex> __lk(__m_);
261 bool no_timeout = _Clock::now() < __t;
262 while (no_timeout && __locked_)
263 no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
272 class _LIBCPP_TYPE_VIS recursive_timed_mutex
275 condition_variable __cv_;
277 __libcpp_thread_id __id_;
279 recursive_timed_mutex();
280 ~recursive_timed_mutex();
283 recursive_timed_mutex(const recursive_timed_mutex&); // = delete;
284 recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete;
288 bool try_lock() _NOEXCEPT;
289 template <class _Rep, class _Period>
290 _LIBCPP_INLINE_VISIBILITY
291 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
292 {return try_lock_until(chrono::steady_clock::now() + __d);}
293 template <class _Clock, class _Duration>
294 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
295 void unlock() _NOEXCEPT;
298 template <class _Clock, class _Duration>
300 recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
302 using namespace chrono;
303 __libcpp_thread_id __id = __libcpp_thread_get_current_id();
304 unique_lock<mutex> lk(__m_);
305 if (__libcpp_thread_id_equal(__id, __id_))
307 if (__count_ == numeric_limits<size_t>::max())
312 bool no_timeout = _Clock::now() < __t;
313 while (no_timeout && __count_ != 0)
314 no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout;
324 template <class _L0, class _L1>
326 try_lock(_L0& __l0, _L1& __l1)
328 unique_lock<_L0> __u0(__l0, try_to_lock);
329 if (__u0.owns_lock())
342 #ifndef _LIBCPP_HAS_NO_VARIADICS
344 template <class _L0, class _L1, class _L2, class... _L3>
346 try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
349 unique_lock<_L0> __u0(__l0, try_to_lock);
350 if (__u0.owns_lock())
352 __r = try_lock(__l1, __l2, __l3...);
361 #endif // _LIBCPP_HAS_NO_VARIADICS
363 template <class _L0, class _L1>
365 lock(_L0& __l0, _L1& __l1)
370 unique_lock<_L0> __u0(__l0);
377 __libcpp_thread_yield();
379 unique_lock<_L1> __u1(__l1);
386 __libcpp_thread_yield();
390 #ifndef _LIBCPP_HAS_NO_VARIADICS
392 template <class _L0, class _L1, class _L2, class ..._L3>
394 __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
402 unique_lock<_L0> __u0(__l0);
403 __i = try_lock(__l1, __l2, __l3...);
411 __libcpp_thread_yield();
415 unique_lock<_L1> __u1(__l1);
416 __i = try_lock(__l2, __l3..., __l0);
423 if (__i == sizeof...(_L3) + 1)
427 __libcpp_thread_yield();
430 __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
436 template <class _L0, class _L1, class _L2, class ..._L3>
437 inline _LIBCPP_INLINE_VISIBILITY
439 lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
441 __lock_first(0, __l0, __l1, __l2, __l3...);
445 inline _LIBCPP_INLINE_VISIBILITY
446 void __unlock(_L0& __l0) {
450 template <class _L0, class _L1>
451 inline _LIBCPP_INLINE_VISIBILITY
452 void __unlock(_L0& __l0, _L1& __l1) {
457 template <class _L0, class _L1, class _L2, class ..._L3>
458 inline _LIBCPP_INLINE_VISIBILITY
459 void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
462 _VSTD::__unlock(__l2, __l3...);
465 #endif // _LIBCPP_HAS_NO_VARIADICS
467 #endif // !_LIBCPP_HAS_NO_THREADS
469 struct _LIBCPP_TEMPLATE_VIS once_flag;
471 #ifndef _LIBCPP_HAS_NO_VARIADICS
473 template<class _Callable, class... _Args>
474 _LIBCPP_INLINE_VISIBILITY
475 void call_once(once_flag&, _Callable&&, _Args&&...);
477 #else // _LIBCPP_HAS_NO_VARIADICS
479 template<class _Callable>
480 _LIBCPP_INLINE_VISIBILITY
481 void call_once(once_flag&, _Callable&);
483 template<class _Callable>
484 _LIBCPP_INLINE_VISIBILITY
485 void call_once(once_flag&, const _Callable&);
487 #endif // _LIBCPP_HAS_NO_VARIADICS
489 struct _LIBCPP_TEMPLATE_VIS once_flag
491 _LIBCPP_INLINE_VISIBILITY
493 once_flag() _NOEXCEPT : __state_(0) {}
496 once_flag(const once_flag&); // = delete;
497 once_flag& operator=(const once_flag&); // = delete;
499 unsigned long __state_;
501 #ifndef _LIBCPP_HAS_NO_VARIADICS
502 template<class _Callable, class... _Args>
504 void call_once(once_flag&, _Callable&&, _Args&&...);
505 #else // _LIBCPP_HAS_NO_VARIADICS
506 template<class _Callable>
508 void call_once(once_flag&, _Callable&);
510 template<class _Callable>
512 void call_once(once_flag&, const _Callable&);
513 #endif // _LIBCPP_HAS_NO_VARIADICS
516 #ifndef _LIBCPP_HAS_NO_VARIADICS
519 class __call_once_param
523 _LIBCPP_INLINE_VISIBILITY
524 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
526 _LIBCPP_INLINE_VISIBILITY
529 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
534 template <size_t ..._Indices>
535 _LIBCPP_INLINE_VISIBILITY
536 void __execute(__tuple_indices<_Indices...>)
538 __invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
545 class __call_once_param
549 _LIBCPP_INLINE_VISIBILITY
550 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
552 _LIBCPP_INLINE_VISIBILITY
562 inline _LIBCPP_INLINE_VISIBILITY
564 __call_once_proxy(void* __vp)
566 __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
570 _LIBCPP_FUNC_VIS void __call_once(volatile unsigned long&, void*, void(*)(void*));
572 #ifndef _LIBCPP_HAS_NO_VARIADICS
574 template<class _Callable, class... _Args>
575 inline _LIBCPP_INLINE_VISIBILITY
577 call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
579 if (__libcpp_acquire_load(&__flag.__state_) != ~0ul)
581 typedef tuple<_Callable&&, _Args&&...> _Gp;
582 _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
583 __call_once_param<_Gp> __p(__f);
584 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
588 #else // _LIBCPP_HAS_NO_VARIADICS
590 template<class _Callable>
591 inline _LIBCPP_INLINE_VISIBILITY
593 call_once(once_flag& __flag, _Callable& __func)
595 if (__libcpp_acquire_load(&__flag.__state_) != ~0ul)
597 __call_once_param<_Callable> __p(__func);
598 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
602 template<class _Callable>
603 inline _LIBCPP_INLINE_VISIBILITY
605 call_once(once_flag& __flag, const _Callable& __func)
607 if (__flag.__state_ != ~0ul)
609 __call_once_param<const _Callable> __p(__func);
610 __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
614 #endif // _LIBCPP_HAS_NO_VARIADICS
617 #if defined(_LIBCPP_ABI_VARIADIC_LOCK_GUARD) \
618 && !defined(_LIBCPP_CXX03_LANG)
620 class _LIBCPP_TEMPLATE_VIS lock_guard<> {
622 explicit lock_guard() {}
623 ~lock_guard() = default;
625 _LIBCPP_INLINE_VISIBILITY
626 explicit lock_guard(adopt_lock_t) {}
628 lock_guard(lock_guard const&) = delete;
629 lock_guard& operator=(lock_guard const&) = delete;
632 template <class ..._MArgs>
633 class _LIBCPP_TEMPLATE_VIS lock_guard
635 static_assert(sizeof...(_MArgs) >= 2, "At least 2 lock types required");
636 typedef tuple<_MArgs&...> _MutexTuple;
639 _LIBCPP_INLINE_VISIBILITY
640 explicit lock_guard(_MArgs&... __margs)
643 _VSTD::lock(__margs...);
646 _LIBCPP_INLINE_VISIBILITY
647 lock_guard(_MArgs&... __margs, adopt_lock_t)
652 _LIBCPP_INLINE_VISIBILITY
654 typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
655 __unlock_unpack(_Indices{}, __t_);
658 lock_guard(lock_guard const&) = delete;
659 lock_guard& operator=(lock_guard const&) = delete;
662 template <size_t ..._Indx>
663 _LIBCPP_INLINE_VISIBILITY
664 static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
665 _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...);
671 #endif // _LIBCPP_ABI_VARIADIC_LOCK_GUARD
673 _LIBCPP_END_NAMESPACE_STD
675 #endif // _LIBCPP_MUTEX