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 #if !defined(_LIBCPP_HAS_NO_THREADS) && defined(__minix)
177 #include <__mutex_base>
178 #endif // !defined(_LIBCPP_HAS_NO_THREADS) && defined(__minix)
179 #include <functional>
181 #ifndef _LIBCPP_HAS_NO_VARIADICS
186 #include <__undef_min_max>
188 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
189 #pragma GCC system_header
192 _LIBCPP_BEGIN_NAMESPACE_STD
194 #ifndef _LIBCPP_HAS_NO_THREADS
196 class _LIBCPP_TYPE_VIS recursive_mutex
198 pthread_mutex_t __m_;
205 recursive_mutex(const recursive_mutex&); // = delete;
206 recursive_mutex& operator=(const recursive_mutex&); // = delete;
210 bool try_lock() _NOEXCEPT;
211 void unlock() _NOEXCEPT;
213 typedef pthread_mutex_t* native_handle_type;
214 _LIBCPP_INLINE_VISIBILITY
215 native_handle_type native_handle() {return &__m_;}
218 class _LIBCPP_TYPE_VIS timed_mutex
221 condition_variable __cv_;
228 timed_mutex(const timed_mutex&); // = delete;
229 timed_mutex& operator=(const timed_mutex&); // = delete;
233 bool try_lock() _NOEXCEPT;
234 template <class _Rep, class _Period>
235 _LIBCPP_INLINE_VISIBILITY
236 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
237 {return try_lock_until(chrono::steady_clock::now() + __d);}
238 template <class _Clock, class _Duration>
239 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
240 void unlock() _NOEXCEPT;
243 template <class _Clock, class _Duration>
245 timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
247 using namespace chrono;
248 unique_lock<mutex> __lk(__m_);
249 bool no_timeout = _Clock::now() < __t;
250 while (no_timeout && __locked_)
251 no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
260 class _LIBCPP_TYPE_VIS recursive_timed_mutex
263 condition_variable __cv_;
267 recursive_timed_mutex();
268 ~recursive_timed_mutex();
271 recursive_timed_mutex(const recursive_timed_mutex&); // = delete;
272 recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete;
276 bool try_lock() _NOEXCEPT;
277 template <class _Rep, class _Period>
278 _LIBCPP_INLINE_VISIBILITY
279 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
280 {return try_lock_until(chrono::steady_clock::now() + __d);}
281 template <class _Clock, class _Duration>
282 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
283 void unlock() _NOEXCEPT;
286 template <class _Clock, class _Duration>
288 recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
290 using namespace chrono;
291 pthread_t __id = pthread_self();
292 unique_lock<mutex> lk(__m_);
293 if (pthread_equal(__id, __id_))
295 if (__count_ == numeric_limits<size_t>::max())
300 bool no_timeout = _Clock::now() < __t;
301 while (no_timeout && __count_ != 0)
302 no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout;
312 template <class _L0, class _L1>
314 try_lock(_L0& __l0, _L1& __l1)
316 unique_lock<_L0> __u0(__l0, try_to_lock);
317 if (__u0.owns_lock())
330 #ifndef _LIBCPP_HAS_NO_VARIADICS
332 template <class _L0, class _L1, class _L2, class... _L3>
334 try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
337 unique_lock<_L0> __u0(__l0, try_to_lock);
338 if (__u0.owns_lock())
340 __r = try_lock(__l1, __l2, __l3...);
349 #endif // _LIBCPP_HAS_NO_VARIADICS
351 template <class _L0, class _L1>
353 lock(_L0& __l0, _L1& __l1)
358 unique_lock<_L0> __u0(__l0);
367 unique_lock<_L1> __u1(__l1);
378 #ifndef _LIBCPP_HAS_NO_VARIADICS
380 template <class _L0, class _L1, class _L2, class ..._L3>
382 __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
390 unique_lock<_L0> __u0(__l0);
391 __i = try_lock(__l1, __l2, __l3...);
403 unique_lock<_L1> __u1(__l1);
404 __i = try_lock(__l2, __l3..., __l0);
411 if (__i == sizeof...(_L3) + 1)
418 __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
424 template <class _L0, class _L1, class _L2, class ..._L3>
425 inline _LIBCPP_INLINE_VISIBILITY
427 lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
429 __lock_first(0, __l0, __l1, __l2, __l3...);
432 #endif // _LIBCPP_HAS_NO_VARIADICS
434 #endif // !_LIBCPP_HAS_NO_THREADS
436 struct _LIBCPP_TYPE_VIS_ONLY once_flag;
438 #ifndef _LIBCPP_HAS_NO_VARIADICS
440 template<class _Callable, class... _Args>
441 _LIBCPP_INLINE_VISIBILITY
442 void call_once(once_flag&, _Callable&&, _Args&&...);
444 #else // _LIBCPP_HAS_NO_VARIADICS
446 template<class _Callable>
447 _LIBCPP_INLINE_VISIBILITY
448 void call_once(once_flag&, _Callable&);
450 template<class _Callable>
451 _LIBCPP_INLINE_VISIBILITY
452 void call_once(once_flag&, const _Callable&);
454 #endif // _LIBCPP_HAS_NO_VARIADICS
456 struct _LIBCPP_TYPE_VIS_ONLY once_flag
458 _LIBCPP_INLINE_VISIBILITY
460 once_flag() _NOEXCEPT : __state_(0) {}
463 once_flag(const once_flag&); // = delete;
464 once_flag& operator=(const once_flag&); // = delete;
466 unsigned long __state_;
468 #ifndef _LIBCPP_HAS_NO_VARIADICS
469 template<class _Callable, class... _Args>
471 void call_once(once_flag&, _Callable&&, _Args&&...);
472 #else // _LIBCPP_HAS_NO_VARIADICS
473 template<class _Callable>
475 void call_once(once_flag&, _Callable&);
477 template<class _Callable>
479 void call_once(once_flag&, const _Callable&);
480 #endif // _LIBCPP_HAS_NO_VARIADICS
483 #ifndef _LIBCPP_HAS_NO_VARIADICS
486 class __call_once_param
490 _LIBCPP_INLINE_VISIBILITY
491 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
493 _LIBCPP_INLINE_VISIBILITY
496 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
501 template <size_t ..._Indices>
502 _LIBCPP_INLINE_VISIBILITY
503 void __execute(__tuple_indices<_Indices...>)
505 __invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
512 class __call_once_param
516 _LIBCPP_INLINE_VISIBILITY
517 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
519 _LIBCPP_INLINE_VISIBILITY
530 __call_once_proxy(void* __vp)
532 __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
536 _LIBCPP_FUNC_VIS void __call_once(volatile unsigned long&, void*, void(*)(void*));
538 #ifndef _LIBCPP_HAS_NO_VARIADICS
540 template<class _Callable, class... _Args>
541 inline _LIBCPP_INLINE_VISIBILITY
543 call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
545 if (__libcpp_relaxed_load(&__flag.__state_) != ~0ul)
547 typedef tuple<_Callable&&, _Args&&...> _Gp;
548 _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
549 __call_once_param<_Gp> __p(__f);
550 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
554 #else // _LIBCPP_HAS_NO_VARIADICS
556 template<class _Callable>
557 inline _LIBCPP_INLINE_VISIBILITY
559 call_once(once_flag& __flag, _Callable& __func)
561 if (__libcpp_relaxed_load(&__flag.__state_) != ~0ul)
563 __call_once_param<_Callable> __p(__func);
564 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
568 template<class _Callable>
569 inline _LIBCPP_INLINE_VISIBILITY
571 call_once(once_flag& __flag, const _Callable& __func)
573 if (__flag.__state_ != ~0ul)
575 __call_once_param<const _Callable> __p(__func);
576 __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
580 #endif // _LIBCPP_HAS_NO_VARIADICS
582 _LIBCPP_END_NAMESPACE_STD
584 #endif // _LIBCPP_MUTEX