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 Mutex>
116 typedef Mutex mutex_type;
117 unique_lock() noexcept;
118 explicit unique_lock(mutex_type& m);
119 unique_lock(mutex_type& m, defer_lock_t) noexcept;
120 unique_lock(mutex_type& m, try_to_lock_t);
121 unique_lock(mutex_type& m, adopt_lock_t);
122 template <class Clock, class Duration>
123 unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
124 template <class Rep, class Period>
125 unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
128 unique_lock(unique_lock const&) = delete;
129 unique_lock& operator=(unique_lock const&) = delete;
131 unique_lock(unique_lock&& u) noexcept;
132 unique_lock& operator=(unique_lock&& u) noexcept;
137 template <class Rep, class Period>
138 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
139 template <class Clock, class Duration>
140 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
144 void swap(unique_lock& u) noexcept;
145 mutex_type* release() noexcept;
147 bool owns_lock() const noexcept;
148 explicit operator bool () const noexcept;
149 mutex_type* mutex() const noexcept;
152 template <class Mutex>
153 void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
155 template <class L1, class L2, class... L3>
156 int try_lock(L1&, L2&, L3&...);
157 template <class L1, class L2, class... L3>
158 void lock(L1&, L2&, L3&...);
162 constexpr once_flag() noexcept;
164 once_flag(const once_flag&) = delete;
165 once_flag& operator=(const once_flag&) = delete;
168 template<class Callable, class ...Args>
169 void call_once(once_flag& flag, Callable&& func, Args&&... args);
176 #include <__mutex_base>
177 #include <functional>
179 #ifndef _LIBCPP_HAS_NO_VARIADICS
184 #include <__undef_min_max>
186 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
187 #pragma GCC system_header
190 _LIBCPP_BEGIN_NAMESPACE_STD
192 #ifndef _LIBCPP_HAS_NO_THREADS
194 class _LIBCPP_TYPE_VIS recursive_mutex
196 pthread_mutex_t __m_;
203 recursive_mutex(const recursive_mutex&); // = delete;
204 recursive_mutex& operator=(const recursive_mutex&); // = delete;
208 bool try_lock() _NOEXCEPT;
209 void unlock() _NOEXCEPT;
211 typedef pthread_mutex_t* native_handle_type;
212 _LIBCPP_INLINE_VISIBILITY
213 native_handle_type native_handle() {return &__m_;}
216 class _LIBCPP_TYPE_VIS timed_mutex
219 condition_variable __cv_;
226 timed_mutex(const timed_mutex&); // = delete;
227 timed_mutex& operator=(const timed_mutex&); // = delete;
231 bool try_lock() _NOEXCEPT;
232 template <class _Rep, class _Period>
233 _LIBCPP_INLINE_VISIBILITY
234 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
235 {return try_lock_until(chrono::steady_clock::now() + __d);}
236 template <class _Clock, class _Duration>
237 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
238 void unlock() _NOEXCEPT;
241 template <class _Clock, class _Duration>
243 timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
245 using namespace chrono;
246 unique_lock<mutex> __lk(__m_);
247 bool no_timeout = _Clock::now() < __t;
248 while (no_timeout && __locked_)
249 no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
258 class _LIBCPP_TYPE_VIS recursive_timed_mutex
261 condition_variable __cv_;
265 recursive_timed_mutex();
266 ~recursive_timed_mutex();
269 recursive_timed_mutex(const recursive_timed_mutex&); // = delete;
270 recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete;
274 bool try_lock() _NOEXCEPT;
275 template <class _Rep, class _Period>
276 _LIBCPP_INLINE_VISIBILITY
277 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
278 {return try_lock_until(chrono::steady_clock::now() + __d);}
279 template <class _Clock, class _Duration>
280 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
281 void unlock() _NOEXCEPT;
284 template <class _Clock, class _Duration>
286 recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
288 using namespace chrono;
289 pthread_t __id = pthread_self();
290 unique_lock<mutex> lk(__m_);
291 if (pthread_equal(__id, __id_))
293 if (__count_ == numeric_limits<size_t>::max())
298 bool no_timeout = _Clock::now() < __t;
299 while (no_timeout && __count_ != 0)
300 no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout;
310 template <class _L0, class _L1>
312 try_lock(_L0& __l0, _L1& __l1)
314 unique_lock<_L0> __u0(__l0, try_to_lock);
315 if (__u0.owns_lock())
328 #ifndef _LIBCPP_HAS_NO_VARIADICS
330 template <class _L0, class _L1, class _L2, class... _L3>
332 try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
335 unique_lock<_L0> __u0(__l0, try_to_lock);
336 if (__u0.owns_lock())
338 __r = try_lock(__l1, __l2, __l3...);
347 #endif // _LIBCPP_HAS_NO_VARIADICS
349 template <class _L0, class _L1>
351 lock(_L0& __l0, _L1& __l1)
356 unique_lock<_L0> __u0(__l0);
365 unique_lock<_L1> __u1(__l1);
376 #ifndef _LIBCPP_HAS_NO_VARIADICS
378 template <class _L0, class _L1, class _L2, class ..._L3>
380 __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
388 unique_lock<_L0> __u0(__l0);
389 __i = try_lock(__l1, __l2, __l3...);
401 unique_lock<_L1> __u1(__l1);
402 __i = try_lock(__l2, __l3..., __l0);
409 if (__i == sizeof...(_L3) + 1)
416 __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
422 template <class _L0, class _L1, class _L2, class ..._L3>
423 inline _LIBCPP_INLINE_VISIBILITY
425 lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
427 __lock_first(0, __l0, __l1, __l2, __l3...);
430 #endif // _LIBCPP_HAS_NO_VARIADICS
432 #endif // !_LIBCPP_HAS_NO_THREADS
434 struct _LIBCPP_TYPE_VIS_ONLY once_flag;
436 #ifndef _LIBCPP_HAS_NO_VARIADICS
438 template<class _Callable, class... _Args>
439 _LIBCPP_INLINE_VISIBILITY
440 void call_once(once_flag&, _Callable&&, _Args&&...);
442 #else // _LIBCPP_HAS_NO_VARIADICS
444 template<class _Callable>
445 _LIBCPP_INLINE_VISIBILITY
446 void call_once(once_flag&, _Callable&);
448 template<class _Callable>
449 _LIBCPP_INLINE_VISIBILITY
450 void call_once(once_flag&, const _Callable&);
452 #endif // _LIBCPP_HAS_NO_VARIADICS
454 struct _LIBCPP_TYPE_VIS_ONLY once_flag
456 _LIBCPP_INLINE_VISIBILITY
458 once_flag() _NOEXCEPT : __state_(0) {}
461 once_flag(const once_flag&); // = delete;
462 once_flag& operator=(const once_flag&); // = delete;
464 unsigned long __state_;
466 #ifndef _LIBCPP_HAS_NO_VARIADICS
467 template<class _Callable, class... _Args>
469 void call_once(once_flag&, _Callable&&, _Args&&...);
470 #else // _LIBCPP_HAS_NO_VARIADICS
471 template<class _Callable>
473 void call_once(once_flag&, _Callable&);
475 template<class _Callable>
477 void call_once(once_flag&, const _Callable&);
478 #endif // _LIBCPP_HAS_NO_VARIADICS
481 #ifndef _LIBCPP_HAS_NO_VARIADICS
484 class __call_once_param
488 _LIBCPP_INLINE_VISIBILITY
489 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
491 _LIBCPP_INLINE_VISIBILITY
494 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
499 template <size_t ..._Indices>
500 _LIBCPP_INLINE_VISIBILITY
501 void __execute(__tuple_indices<_Indices...>)
503 __invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
510 class __call_once_param
514 _LIBCPP_INLINE_VISIBILITY
515 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
517 _LIBCPP_INLINE_VISIBILITY
528 __call_once_proxy(void* __vp)
530 __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
534 _LIBCPP_FUNC_VIS void __call_once(volatile unsigned long&, void*, void(*)(void*));
536 #ifndef _LIBCPP_HAS_NO_VARIADICS
538 template<class _Callable, class... _Args>
539 inline _LIBCPP_INLINE_VISIBILITY
541 call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
543 if (__libcpp_relaxed_load(&__flag.__state_) != ~0ul)
545 typedef tuple<_Callable&&, _Args&&...> _Gp;
546 _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
547 __call_once_param<_Gp> __p(__f);
548 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
552 #else // _LIBCPP_HAS_NO_VARIADICS
554 template<class _Callable>
555 inline _LIBCPP_INLINE_VISIBILITY
557 call_once(once_flag& __flag, _Callable& __func)
559 if (__libcpp_relaxed_load(&__flag.__state_) != ~0ul)
561 __call_once_param<_Callable> __p(__func);
562 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
566 template<class _Callable>
567 inline _LIBCPP_INLINE_VISIBILITY
569 call_once(once_flag& __flag, const _Callable& __func)
571 if (__flag.__state_ != ~0ul)
573 __call_once_param<const _Callable> __p(__func);
574 __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
578 #endif // _LIBCPP_HAS_NO_VARIADICS
580 _LIBCPP_END_NAMESPACE_STD
582 #endif // _LIBCPP_MUTEX