blob: 4940f931c97f1631c89b444728486e6b92e676ab [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
Eric Fiselier8c570322016-11-18 06:42:1746 constexpr mutex() _NOEXCEPT = 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
Howard Hinnantbc8d3f92010-05-11 19:42:1683template <class _Mutex>
Eric Fiselierc3589a82017-01-04 23:56:0084class _LIBCPP_TEMPLATE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable)
Eric Fiselier10b52a02016-06-14 03:48:0985lock_guard
Howard Hinnantbc8d3f92010-05-11 19:42:1686{
87public:
88 typedef _Mutex mutex_type;
89
90private:
91 mutex_type& __m_;
92public:
93
Howard Hinnant333f50d2010-09-21 20:16:3794 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliera15e8c32016-03-16 02:30:0695 explicit lock_guard(mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
Howard Hinnantbc8d3f92010-05-11 19:42:1696 : __m_(__m) {__m_.lock();}
Howard Hinnant333f50d2010-09-21 20:16:3797 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliera15e8c32016-03-16 02:30:0698 lock_guard(mutex_type& __m, adopt_lock_t) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
Howard Hinnantbc8d3f92010-05-11 19:42:1699 : __m_(__m) {}
Howard Hinnant333f50d2010-09-21 20:16:37100 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliera15e8c32016-03-16 02:30:06101 ~lock_guard() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}
Howard Hinnantbc8d3f92010-05-11 19:42:16102
103private:
Eric Fiselier10b52a02016-06-14 03:48:09104 lock_guard(lock_guard const&) _LIBCPP_EQUAL_DELETE;
105 lock_guard& operator=(lock_guard const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantbc8d3f92010-05-11 19:42:16106};
107
108template <class _Mutex>
Eric Fiselierc3589a82017-01-04 23:56:00109class _LIBCPP_TEMPLATE_VIS unique_lock
Howard Hinnantbc8d3f92010-05-11 19:42:16110{
111public:
112 typedef _Mutex mutex_type;
113
114private:
115 mutex_type* __m_;
116 bool __owns_;
117
118public:
Howard Hinnant333f50d2010-09-21 20:16:37119 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09120 unique_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
Howard Hinnant333f50d2010-09-21 20:16:37121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16122 explicit unique_lock(mutex_type& __m)
Marshall Clow81652492016-04-13 17:02:23123 : __m_(_VSTD::addressof(__m)), __owns_(true) {__m_->lock();}
Howard Hinnant333f50d2010-09-21 20:16:37124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09125 unique_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
Marshall Clow81652492016-04-13 17:02:23126 : __m_(_VSTD::addressof(__m)), __owns_(false) {}
Howard Hinnant333f50d2010-09-21 20:16:37127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16128 unique_lock(mutex_type& __m, try_to_lock_t)
Marshall Clow81652492016-04-13 17:02:23129 : __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock()) {}
Howard Hinnant333f50d2010-09-21 20:16:37130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16131 unique_lock(mutex_type& __m, adopt_lock_t)
Marshall Clow81652492016-04-13 17:02:23132 : __m_(_VSTD::addressof(__m)), __owns_(true) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16133 template <class _Clock, class _Duration>
Howard Hinnant333f50d2010-09-21 20:16:37134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16135 unique_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __t)
Marshall Clow81652492016-04-13 17:02:23136 : __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock_until(__t)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16137 template <class _Rep, class _Period>
Howard Hinnant333f50d2010-09-21 20:16:37138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16139 unique_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __d)
Marshall Clow81652492016-04-13 17:02:23140 : __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock_for(__d)) {}
Howard Hinnant333f50d2010-09-21 20:16:37141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16142 ~unique_lock()
143 {
144 if (__owns_)
145 __m_->unlock();
146 }
147
148private:
149 unique_lock(unique_lock const&); // = delete;
150 unique_lock& operator=(unique_lock const&); // = delete;
151
152public:
Howard Hinnant73d21a42010-09-04 23:28:19153#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant333f50d2010-09-21 20:16:37154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09155 unique_lock(unique_lock&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16156 : __m_(__u.__m_), __owns_(__u.__owns_)
157 {__u.__m_ = nullptr; __u.__owns_ = false;}
Howard Hinnant333f50d2010-09-21 20:16:37158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09159 unique_lock& operator=(unique_lock&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16160 {
161 if (__owns_)
162 __m_->unlock();
163 __m_ = __u.__m_;
164 __owns_ = __u.__owns_;
165 __u.__m_ = nullptr;
166 __u.__owns_ = false;
167 return *this;
168 }
Howard Hinnantac417fa2010-11-28 19:41:07169
Howard Hinnant73d21a42010-09-04 23:28:19170#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16171
172 void lock();
173 bool try_lock();
174
175 template <class _Rep, class _Period>
176 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d);
177 template <class _Clock, class _Duration>
178 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
179
180 void unlock();
181
Howard Hinnant333f50d2010-09-21 20:16:37182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09183 void swap(unique_lock& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16184 {
Howard Hinnant0949eed2011-06-30 21:18:19185 _VSTD::swap(__m_, __u.__m_);
186 _VSTD::swap(__owns_, __u.__owns_);
Howard Hinnantbc8d3f92010-05-11 19:42:16187 }
Howard Hinnant333f50d2010-09-21 20:16:37188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09189 mutex_type* release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16190 {
191 mutex_type* __m = __m_;
192 __m_ = nullptr;
193 __owns_ = false;
194 return __m;
195 }
196
Howard Hinnant333f50d2010-09-21 20:16:37197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09198 bool owns_lock() const _NOEXCEPT {return __owns_;}
Howard Hinnant333f50d2010-09-21 20:16:37199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43200 _LIBCPP_EXPLICIT
Howard Hinnant499c61f2012-07-21 16:13:09201 operator bool () const _NOEXCEPT {return __owns_;}
Howard Hinnant333f50d2010-09-21 20:16:37202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09203 mutex_type* mutex() const _NOEXCEPT {return __m_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16204};
205
206template <class _Mutex>
207void
208unique_lock<_Mutex>::lock()
209{
210 if (__m_ == nullptr)
211 __throw_system_error(EPERM, "unique_lock::lock: references null mutex");
212 if (__owns_)
213 __throw_system_error(EDEADLK, "unique_lock::lock: already locked");
214 __m_->lock();
215 __owns_ = true;
216}
217
218template <class _Mutex>
219bool
220unique_lock<_Mutex>::try_lock()
221{
222 if (__m_ == nullptr)
223 __throw_system_error(EPERM, "unique_lock::try_lock: references null mutex");
224 if (__owns_)
225 __throw_system_error(EDEADLK, "unique_lock::try_lock: already locked");
226 __owns_ = __m_->try_lock();
227 return __owns_;
228}
229
230template <class _Mutex>
231template <class _Rep, class _Period>
232bool
233unique_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d)
234{
235 if (__m_ == nullptr)
236 __throw_system_error(EPERM, "unique_lock::try_lock_for: references null mutex");
237 if (__owns_)
238 __throw_system_error(EDEADLK, "unique_lock::try_lock_for: already locked");
239 __owns_ = __m_->try_lock_for(__d);
240 return __owns_;
241}
242
243template <class _Mutex>
244template <class _Clock, class _Duration>
245bool
246unique_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
247{
248 if (__m_ == nullptr)
249 __throw_system_error(EPERM, "unique_lock::try_lock_until: references null mutex");
250 if (__owns_)
251 __throw_system_error(EDEADLK, "unique_lock::try_lock_until: already locked");
252 __owns_ = __m_->try_lock_until(__t);
253 return __owns_;
254}
255
256template <class _Mutex>
257void
258unique_lock<_Mutex>::unlock()
259{
260 if (!__owns_)
261 __throw_system_error(EPERM, "unique_lock::unlock: not locked");
262 __m_->unlock();
263 __owns_ = false;
264}
265
266template <class _Mutex>
Howard Hinnant333f50d2010-09-21 20:16:37267inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16268void
Howard Hinnant499c61f2012-07-21 16:13:09269swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT
270 {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16271
Marshall Clow239bc422013-12-23 22:14:27272//enum class cv_status
273_LIBCPP_DECLARE_STRONG_ENUM(cv_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16274{
Marshall Clow239bc422013-12-23 22:14:27275 no_timeout,
276 timeout
Howard Hinnantbc8d3f92010-05-11 19:42:16277};
Marshall Clow239bc422013-12-23 22:14:27278_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(cv_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16279
Howard Hinnant83eade62013-03-06 23:30:19280class _LIBCPP_TYPE_VIS condition_variable
Howard Hinnantbc8d3f92010-05-11 19:42:16281{
Marshall Clow65eb1e92016-07-18 17:23:06282#ifndef _LIBCPP_HAS_NO_CONSTEXPR
283 __libcpp_condvar_t __cv_ = _LIBCPP_CONDVAR_INITIALIZER;
284#else
285 __libcpp_condvar_t __cv_;
286#endif
287
Howard Hinnantbc8d3f92010-05-11 19:42:16288public:
Howard Hinnant333f50d2010-09-21 20:16:37289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant384608e2012-07-07 20:01:52290#ifndef _LIBCPP_HAS_NO_CONSTEXPR
Eric Fiselier8c570322016-11-18 06:42:17291 constexpr condition_variable() _NOEXCEPT = default;
Howard Hinnant384608e2012-07-07 20:01:52292#else
Marshall Clow65eb1e92016-07-18 17:23:06293 condition_variable() _NOEXCEPT {__cv_ = (__libcpp_condvar_t)_LIBCPP_CONDVAR_INITIALIZER;}
Howard Hinnant384608e2012-07-07 20:01:52294#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16295 ~condition_variable();
296
297private:
298 condition_variable(const condition_variable&); // = delete;
299 condition_variable& operator=(const condition_variable&); // = delete;
300
301public:
Howard Hinnantc8f74132012-07-21 16:32:53302 void notify_one() _NOEXCEPT;
303 void notify_all() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16304
Marshall Clowb0767852014-03-26 02:45:04305 void wait(unique_lock<mutex>& __lk) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16306 template <class _Predicate>
Shoaib Meenai6b734922017-03-02 03:22:18307 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantbc8d3f92010-05-11 19:42:16308 void wait(unique_lock<mutex>& __lk, _Predicate __pred);
309
Howard Hinnantbc8d3f92010-05-11 19:42:16310 template <class _Clock, class _Duration>
Shoaib Meenai6b734922017-03-02 03:22:18311 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantbc8d3f92010-05-11 19:42:16312 cv_status
313 wait_until(unique_lock<mutex>& __lk,
314 const chrono::time_point<_Clock, _Duration>& __t);
315
316 template <class _Clock, class _Duration, class _Predicate>
Shoaib Meenai6b734922017-03-02 03:22:18317 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantbc8d3f92010-05-11 19:42:16318 bool
319 wait_until(unique_lock<mutex>& __lk,
320 const chrono::time_point<_Clock, _Duration>& __t,
321 _Predicate __pred);
322
323 template <class _Rep, class _Period>
Shoaib Meenai6b734922017-03-02 03:22:18324 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantbc8d3f92010-05-11 19:42:16325 cv_status
326 wait_for(unique_lock<mutex>& __lk,
327 const chrono::duration<_Rep, _Period>& __d);
328
329 template <class _Rep, class _Period, class _Predicate>
330 bool
Evgeniy Stepanova3b25f82015-11-07 01:22:13331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16332 wait_for(unique_lock<mutex>& __lk,
333 const chrono::duration<_Rep, _Period>& __d,
334 _Predicate __pred);
335
Asiri Rathnayake35ff03b2016-05-06 14:06:29336 typedef __libcpp_condvar_t* native_handle_type;
Howard Hinnant333f50d2010-09-21 20:16:37337 _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__cv_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16338
339private:
340 void __do_timed_wait(unique_lock<mutex>& __lk,
Marshall Clowb0767852014-03-26 02:45:04341 chrono::time_point<chrono::system_clock, chrono::nanoseconds>) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16342};
Jonathan Roelofs8d86b2e2014-09-05 19:45:05343#endif // !_LIBCPP_HAS_NO_THREADS
Howard Hinnantbc8d3f92010-05-11 19:42:16344
345template <class _To, class _Rep, class _Period>
Howard Hinnant333f50d2010-09-21 20:16:37346inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16347typename enable_if
348<
349 chrono::__is_duration<_To>::value,
350 _To
351>::type
352__ceil(chrono::duration<_Rep, _Period> __d)
353{
354 using namespace chrono;
355 _To __r = duration_cast<_To>(__d);
356 if (__r < __d)
357 ++__r;
358 return __r;
359}
360
Jonathan Roelofs8d86b2e2014-09-05 19:45:05361#ifndef _LIBCPP_HAS_NO_THREADS
Howard Hinnantbc8d3f92010-05-11 19:42:16362template <class _Predicate>
363void
364condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred)
365{
366 while (!__pred())
367 wait(__lk);
368}
369
Howard Hinnantbc8d3f92010-05-11 19:42:16370template <class _Clock, class _Duration>
371cv_status
372condition_variable::wait_until(unique_lock<mutex>& __lk,
373 const chrono::time_point<_Clock, _Duration>& __t)
374{
375 using namespace chrono;
Howard Hinnantcf115d22012-08-30 19:14:33376 wait_for(__lk, __t - _Clock::now());
Howard Hinnantbc8d3f92010-05-11 19:42:16377 return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout;
378}
379
380template <class _Clock, class _Duration, class _Predicate>
381bool
382condition_variable::wait_until(unique_lock<mutex>& __lk,
383 const chrono::time_point<_Clock, _Duration>& __t,
384 _Predicate __pred)
385{
386 while (!__pred())
387 {
388 if (wait_until(__lk, __t) == cv_status::timeout)
389 return __pred();
390 }
391 return true;
392}
393
394template <class _Rep, class _Period>
395cv_status
396condition_variable::wait_for(unique_lock<mutex>& __lk,
397 const chrono::duration<_Rep, _Period>& __d)
398{
399 using namespace chrono;
Howard Hinnantcf115d22012-08-30 19:14:33400 if (__d <= __d.zero())
401 return cv_status::timeout;
402 typedef time_point<system_clock, duration<long double, nano> > __sys_tpf;
403 typedef time_point<system_clock, nanoseconds> __sys_tpi;
404 __sys_tpf _Max = __sys_tpi::max();
Howard Hinnantf8f85212010-11-20 19:16:30405 steady_clock::time_point __c_now = steady_clock::now();
Marshall Clowf3e76772017-01-09 22:32:11406 system_clock::time_point __s_now = system_clock::now();
Howard Hinnantcf115d22012-08-30 19:14:33407 if (_Max - __d > __s_now)
408 __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__d));
409 else
410 __do_timed_wait(__lk, __sys_tpi::max());
Howard Hinnantf8f85212010-11-20 19:16:30411 return steady_clock::now() - __c_now < __d ? cv_status::no_timeout :
412 cv_status::timeout;
Howard Hinnantbc8d3f92010-05-11 19:42:16413}
414
415template <class _Rep, class _Period, class _Predicate>
Evgeniy Stepanova3b25f82015-11-07 01:22:13416inline
Howard Hinnantbc8d3f92010-05-11 19:42:16417bool
418condition_variable::wait_for(unique_lock<mutex>& __lk,
419 const chrono::duration<_Rep, _Period>& __d,
420 _Predicate __pred)
421{
Howard Hinnantf8f85212010-11-20 19:16:30422 return wait_until(__lk, chrono::steady_clock::now() + __d,
Howard Hinnant0949eed2011-06-30 21:18:19423 _VSTD::move(__pred));
Howard Hinnantbc8d3f92010-05-11 19:42:16424}
425
Jonathan Roelofs8d86b2e2014-09-05 19:45:05426#endif // !_LIBCPP_HAS_NO_THREADS
427
Howard Hinnantbc8d3f92010-05-11 19:42:16428_LIBCPP_END_NAMESPACE_STD
429
430#endif // _LIBCPP___MUTEX_BASE