blob: 38a76ac6f2ec90ae50813429260d1bac6815a03f [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP___MUTEX_BASE
12#define _LIBCPP___MUTEX_BASE
13
14#include <__config>
15#include <chrono>
16#include <system_error>
Asiri Rathnayake35ff03b2016-05-06 14:06:2917#include <__threading_support>
Howard Hinnantbc8d3f92010-05-11 19:42:1618
Howard Hinnant08e17472011-10-17 20:05:1019#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:1620#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:1021#endif
Howard Hinnantbc8d3f92010-05-11 19:42:1622
23_LIBCPP_BEGIN_NAMESPACE_STD
24
Jonathan Roelofs8d86b2e2014-09-05 19:45:0525#ifndef _LIBCPP_HAS_NO_THREADS
26
Eric Fiseliera15e8c32016-03-16 02:30:0627#ifndef _LIBCPP_THREAD_SAFETY_ANNOTATION
28# ifdef _LIBCPP_HAS_THREAD_SAFETY_ANNOTATIONS
29# define _LIBCPP_THREAD_SAFETY_ANNOTATION(x) __attribute__((x))
30# else
31# define _LIBCPP_THREAD_SAFETY_ANNOTATION(x)
32# endif
33#endif // _LIBCPP_THREAD_SAFETY_ANNOTATION
34
35class _LIBCPP_TYPE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(capability("mutex")) mutex
Howard Hinnantbc8d3f92010-05-11 19:42:1636{
Marshall Clow65eb1e92016-07-18 17:23:0637#ifndef _LIBCPP_HAS_NO_CONSTEXPR
38 __libcpp_mutex_t __m_ = _LIBCPP_MUTEX_INITIALIZER;
39#else
Asiri Rathnayake35ff03b2016-05-06 14:06:2940 __libcpp_mutex_t __m_;
Marshall Clow65eb1e92016-07-18 17:23:0641#endif
Howard Hinnantbc8d3f92010-05-11 19:42:1642
43public:
Howard Hinnant333f50d2010-09-21 20:16:3744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant384608e2012-07-07 20:01:5245#ifndef _LIBCPP_HAS_NO_CONSTEXPR
Marshall Clow65eb1e92016-07-18 17:23:0646 constexpr mutex() _NOEXCEPT _LIBCPP_DEFAULT
Howard Hinnant384608e2012-07-07 20:01:5247#else
Asiri Rathnayake35ff03b2016-05-06 14:06:2948 mutex() _NOEXCEPT {__m_ = (__libcpp_mutex_t)_LIBCPP_MUTEX_INITIALIZER;}
Howard Hinnant384608e2012-07-07 20:01:5249#endif
Marshall Clow65eb1e92016-07-18 17:23:0650 ~mutex();
Howard Hinnantbc8d3f92010-05-11 19:42:1651
52private:
53 mutex(const mutex&);// = delete;
54 mutex& operator=(const mutex&);// = delete;
55
56public:
Eric Fiseliera15e8c32016-03-16 02:30:0657 void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability());
58 bool try_lock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(try_acquire_capability(true));
59 void unlock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability());
Howard Hinnantbc8d3f92010-05-11 19:42:1660
Asiri Rathnayake35ff03b2016-05-06 14:06:2961 typedef __libcpp_mutex_t* native_handle_type;
Howard Hinnant333f50d2010-09-21 20:16:3762 _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;}
Howard Hinnantbc8d3f92010-05-11 19:42:1663};
64
Howard Hinnant83eade62013-03-06 23:30:1965struct _LIBCPP_TYPE_VIS defer_lock_t {};
66struct _LIBCPP_TYPE_VIS try_to_lock_t {};
67struct _LIBCPP_TYPE_VIS adopt_lock_t {};
Howard Hinnantbc8d3f92010-05-11 19:42:1668
Howard Hinnant499c61f2012-07-21 16:13:0969#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MUTEX)
Howard Hinnantbc8d3f92010-05-11 19:42:1670
Howard Hinnant499c61f2012-07-21 16:13:0971extern const defer_lock_t defer_lock;
72extern const try_to_lock_t try_to_lock;
73extern const adopt_lock_t adopt_lock;
Howard Hinnantbc8d3f92010-05-11 19:42:1674
Howard Hinnant499c61f2012-07-21 16:13:0975#else
76
77constexpr defer_lock_t defer_lock = defer_lock_t();
78constexpr try_to_lock_t try_to_lock = try_to_lock_t();
79constexpr adopt_lock_t adopt_lock = adopt_lock_t();
80
81#endif
Howard Hinnantbc8d3f92010-05-11 19:42:1682
Eric Fiselier10b52a02016-06-14 03:48:0983
84// Forward declare lock_guard as a variadic template even in C++03 to keep
85// the mangling consistent between dialects.
86#if defined(_LIBCPP_ABI_VARIADIC_LOCK_GUARD)
87template <class ..._Mutexes>
88class _LIBCPP_TYPE_VIS_ONLY lock_guard;
89#endif
90
Howard Hinnantbc8d3f92010-05-11 19:42:1691template <class _Mutex>
Eric Fiselier10b52a02016-06-14 03:48:0992class _LIBCPP_TYPE_VIS_ONLY _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable)
93#if !defined(_LIBCPP_ABI_VARIADIC_LOCK_GUARD)
94lock_guard
95#else
96lock_guard<_Mutex>
97#endif
Howard Hinnantbc8d3f92010-05-11 19:42:1698{
99public:
100 typedef _Mutex mutex_type;
101
102private:
103 mutex_type& __m_;
104public:
105
Howard Hinnant333f50d2010-09-21 20:16:37106 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliera15e8c32016-03-16 02:30:06107 explicit lock_guard(mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16108 : __m_(__m) {__m_.lock();}
Howard Hinnant333f50d2010-09-21 20:16:37109 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliera15e8c32016-03-16 02:30:06110 lock_guard(mutex_type& __m, adopt_lock_t) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16111 : __m_(__m) {}
Howard Hinnant333f50d2010-09-21 20:16:37112 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliera15e8c32016-03-16 02:30:06113 ~lock_guard() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}
Howard Hinnantbc8d3f92010-05-11 19:42:16114
115private:
Eric Fiselier10b52a02016-06-14 03:48:09116 lock_guard(lock_guard const&) _LIBCPP_EQUAL_DELETE;
117 lock_guard& operator=(lock_guard const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantbc8d3f92010-05-11 19:42:16118};
119
120template <class _Mutex>
Howard Hinnant0f678bd2013-08-12 18:38:34121class _LIBCPP_TYPE_VIS_ONLY unique_lock
Howard Hinnantbc8d3f92010-05-11 19:42:16122{
123public:
124 typedef _Mutex mutex_type;
125
126private:
127 mutex_type* __m_;
128 bool __owns_;
129
130public:
Howard Hinnant333f50d2010-09-21 20:16:37131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09132 unique_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
Howard Hinnant333f50d2010-09-21 20:16:37133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16134 explicit unique_lock(mutex_type& __m)
Marshall Clow81652492016-04-13 17:02:23135 : __m_(_VSTD::addressof(__m)), __owns_(true) {__m_->lock();}
Howard Hinnant333f50d2010-09-21 20:16:37136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09137 unique_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
Marshall Clow81652492016-04-13 17:02:23138 : __m_(_VSTD::addressof(__m)), __owns_(false) {}
Howard Hinnant333f50d2010-09-21 20:16:37139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16140 unique_lock(mutex_type& __m, try_to_lock_t)
Marshall Clow81652492016-04-13 17:02:23141 : __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock()) {}
Howard Hinnant333f50d2010-09-21 20:16:37142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16143 unique_lock(mutex_type& __m, adopt_lock_t)
Marshall Clow81652492016-04-13 17:02:23144 : __m_(_VSTD::addressof(__m)), __owns_(true) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16145 template <class _Clock, class _Duration>
Howard Hinnant333f50d2010-09-21 20:16:37146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16147 unique_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __t)
Marshall Clow81652492016-04-13 17:02:23148 : __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock_until(__t)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16149 template <class _Rep, class _Period>
Howard Hinnant333f50d2010-09-21 20:16:37150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16151 unique_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __d)
Marshall Clow81652492016-04-13 17:02:23152 : __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock_for(__d)) {}
Howard Hinnant333f50d2010-09-21 20:16:37153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16154 ~unique_lock()
155 {
156 if (__owns_)
157 __m_->unlock();
158 }
159
160private:
161 unique_lock(unique_lock const&); // = delete;
162 unique_lock& operator=(unique_lock const&); // = delete;
163
164public:
Howard Hinnant73d21a42010-09-04 23:28:19165#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant333f50d2010-09-21 20:16:37166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09167 unique_lock(unique_lock&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16168 : __m_(__u.__m_), __owns_(__u.__owns_)
169 {__u.__m_ = nullptr; __u.__owns_ = false;}
Howard Hinnant333f50d2010-09-21 20:16:37170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09171 unique_lock& operator=(unique_lock&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16172 {
173 if (__owns_)
174 __m_->unlock();
175 __m_ = __u.__m_;
176 __owns_ = __u.__owns_;
177 __u.__m_ = nullptr;
178 __u.__owns_ = false;
179 return *this;
180 }
Howard Hinnantac417fa2010-11-28 19:41:07181
Howard Hinnant73d21a42010-09-04 23:28:19182#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16183
184 void lock();
185 bool try_lock();
186
187 template <class _Rep, class _Period>
188 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d);
189 template <class _Clock, class _Duration>
190 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
191
192 void unlock();
193
Howard Hinnant333f50d2010-09-21 20:16:37194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09195 void swap(unique_lock& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16196 {
Howard Hinnant0949eed2011-06-30 21:18:19197 _VSTD::swap(__m_, __u.__m_);
198 _VSTD::swap(__owns_, __u.__owns_);
Howard Hinnantbc8d3f92010-05-11 19:42:16199 }
Howard Hinnant333f50d2010-09-21 20:16:37200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09201 mutex_type* release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16202 {
203 mutex_type* __m = __m_;
204 __m_ = nullptr;
205 __owns_ = false;
206 return __m;
207 }
208
Howard Hinnant333f50d2010-09-21 20:16:37209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09210 bool owns_lock() const _NOEXCEPT {return __owns_;}
Howard Hinnant333f50d2010-09-21 20:16:37211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43212 _LIBCPP_EXPLICIT
Howard Hinnant499c61f2012-07-21 16:13:09213 operator bool () const _NOEXCEPT {return __owns_;}
Howard Hinnant333f50d2010-09-21 20:16:37214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09215 mutex_type* mutex() const _NOEXCEPT {return __m_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16216};
217
218template <class _Mutex>
219void
220unique_lock<_Mutex>::lock()
221{
222 if (__m_ == nullptr)
223 __throw_system_error(EPERM, "unique_lock::lock: references null mutex");
224 if (__owns_)
225 __throw_system_error(EDEADLK, "unique_lock::lock: already locked");
226 __m_->lock();
227 __owns_ = true;
228}
229
230template <class _Mutex>
231bool
232unique_lock<_Mutex>::try_lock()
233{
234 if (__m_ == nullptr)
235 __throw_system_error(EPERM, "unique_lock::try_lock: references null mutex");
236 if (__owns_)
237 __throw_system_error(EDEADLK, "unique_lock::try_lock: already locked");
238 __owns_ = __m_->try_lock();
239 return __owns_;
240}
241
242template <class _Mutex>
243template <class _Rep, class _Period>
244bool
245unique_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d)
246{
247 if (__m_ == nullptr)
248 __throw_system_error(EPERM, "unique_lock::try_lock_for: references null mutex");
249 if (__owns_)
250 __throw_system_error(EDEADLK, "unique_lock::try_lock_for: already locked");
251 __owns_ = __m_->try_lock_for(__d);
252 return __owns_;
253}
254
255template <class _Mutex>
256template <class _Clock, class _Duration>
257bool
258unique_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
259{
260 if (__m_ == nullptr)
261 __throw_system_error(EPERM, "unique_lock::try_lock_until: references null mutex");
262 if (__owns_)
263 __throw_system_error(EDEADLK, "unique_lock::try_lock_until: already locked");
264 __owns_ = __m_->try_lock_until(__t);
265 return __owns_;
266}
267
268template <class _Mutex>
269void
270unique_lock<_Mutex>::unlock()
271{
272 if (!__owns_)
273 __throw_system_error(EPERM, "unique_lock::unlock: not locked");
274 __m_->unlock();
275 __owns_ = false;
276}
277
278template <class _Mutex>
Howard Hinnant333f50d2010-09-21 20:16:37279inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16280void
Howard Hinnant499c61f2012-07-21 16:13:09281swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT
282 {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16283
Marshall Clow239bc422013-12-23 22:14:27284//enum class cv_status
285_LIBCPP_DECLARE_STRONG_ENUM(cv_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16286{
Marshall Clow239bc422013-12-23 22:14:27287 no_timeout,
288 timeout
Howard Hinnantbc8d3f92010-05-11 19:42:16289};
Marshall Clow239bc422013-12-23 22:14:27290_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(cv_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16291
Howard Hinnant83eade62013-03-06 23:30:19292class _LIBCPP_TYPE_VIS condition_variable
Howard Hinnantbc8d3f92010-05-11 19:42:16293{
Marshall Clow65eb1e92016-07-18 17:23:06294#ifndef _LIBCPP_HAS_NO_CONSTEXPR
295 __libcpp_condvar_t __cv_ = _LIBCPP_CONDVAR_INITIALIZER;
296#else
297 __libcpp_condvar_t __cv_;
298#endif
299
Howard Hinnantbc8d3f92010-05-11 19:42:16300public:
Howard Hinnant333f50d2010-09-21 20:16:37301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant384608e2012-07-07 20:01:52302#ifndef _LIBCPP_HAS_NO_CONSTEXPR
Marshall Clow65eb1e92016-07-18 17:23:06303 constexpr condition_variable() _NOEXCEPT _LIBCPP_DEFAULT
Howard Hinnant384608e2012-07-07 20:01:52304#else
Marshall Clow65eb1e92016-07-18 17:23:06305 condition_variable() _NOEXCEPT {__cv_ = (__libcpp_condvar_t)_LIBCPP_CONDVAR_INITIALIZER;}
Howard Hinnant384608e2012-07-07 20:01:52306#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16307 ~condition_variable();
308
309private:
310 condition_variable(const condition_variable&); // = delete;
311 condition_variable& operator=(const condition_variable&); // = delete;
312
313public:
Howard Hinnantc8f74132012-07-21 16:32:53314 void notify_one() _NOEXCEPT;
315 void notify_all() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16316
Marshall Clowb0767852014-03-26 02:45:04317 void wait(unique_lock<mutex>& __lk) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16318 template <class _Predicate>
319 void wait(unique_lock<mutex>& __lk, _Predicate __pred);
320
Howard Hinnantbc8d3f92010-05-11 19:42:16321 template <class _Clock, class _Duration>
322 cv_status
323 wait_until(unique_lock<mutex>& __lk,
324 const chrono::time_point<_Clock, _Duration>& __t);
325
326 template <class _Clock, class _Duration, class _Predicate>
327 bool
328 wait_until(unique_lock<mutex>& __lk,
329 const chrono::time_point<_Clock, _Duration>& __t,
330 _Predicate __pred);
331
332 template <class _Rep, class _Period>
333 cv_status
334 wait_for(unique_lock<mutex>& __lk,
335 const chrono::duration<_Rep, _Period>& __d);
336
337 template <class _Rep, class _Period, class _Predicate>
338 bool
Evgeniy Stepanova3b25f82015-11-07 01:22:13339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16340 wait_for(unique_lock<mutex>& __lk,
341 const chrono::duration<_Rep, _Period>& __d,
342 _Predicate __pred);
343
Asiri Rathnayake35ff03b2016-05-06 14:06:29344 typedef __libcpp_condvar_t* native_handle_type;
Howard Hinnant333f50d2010-09-21 20:16:37345 _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__cv_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16346
347private:
348 void __do_timed_wait(unique_lock<mutex>& __lk,
Marshall Clowb0767852014-03-26 02:45:04349 chrono::time_point<chrono::system_clock, chrono::nanoseconds>) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16350};
Jonathan Roelofs8d86b2e2014-09-05 19:45:05351#endif // !_LIBCPP_HAS_NO_THREADS
Howard Hinnantbc8d3f92010-05-11 19:42:16352
353template <class _To, class _Rep, class _Period>
Howard Hinnant333f50d2010-09-21 20:16:37354inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16355typename enable_if
356<
357 chrono::__is_duration<_To>::value,
358 _To
359>::type
360__ceil(chrono::duration<_Rep, _Period> __d)
361{
362 using namespace chrono;
363 _To __r = duration_cast<_To>(__d);
364 if (__r < __d)
365 ++__r;
366 return __r;
367}
368
Jonathan Roelofs8d86b2e2014-09-05 19:45:05369#ifndef _LIBCPP_HAS_NO_THREADS
Howard Hinnantbc8d3f92010-05-11 19:42:16370template <class _Predicate>
371void
372condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred)
373{
374 while (!__pred())
375 wait(__lk);
376}
377
Howard Hinnantbc8d3f92010-05-11 19:42:16378template <class _Clock, class _Duration>
379cv_status
380condition_variable::wait_until(unique_lock<mutex>& __lk,
381 const chrono::time_point<_Clock, _Duration>& __t)
382{
383 using namespace chrono;
Howard Hinnantcf115d22012-08-30 19:14:33384 wait_for(__lk, __t - _Clock::now());
Howard Hinnantbc8d3f92010-05-11 19:42:16385 return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout;
386}
387
388template <class _Clock, class _Duration, class _Predicate>
389bool
390condition_variable::wait_until(unique_lock<mutex>& __lk,
391 const chrono::time_point<_Clock, _Duration>& __t,
392 _Predicate __pred)
393{
394 while (!__pred())
395 {
396 if (wait_until(__lk, __t) == cv_status::timeout)
397 return __pred();
398 }
399 return true;
400}
401
402template <class _Rep, class _Period>
403cv_status
404condition_variable::wait_for(unique_lock<mutex>& __lk,
405 const chrono::duration<_Rep, _Period>& __d)
406{
407 using namespace chrono;
Howard Hinnantcf115d22012-08-30 19:14:33408 if (__d <= __d.zero())
409 return cv_status::timeout;
410 typedef time_point<system_clock, duration<long double, nano> > __sys_tpf;
411 typedef time_point<system_clock, nanoseconds> __sys_tpi;
412 __sys_tpf _Max = __sys_tpi::max();
Howard Hinnantf8f85212010-11-20 19:16:30413 system_clock::time_point __s_now = system_clock::now();
414 steady_clock::time_point __c_now = steady_clock::now();
Howard Hinnantcf115d22012-08-30 19:14:33415 if (_Max - __d > __s_now)
416 __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__d));
417 else
418 __do_timed_wait(__lk, __sys_tpi::max());
Howard Hinnantf8f85212010-11-20 19:16:30419 return steady_clock::now() - __c_now < __d ? cv_status::no_timeout :
420 cv_status::timeout;
Howard Hinnantbc8d3f92010-05-11 19:42:16421}
422
423template <class _Rep, class _Period, class _Predicate>
Evgeniy Stepanova3b25f82015-11-07 01:22:13424inline
Howard Hinnantbc8d3f92010-05-11 19:42:16425bool
426condition_variable::wait_for(unique_lock<mutex>& __lk,
427 const chrono::duration<_Rep, _Period>& __d,
428 _Predicate __pred)
429{
Howard Hinnantf8f85212010-11-20 19:16:30430 return wait_until(__lk, chrono::steady_clock::now() + __d,
Howard Hinnant0949eed2011-06-30 21:18:19431 _VSTD::move(__pred));
Howard Hinnantbc8d3f92010-05-11 19:42:16432}
433
Jonathan Roelofs8d86b2e2014-09-05 19:45:05434#endif // !_LIBCPP_HAS_NO_THREADS
435
Howard Hinnantbc8d3f92010-05-11 19:42:16436_LIBCPP_END_NAMESPACE_STD
437
438#endif // _LIBCPP___MUTEX_BASE