blob: 71d0fa2e846d0dde3f2ad9f46fc4b1d845a8239e [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00005//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP___MUTEX_BASE
12#define _LIBCPP___MUTEX_BASE
13
14#include <__config>
15#include <chrono>
16#include <system_error>
Jonathan Roelofs643e0ab2015-08-27 17:47:34 +000017#ifndef _LIBCPP_HAS_NO_THREADS
Howard Hinnant3e519522010-05-11 19:42:16 +000018#include <pthread.h>
Jonathan Roelofs643e0ab2015-08-27 17:47:34 +000019#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000020
Howard Hinnant073458b2011-10-17 20:05:10 +000021#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +000022#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +000023#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000024
25_LIBCPP_BEGIN_NAMESPACE_STD
26
Jonathan Roelofsb3fcc672014-09-05 19:45:05 +000027#ifndef _LIBCPP_HAS_NO_THREADS
28
Eric Fiselier7865b2e2016-03-16 02:30:06 +000029#ifndef _LIBCPP_THREAD_SAFETY_ANNOTATION
30# ifdef _LIBCPP_HAS_THREAD_SAFETY_ANNOTATIONS
31# define _LIBCPP_THREAD_SAFETY_ANNOTATION(x) __attribute__((x))
32# else
33# define _LIBCPP_THREAD_SAFETY_ANNOTATION(x)
34# endif
35#endif // _LIBCPP_THREAD_SAFETY_ANNOTATION
36
37class _LIBCPP_TYPE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(capability("mutex")) mutex
Howard Hinnant3e519522010-05-11 19:42:16 +000038{
39 pthread_mutex_t __m_;
40
41public:
Howard Hinnantf5ab7032010-09-21 20:16:37 +000042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbfa79902012-07-07 20:01:52 +000043#ifndef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantab303f72012-09-11 16:10:20 +000044 constexpr mutex() _NOEXCEPT : __m_(PTHREAD_MUTEX_INITIALIZER) {}
Howard Hinnantbfa79902012-07-07 20:01:52 +000045#else
Howard Hinnant02e610e2012-07-21 16:13:09 +000046 mutex() _NOEXCEPT {__m_ = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;}
Howard Hinnantbfa79902012-07-07 20:01:52 +000047#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000048 ~mutex();
49
50private:
51 mutex(const mutex&);// = delete;
52 mutex& operator=(const mutex&);// = delete;
53
54public:
Eric Fiselier7865b2e2016-03-16 02:30:06 +000055 void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability());
56 bool try_lock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(try_acquire_capability(true));
57 void unlock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability());
Howard Hinnant3e519522010-05-11 19:42:16 +000058
59 typedef pthread_mutex_t* native_handle_type;
Howard Hinnantf5ab7032010-09-21 20:16:37 +000060 _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;}
Howard Hinnant3e519522010-05-11 19:42:16 +000061};
62
Howard Hinnant6e412562013-03-06 23:30:19 +000063struct _LIBCPP_TYPE_VIS defer_lock_t {};
64struct _LIBCPP_TYPE_VIS try_to_lock_t {};
65struct _LIBCPP_TYPE_VIS adopt_lock_t {};
Howard Hinnant3e519522010-05-11 19:42:16 +000066
Howard Hinnant02e610e2012-07-21 16:13:09 +000067#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MUTEX)
Howard Hinnant3e519522010-05-11 19:42:16 +000068
Howard Hinnant02e610e2012-07-21 16:13:09 +000069extern const defer_lock_t defer_lock;
70extern const try_to_lock_t try_to_lock;
71extern const adopt_lock_t adopt_lock;
Howard Hinnant3e519522010-05-11 19:42:16 +000072
Howard Hinnant02e610e2012-07-21 16:13:09 +000073#else
74
75constexpr defer_lock_t defer_lock = defer_lock_t();
76constexpr try_to_lock_t try_to_lock = try_to_lock_t();
77constexpr adopt_lock_t adopt_lock = adopt_lock_t();
78
79#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000080
81template <class _Mutex>
Eric Fiselier7865b2e2016-03-16 02:30:06 +000082class _LIBCPP_TYPE_VIS_ONLY _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) lock_guard
Howard Hinnant3e519522010-05-11 19:42:16 +000083{
84public:
85 typedef _Mutex mutex_type;
86
87private:
88 mutex_type& __m_;
89public:
90
Howard Hinnantf5ab7032010-09-21 20:16:37 +000091 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7865b2e2016-03-16 02:30:06 +000092 explicit lock_guard(mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
Howard Hinnant3e519522010-05-11 19:42:16 +000093 : __m_(__m) {__m_.lock();}
Howard Hinnantf5ab7032010-09-21 20:16:37 +000094 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7865b2e2016-03-16 02:30:06 +000095 lock_guard(mutex_type& __m, adopt_lock_t) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
Howard Hinnant3e519522010-05-11 19:42:16 +000096 : __m_(__m) {}
Howard Hinnantf5ab7032010-09-21 20:16:37 +000097 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7865b2e2016-03-16 02:30:06 +000098 ~lock_guard() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}
Howard Hinnant3e519522010-05-11 19:42:16 +000099
100private:
101 lock_guard(lock_guard const&);// = delete;
102 lock_guard& operator=(lock_guard const&);// = delete;
103};
104
105template <class _Mutex>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000106class _LIBCPP_TYPE_VIS_ONLY unique_lock
Howard Hinnant3e519522010-05-11 19:42:16 +0000107{
108public:
109 typedef _Mutex mutex_type;
110
111private:
112 mutex_type* __m_;
113 bool __owns_;
114
115public:
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000116 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant02e610e2012-07-21 16:13:09 +0000117 unique_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000119 explicit unique_lock(mutex_type& __m)
Marshall Clow0b54e792016-03-14 23:07:32 +0000120 : __m_(addressof(__m)), __owns_(true) {__m_->lock();}
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant02e610e2012-07-21 16:13:09 +0000122 unique_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
Marshall Clow0b54e792016-03-14 23:07:32 +0000123 : __m_(addressof(__m)), __owns_(false) {}
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000125 unique_lock(mutex_type& __m, try_to_lock_t)
Marshall Clow0b54e792016-03-14 23:07:32 +0000126 : __m_(addressof(__m)), __owns_(__m.try_lock()) {}
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000128 unique_lock(mutex_type& __m, adopt_lock_t)
Marshall Clow0b54e792016-03-14 23:07:32 +0000129 : __m_(addressof(__m)), __owns_(true) {}
Howard Hinnant3e519522010-05-11 19:42:16 +0000130 template <class _Clock, class _Duration>
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000132 unique_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __t)
Marshall Clow0b54e792016-03-14 23:07:32 +0000133 : __m_(addressof(__m)), __owns_(__m.try_lock_until(__t)) {}
Howard Hinnant3e519522010-05-11 19:42:16 +0000134 template <class _Rep, class _Period>
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000136 unique_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __d)
Marshall Clow0b54e792016-03-14 23:07:32 +0000137 : __m_(addressof(__m)), __owns_(__m.try_lock_for(__d)) {}
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000139 ~unique_lock()
140 {
141 if (__owns_)
142 __m_->unlock();
143 }
144
145private:
146 unique_lock(unique_lock const&); // = delete;
147 unique_lock& operator=(unique_lock const&); // = delete;
148
149public:
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000150#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000151 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant02e610e2012-07-21 16:13:09 +0000152 unique_lock(unique_lock&& __u) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000153 : __m_(__u.__m_), __owns_(__u.__owns_)
154 {__u.__m_ = nullptr; __u.__owns_ = false;}
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000155 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant02e610e2012-07-21 16:13:09 +0000156 unique_lock& operator=(unique_lock&& __u) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000157 {
158 if (__owns_)
159 __m_->unlock();
160 __m_ = __u.__m_;
161 __owns_ = __u.__owns_;
162 __u.__m_ = nullptr;
163 __u.__owns_ = false;
164 return *this;
165 }
Howard Hinnant6fd5c652010-11-28 19:41:07 +0000166
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000167#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000168
169 void lock();
170 bool try_lock();
171
172 template <class _Rep, class _Period>
173 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d);
174 template <class _Clock, class _Duration>
175 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
176
177 void unlock();
178
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant02e610e2012-07-21 16:13:09 +0000180 void swap(unique_lock& __u) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000181 {
Howard Hinnantce48a112011-06-30 21:18:19 +0000182 _VSTD::swap(__m_, __u.__m_);
183 _VSTD::swap(__owns_, __u.__owns_);
Howard Hinnant3e519522010-05-11 19:42:16 +0000184 }
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000185 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant02e610e2012-07-21 16:13:09 +0000186 mutex_type* release() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000187 {
188 mutex_type* __m = __m_;
189 __m_ = nullptr;
190 __owns_ = false;
191 return __m;
192 }
193
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant02e610e2012-07-21 16:13:09 +0000195 bool owns_lock() const _NOEXCEPT {return __owns_;}
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf2f2d8b2012-02-21 21:46:43 +0000197 _LIBCPP_EXPLICIT
Howard Hinnant02e610e2012-07-21 16:13:09 +0000198 operator bool () const _NOEXCEPT {return __owns_;}
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant02e610e2012-07-21 16:13:09 +0000200 mutex_type* mutex() const _NOEXCEPT {return __m_;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000201};
202
203template <class _Mutex>
204void
205unique_lock<_Mutex>::lock()
206{
207 if (__m_ == nullptr)
208 __throw_system_error(EPERM, "unique_lock::lock: references null mutex");
209 if (__owns_)
210 __throw_system_error(EDEADLK, "unique_lock::lock: already locked");
211 __m_->lock();
212 __owns_ = true;
213}
214
215template <class _Mutex>
216bool
217unique_lock<_Mutex>::try_lock()
218{
219 if (__m_ == nullptr)
220 __throw_system_error(EPERM, "unique_lock::try_lock: references null mutex");
221 if (__owns_)
222 __throw_system_error(EDEADLK, "unique_lock::try_lock: already locked");
223 __owns_ = __m_->try_lock();
224 return __owns_;
225}
226
227template <class _Mutex>
228template <class _Rep, class _Period>
229bool
230unique_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d)
231{
232 if (__m_ == nullptr)
233 __throw_system_error(EPERM, "unique_lock::try_lock_for: references null mutex");
234 if (__owns_)
235 __throw_system_error(EDEADLK, "unique_lock::try_lock_for: already locked");
236 __owns_ = __m_->try_lock_for(__d);
237 return __owns_;
238}
239
240template <class _Mutex>
241template <class _Clock, class _Duration>
242bool
243unique_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
244{
245 if (__m_ == nullptr)
246 __throw_system_error(EPERM, "unique_lock::try_lock_until: references null mutex");
247 if (__owns_)
248 __throw_system_error(EDEADLK, "unique_lock::try_lock_until: already locked");
249 __owns_ = __m_->try_lock_until(__t);
250 return __owns_;
251}
252
253template <class _Mutex>
254void
255unique_lock<_Mutex>::unlock()
256{
257 if (!__owns_)
258 __throw_system_error(EPERM, "unique_lock::unlock: not locked");
259 __m_->unlock();
260 __owns_ = false;
261}
262
263template <class _Mutex>
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000264inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000265void
Howard Hinnant02e610e2012-07-21 16:13:09 +0000266swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT
267 {__x.swap(__y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000268
Marshall Clowce81aed42013-12-23 22:14:27 +0000269//enum class cv_status
270_LIBCPP_DECLARE_STRONG_ENUM(cv_status)
Howard Hinnant3e519522010-05-11 19:42:16 +0000271{
Marshall Clowce81aed42013-12-23 22:14:27 +0000272 no_timeout,
273 timeout
Howard Hinnant3e519522010-05-11 19:42:16 +0000274};
Marshall Clowce81aed42013-12-23 22:14:27 +0000275_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(cv_status)
Howard Hinnant3e519522010-05-11 19:42:16 +0000276
Howard Hinnant6e412562013-03-06 23:30:19 +0000277class _LIBCPP_TYPE_VIS condition_variable
Howard Hinnant3e519522010-05-11 19:42:16 +0000278{
279 pthread_cond_t __cv_;
280public:
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbfa79902012-07-07 20:01:52 +0000282#ifndef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantab303f72012-09-11 16:10:20 +0000283 constexpr condition_variable() : __cv_(PTHREAD_COND_INITIALIZER) {}
Howard Hinnantbfa79902012-07-07 20:01:52 +0000284#else
Howard Hinnant3e519522010-05-11 19:42:16 +0000285 condition_variable() {__cv_ = (pthread_cond_t)PTHREAD_COND_INITIALIZER;}
Howard Hinnantbfa79902012-07-07 20:01:52 +0000286#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000287 ~condition_variable();
288
289private:
290 condition_variable(const condition_variable&); // = delete;
291 condition_variable& operator=(const condition_variable&); // = delete;
292
293public:
Howard Hinnant45c663d2012-07-21 16:32:53 +0000294 void notify_one() _NOEXCEPT;
295 void notify_all() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000296
Marshall Clow1641a7c2014-03-26 02:45:04 +0000297 void wait(unique_lock<mutex>& __lk) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000298 template <class _Predicate>
299 void wait(unique_lock<mutex>& __lk, _Predicate __pred);
300
Howard Hinnant3e519522010-05-11 19:42:16 +0000301 template <class _Clock, class _Duration>
302 cv_status
303 wait_until(unique_lock<mutex>& __lk,
304 const chrono::time_point<_Clock, _Duration>& __t);
305
306 template <class _Clock, class _Duration, class _Predicate>
307 bool
308 wait_until(unique_lock<mutex>& __lk,
309 const chrono::time_point<_Clock, _Duration>& __t,
310 _Predicate __pred);
311
312 template <class _Rep, class _Period>
313 cv_status
314 wait_for(unique_lock<mutex>& __lk,
315 const chrono::duration<_Rep, _Period>& __d);
316
317 template <class _Rep, class _Period, class _Predicate>
318 bool
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000320 wait_for(unique_lock<mutex>& __lk,
321 const chrono::duration<_Rep, _Period>& __d,
322 _Predicate __pred);
323
324 typedef pthread_cond_t* native_handle_type;
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000325 _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__cv_;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000326
327private:
328 void __do_timed_wait(unique_lock<mutex>& __lk,
Marshall Clow1641a7c2014-03-26 02:45:04 +0000329 chrono::time_point<chrono::system_clock, chrono::nanoseconds>) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000330};
Jonathan Roelofsb3fcc672014-09-05 19:45:05 +0000331#endif // !_LIBCPP_HAS_NO_THREADS
Howard Hinnant3e519522010-05-11 19:42:16 +0000332
333template <class _To, class _Rep, class _Period>
Howard Hinnantf5ab7032010-09-21 20:16:37 +0000334inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000335typename enable_if
336<
337 chrono::__is_duration<_To>::value,
338 _To
339>::type
340__ceil(chrono::duration<_Rep, _Period> __d)
341{
342 using namespace chrono;
343 _To __r = duration_cast<_To>(__d);
344 if (__r < __d)
345 ++__r;
346 return __r;
347}
348
Jonathan Roelofsb3fcc672014-09-05 19:45:05 +0000349#ifndef _LIBCPP_HAS_NO_THREADS
Howard Hinnant3e519522010-05-11 19:42:16 +0000350template <class _Predicate>
351void
352condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred)
353{
354 while (!__pred())
355 wait(__lk);
356}
357
Howard Hinnant3e519522010-05-11 19:42:16 +0000358template <class _Clock, class _Duration>
359cv_status
360condition_variable::wait_until(unique_lock<mutex>& __lk,
361 const chrono::time_point<_Clock, _Duration>& __t)
362{
363 using namespace chrono;
Howard Hinnantaad745a2012-08-30 19:14:33 +0000364 wait_for(__lk, __t - _Clock::now());
Howard Hinnant3e519522010-05-11 19:42:16 +0000365 return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout;
366}
367
368template <class _Clock, class _Duration, class _Predicate>
369bool
370condition_variable::wait_until(unique_lock<mutex>& __lk,
371 const chrono::time_point<_Clock, _Duration>& __t,
372 _Predicate __pred)
373{
374 while (!__pred())
375 {
376 if (wait_until(__lk, __t) == cv_status::timeout)
377 return __pred();
378 }
379 return true;
380}
381
382template <class _Rep, class _Period>
383cv_status
384condition_variable::wait_for(unique_lock<mutex>& __lk,
385 const chrono::duration<_Rep, _Period>& __d)
386{
387 using namespace chrono;
Howard Hinnantaad745a2012-08-30 19:14:33 +0000388 if (__d <= __d.zero())
389 return cv_status::timeout;
390 typedef time_point<system_clock, duration<long double, nano> > __sys_tpf;
391 typedef time_point<system_clock, nanoseconds> __sys_tpi;
392 __sys_tpf _Max = __sys_tpi::max();
Howard Hinnant3dc64552010-11-20 19:16:30 +0000393 system_clock::time_point __s_now = system_clock::now();
394 steady_clock::time_point __c_now = steady_clock::now();
Howard Hinnantaad745a2012-08-30 19:14:33 +0000395 if (_Max - __d > __s_now)
396 __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__d));
397 else
398 __do_timed_wait(__lk, __sys_tpi::max());
Howard Hinnant3dc64552010-11-20 19:16:30 +0000399 return steady_clock::now() - __c_now < __d ? cv_status::no_timeout :
400 cv_status::timeout;
Howard Hinnant3e519522010-05-11 19:42:16 +0000401}
402
403template <class _Rep, class _Period, class _Predicate>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000404inline
Howard Hinnant3e519522010-05-11 19:42:16 +0000405bool
406condition_variable::wait_for(unique_lock<mutex>& __lk,
407 const chrono::duration<_Rep, _Period>& __d,
408 _Predicate __pred)
409{
Howard Hinnant3dc64552010-11-20 19:16:30 +0000410 return wait_until(__lk, chrono::steady_clock::now() + __d,
Howard Hinnantce48a112011-06-30 21:18:19 +0000411 _VSTD::move(__pred));
Howard Hinnant3e519522010-05-11 19:42:16 +0000412}
413
Jonathan Roelofsb3fcc672014-09-05 19:45:05 +0000414#endif // !_LIBCPP_HAS_NO_THREADS
415
Howard Hinnant3e519522010-05-11 19:42:16 +0000416_LIBCPP_END_NAMESPACE_STD
417
418#endif // _LIBCPP___MUTEX_BASE