2 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP___MUTEX_BASE
12 #define _LIBCPP___MUTEX_BASE
16 #include <system_error>
17 #ifndef _LIBCPP_HAS_NO_THREADS
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 #pragma GCC system_header
25 _LIBCPP_BEGIN_NAMESPACE_STD
27 #ifndef _LIBCPP_HAS_NO_THREADS
29 class _LIBCPP_TYPE_VIS mutex
34 _LIBCPP_INLINE_VISIBILITY
35 #ifndef _LIBCPP_HAS_NO_CONSTEXPR
36 constexpr mutex() _NOEXCEPT : __m_(PTHREAD_MUTEX_INITIALIZER) {}
38 mutex() _NOEXCEPT {__m_ = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;}
43 mutex(const mutex&);// = delete;
44 mutex& operator=(const mutex&);// = delete;
48 bool try_lock() _NOEXCEPT;
49 void unlock() _NOEXCEPT;
51 typedef pthread_mutex_t* native_handle_type;
52 _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;}
55 struct _LIBCPP_TYPE_VIS defer_lock_t {};
56 struct _LIBCPP_TYPE_VIS try_to_lock_t {};
57 struct _LIBCPP_TYPE_VIS adopt_lock_t {};
59 #if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MUTEX)
61 extern const defer_lock_t defer_lock;
62 extern const try_to_lock_t try_to_lock;
63 extern const adopt_lock_t adopt_lock;
67 constexpr defer_lock_t defer_lock = defer_lock_t();
68 constexpr try_to_lock_t try_to_lock = try_to_lock_t();
69 constexpr adopt_lock_t adopt_lock = adopt_lock_t();
73 template <class _Mutex>
74 class _LIBCPP_TYPE_VIS_ONLY lock_guard
77 typedef _Mutex mutex_type;
83 _LIBCPP_INLINE_VISIBILITY
84 explicit lock_guard(mutex_type& __m)
85 : __m_(__m) {__m_.lock();}
86 _LIBCPP_INLINE_VISIBILITY
87 lock_guard(mutex_type& __m, adopt_lock_t)
89 _LIBCPP_INLINE_VISIBILITY
90 ~lock_guard() {__m_.unlock();}
93 lock_guard(lock_guard const&);// = delete;
94 lock_guard& operator=(lock_guard const&);// = delete;
97 template <class _Mutex>
98 class _LIBCPP_TYPE_VIS_ONLY unique_lock
101 typedef _Mutex mutex_type;
108 _LIBCPP_INLINE_VISIBILITY
109 unique_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
110 _LIBCPP_INLINE_VISIBILITY
111 explicit unique_lock(mutex_type& __m)
112 : __m_(&__m), __owns_(true) {__m_->lock();}
113 _LIBCPP_INLINE_VISIBILITY
114 unique_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
115 : __m_(&__m), __owns_(false) {}
116 _LIBCPP_INLINE_VISIBILITY
117 unique_lock(mutex_type& __m, try_to_lock_t)
118 : __m_(&__m), __owns_(__m.try_lock()) {}
119 _LIBCPP_INLINE_VISIBILITY
120 unique_lock(mutex_type& __m, adopt_lock_t)
121 : __m_(&__m), __owns_(true) {}
122 template <class _Clock, class _Duration>
123 _LIBCPP_INLINE_VISIBILITY
124 unique_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __t)
125 : __m_(&__m), __owns_(__m.try_lock_until(__t)) {}
126 template <class _Rep, class _Period>
127 _LIBCPP_INLINE_VISIBILITY
128 unique_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __d)
129 : __m_(&__m), __owns_(__m.try_lock_for(__d)) {}
130 _LIBCPP_INLINE_VISIBILITY
138 unique_lock(unique_lock const&); // = delete;
139 unique_lock& operator=(unique_lock const&); // = delete;
142 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
143 _LIBCPP_INLINE_VISIBILITY
144 unique_lock(unique_lock&& __u) _NOEXCEPT
145 : __m_(__u.__m_), __owns_(__u.__owns_)
146 {__u.__m_ = nullptr; __u.__owns_ = false;}
147 _LIBCPP_INLINE_VISIBILITY
148 unique_lock& operator=(unique_lock&& __u) _NOEXCEPT
153 __owns_ = __u.__owns_;
159 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
164 template <class _Rep, class _Period>
165 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d);
166 template <class _Clock, class _Duration>
167 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
171 _LIBCPP_INLINE_VISIBILITY
172 void swap(unique_lock& __u) _NOEXCEPT
174 _VSTD::swap(__m_, __u.__m_);
175 _VSTD::swap(__owns_, __u.__owns_);
177 _LIBCPP_INLINE_VISIBILITY
178 mutex_type* release() _NOEXCEPT
180 mutex_type* __m = __m_;
186 _LIBCPP_INLINE_VISIBILITY
187 bool owns_lock() const _NOEXCEPT {return __owns_;}
188 _LIBCPP_INLINE_VISIBILITY
190 operator bool () const _NOEXCEPT {return __owns_;}
191 _LIBCPP_INLINE_VISIBILITY
192 mutex_type* mutex() const _NOEXCEPT {return __m_;}
195 template <class _Mutex>
197 unique_lock<_Mutex>::lock()
200 __throw_system_error(EPERM, "unique_lock::lock: references null mutex");
202 __throw_system_error(EDEADLK, "unique_lock::lock: already locked");
207 template <class _Mutex>
209 unique_lock<_Mutex>::try_lock()
212 __throw_system_error(EPERM, "unique_lock::try_lock: references null mutex");
214 __throw_system_error(EDEADLK, "unique_lock::try_lock: already locked");
215 __owns_ = __m_->try_lock();
219 template <class _Mutex>
220 template <class _Rep, class _Period>
222 unique_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d)
225 __throw_system_error(EPERM, "unique_lock::try_lock_for: references null mutex");
227 __throw_system_error(EDEADLK, "unique_lock::try_lock_for: already locked");
228 __owns_ = __m_->try_lock_for(__d);
232 template <class _Mutex>
233 template <class _Clock, class _Duration>
235 unique_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
238 __throw_system_error(EPERM, "unique_lock::try_lock_until: references null mutex");
240 __throw_system_error(EDEADLK, "unique_lock::try_lock_until: already locked");
241 __owns_ = __m_->try_lock_until(__t);
245 template <class _Mutex>
247 unique_lock<_Mutex>::unlock()
250 __throw_system_error(EPERM, "unique_lock::unlock: not locked");
255 template <class _Mutex>
256 inline _LIBCPP_INLINE_VISIBILITY
258 swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT
261 //enum class cv_status
262 _LIBCPP_DECLARE_STRONG_ENUM(cv_status)
267 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(cv_status)
269 class _LIBCPP_TYPE_VIS condition_variable
271 pthread_cond_t __cv_;
273 _LIBCPP_INLINE_VISIBILITY
274 #ifndef _LIBCPP_HAS_NO_CONSTEXPR
275 constexpr condition_variable() : __cv_(PTHREAD_COND_INITIALIZER) {}
277 condition_variable() {__cv_ = (pthread_cond_t)PTHREAD_COND_INITIALIZER;}
279 ~condition_variable();
282 condition_variable(const condition_variable&); // = delete;
283 condition_variable& operator=(const condition_variable&); // = delete;
286 void notify_one() _NOEXCEPT;
287 void notify_all() _NOEXCEPT;
289 void wait(unique_lock<mutex>& __lk) _NOEXCEPT;
290 template <class _Predicate>
291 void wait(unique_lock<mutex>& __lk, _Predicate __pred);
293 template <class _Clock, class _Duration>
295 wait_until(unique_lock<mutex>& __lk,
296 const chrono::time_point<_Clock, _Duration>& __t);
298 template <class _Clock, class _Duration, class _Predicate>
300 wait_until(unique_lock<mutex>& __lk,
301 const chrono::time_point<_Clock, _Duration>& __t,
304 template <class _Rep, class _Period>
306 wait_for(unique_lock<mutex>& __lk,
307 const chrono::duration<_Rep, _Period>& __d);
309 template <class _Rep, class _Period, class _Predicate>
311 _LIBCPP_INLINE_VISIBILITY
312 wait_for(unique_lock<mutex>& __lk,
313 const chrono::duration<_Rep, _Period>& __d,
316 typedef pthread_cond_t* native_handle_type;
317 _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__cv_;}
320 void __do_timed_wait(unique_lock<mutex>& __lk,
321 chrono::time_point<chrono::system_clock, chrono::nanoseconds>) _NOEXCEPT;
323 #endif // !_LIBCPP_HAS_NO_THREADS
325 template <class _To, class _Rep, class _Period>
326 inline _LIBCPP_INLINE_VISIBILITY
329 chrono::__is_duration<_To>::value,
332 __ceil(chrono::duration<_Rep, _Period> __d)
334 using namespace chrono;
335 _To __r = duration_cast<_To>(__d);
341 #ifndef _LIBCPP_HAS_NO_THREADS
342 template <class _Predicate>
344 condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred)
350 template <class _Clock, class _Duration>
352 condition_variable::wait_until(unique_lock<mutex>& __lk,
353 const chrono::time_point<_Clock, _Duration>& __t)
355 using namespace chrono;
356 wait_for(__lk, __t - _Clock::now());
357 return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout;
360 template <class _Clock, class _Duration, class _Predicate>
362 condition_variable::wait_until(unique_lock<mutex>& __lk,
363 const chrono::time_point<_Clock, _Duration>& __t,
368 if (wait_until(__lk, __t) == cv_status::timeout)
374 template <class _Rep, class _Period>
376 condition_variable::wait_for(unique_lock<mutex>& __lk,
377 const chrono::duration<_Rep, _Period>& __d)
379 using namespace chrono;
380 if (__d <= __d.zero())
381 return cv_status::timeout;
382 typedef time_point<system_clock, duration<long double, nano> > __sys_tpf;
383 typedef time_point<system_clock, nanoseconds> __sys_tpi;
384 __sys_tpf _Max = __sys_tpi::max();
385 system_clock::time_point __s_now = system_clock::now();
386 steady_clock::time_point __c_now = steady_clock::now();
387 if (_Max - __d > __s_now)
388 __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__d));
390 __do_timed_wait(__lk, __sys_tpi::max());
391 return steady_clock::now() - __c_now < __d ? cv_status::no_timeout :
395 template <class _Rep, class _Period, class _Predicate>
398 condition_variable::wait_for(unique_lock<mutex>& __lk,
399 const chrono::duration<_Rep, _Period>& __d,
402 return wait_until(__lk, chrono::steady_clock::now() + __d,
403 _VSTD::move(__pred));
406 #endif // !_LIBCPP_HAS_NO_THREADS
408 _LIBCPP_END_NAMESPACE_STD
410 #endif // _LIBCPP___MUTEX_BASE