2010-05-12 03:42:16 +08:00
|
|
|
// -*- C++ -*-
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-05-12 05:36:01 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
2010-05-12 03:42:16 +08:00
|
|
|
//
|
2010-11-17 06:09:02 +08:00
|
|
|
// This file is dual licensed under the MIT and the University of Illinois Open
|
|
|
|
// Source Licenses. See LICENSE.TXT for details.
|
2010-05-12 03:42:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP___MUTEX_BASE
|
|
|
|
#define _LIBCPP___MUTEX_BASE
|
|
|
|
|
|
|
|
#include <__config>
|
|
|
|
#include <chrono>
|
|
|
|
#include <system_error>
|
2016-05-06 22:06:29 +08:00
|
|
|
#include <__threading_support>
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2011-10-18 04:05:10 +08:00
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2010-05-12 03:42:16 +08:00
|
|
|
#pragma GCC system_header
|
2011-10-18 04:05:10 +08:00
|
|
|
#endif
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2014-09-06 03:45:05 +08:00
|
|
|
#ifndef _LIBCPP_HAS_NO_THREADS
|
|
|
|
|
2016-03-16 10:30:06 +08:00
|
|
|
#ifndef _LIBCPP_THREAD_SAFETY_ANNOTATION
|
|
|
|
# ifdef _LIBCPP_HAS_THREAD_SAFETY_ANNOTATIONS
|
|
|
|
# define _LIBCPP_THREAD_SAFETY_ANNOTATION(x) __attribute__((x))
|
|
|
|
# else
|
|
|
|
# define _LIBCPP_THREAD_SAFETY_ANNOTATION(x)
|
|
|
|
# endif
|
|
|
|
#endif // _LIBCPP_THREAD_SAFETY_ANNOTATION
|
|
|
|
|
|
|
|
class _LIBCPP_TYPE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(capability("mutex")) mutex
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2016-05-06 22:06:29 +08:00
|
|
|
__libcpp_mutex_t __m_;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
public:
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2012-07-08 04:01:52 +08:00
|
|
|
#ifndef _LIBCPP_HAS_NO_CONSTEXPR
|
2016-05-06 22:06:29 +08:00
|
|
|
constexpr mutex() _NOEXCEPT : __m_(_LIBCPP_MUTEX_INITIALIZER) {}
|
2012-07-08 04:01:52 +08:00
|
|
|
#else
|
2016-05-06 22:06:29 +08:00
|
|
|
mutex() _NOEXCEPT {__m_ = (__libcpp_mutex_t)_LIBCPP_MUTEX_INITIALIZER;}
|
2012-07-08 04:01:52 +08:00
|
|
|
#endif
|
2010-05-12 03:42:16 +08:00
|
|
|
~mutex();
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutex(const mutex&);// = delete;
|
|
|
|
mutex& operator=(const mutex&);// = delete;
|
|
|
|
|
|
|
|
public:
|
2016-03-16 10:30:06 +08:00
|
|
|
void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability());
|
|
|
|
bool try_lock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(try_acquire_capability(true));
|
|
|
|
void unlock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability());
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2016-05-06 22:06:29 +08:00
|
|
|
typedef __libcpp_mutex_t* native_handle_type;
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;}
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
|
|
|
|
2013-03-07 07:30:19 +08:00
|
|
|
struct _LIBCPP_TYPE_VIS defer_lock_t {};
|
|
|
|
struct _LIBCPP_TYPE_VIS try_to_lock_t {};
|
|
|
|
struct _LIBCPP_TYPE_VIS adopt_lock_t {};
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2012-07-22 00:13:09 +08:00
|
|
|
#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MUTEX)
|
|
|
|
|
|
|
|
extern const defer_lock_t defer_lock;
|
|
|
|
extern const try_to_lock_t try_to_lock;
|
|
|
|
extern const adopt_lock_t adopt_lock;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2012-07-22 00:13:09 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
constexpr defer_lock_t defer_lock = defer_lock_t();
|
|
|
|
constexpr try_to_lock_t try_to_lock = try_to_lock_t();
|
|
|
|
constexpr adopt_lock_t adopt_lock = adopt_lock_t();
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2012-07-22 00:13:09 +08:00
|
|
|
#endif
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2016-06-14 11:48:09 +08:00
|
|
|
|
|
|
|
// Forward declare lock_guard as a variadic template even in C++03 to keep
|
|
|
|
// the mangling consistent between dialects.
|
|
|
|
#if defined(_LIBCPP_ABI_VARIADIC_LOCK_GUARD)
|
|
|
|
template <class ..._Mutexes>
|
|
|
|
class _LIBCPP_TYPE_VIS_ONLY lock_guard;
|
|
|
|
#endif
|
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
template <class _Mutex>
|
2016-06-14 11:48:09 +08:00
|
|
|
class _LIBCPP_TYPE_VIS_ONLY _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable)
|
|
|
|
#if !defined(_LIBCPP_ABI_VARIADIC_LOCK_GUARD)
|
|
|
|
lock_guard
|
|
|
|
#else
|
|
|
|
lock_guard<_Mutex>
|
|
|
|
#endif
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef _Mutex mutex_type;
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutex_type& __m_;
|
|
|
|
public:
|
|
|
|
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2016-03-16 10:30:06 +08:00
|
|
|
explicit lock_guard(mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
|
2010-05-12 03:42:16 +08:00
|
|
|
: __m_(__m) {__m_.lock();}
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2016-03-16 10:30:06 +08:00
|
|
|
lock_guard(mutex_type& __m, adopt_lock_t) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
|
2010-05-12 03:42:16 +08:00
|
|
|
: __m_(__m) {}
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2016-03-16 10:30:06 +08:00
|
|
|
~lock_guard() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
private:
|
2016-06-14 11:48:09 +08:00
|
|
|
lock_guard(lock_guard const&) _LIBCPP_EQUAL_DELETE;
|
|
|
|
lock_guard& operator=(lock_guard const&) _LIBCPP_EQUAL_DELETE;
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Mutex>
|
2013-08-13 02:38:34 +08:00
|
|
|
class _LIBCPP_TYPE_VIS_ONLY unique_lock
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef _Mutex mutex_type;
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutex_type* __m_;
|
|
|
|
bool __owns_;
|
|
|
|
|
|
|
|
public:
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2012-07-22 00:13:09 +08:00
|
|
|
unique_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-12 03:42:16 +08:00
|
|
|
explicit unique_lock(mutex_type& __m)
|
2016-04-14 01:02:23 +08:00
|
|
|
: __m_(_VSTD::addressof(__m)), __owns_(true) {__m_->lock();}
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2012-07-22 00:13:09 +08:00
|
|
|
unique_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
|
2016-04-14 01:02:23 +08:00
|
|
|
: __m_(_VSTD::addressof(__m)), __owns_(false) {}
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-12 03:42:16 +08:00
|
|
|
unique_lock(mutex_type& __m, try_to_lock_t)
|
2016-04-14 01:02:23 +08:00
|
|
|
: __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock()) {}
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-12 03:42:16 +08:00
|
|
|
unique_lock(mutex_type& __m, adopt_lock_t)
|
2016-04-14 01:02:23 +08:00
|
|
|
: __m_(_VSTD::addressof(__m)), __owns_(true) {}
|
2010-05-12 03:42:16 +08:00
|
|
|
template <class _Clock, class _Duration>
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-12 03:42:16 +08:00
|
|
|
unique_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __t)
|
2016-04-14 01:02:23 +08:00
|
|
|
: __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock_until(__t)) {}
|
2010-05-12 03:42:16 +08:00
|
|
|
template <class _Rep, class _Period>
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-12 03:42:16 +08:00
|
|
|
unique_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __d)
|
2016-04-14 01:02:23 +08:00
|
|
|
: __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock_for(__d)) {}
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-12 03:42:16 +08:00
|
|
|
~unique_lock()
|
|
|
|
{
|
|
|
|
if (__owns_)
|
|
|
|
__m_->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
unique_lock(unique_lock const&); // = delete;
|
|
|
|
unique_lock& operator=(unique_lock const&); // = delete;
|
|
|
|
|
|
|
|
public:
|
2010-09-05 07:28:19 +08:00
|
|
|
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2012-07-22 00:13:09 +08:00
|
|
|
unique_lock(unique_lock&& __u) _NOEXCEPT
|
2010-05-12 03:42:16 +08:00
|
|
|
: __m_(__u.__m_), __owns_(__u.__owns_)
|
|
|
|
{__u.__m_ = nullptr; __u.__owns_ = false;}
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2012-07-22 00:13:09 +08:00
|
|
|
unique_lock& operator=(unique_lock&& __u) _NOEXCEPT
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
if (__owns_)
|
|
|
|
__m_->unlock();
|
|
|
|
__m_ = __u.__m_;
|
|
|
|
__owns_ = __u.__owns_;
|
|
|
|
__u.__m_ = nullptr;
|
|
|
|
__u.__owns_ = false;
|
|
|
|
return *this;
|
|
|
|
}
|
2010-11-29 03:41:07 +08:00
|
|
|
|
2010-09-05 07:28:19 +08:00
|
|
|
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
void lock();
|
|
|
|
bool try_lock();
|
|
|
|
|
|
|
|
template <class _Rep, class _Period>
|
|
|
|
bool try_lock_for(const chrono::duration<_Rep, _Period>& __d);
|
|
|
|
template <class _Clock, class _Duration>
|
|
|
|
bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
|
|
|
|
|
|
|
|
void unlock();
|
|
|
|
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2012-07-22 00:13:09 +08:00
|
|
|
void swap(unique_lock& __u) _NOEXCEPT
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2011-07-01 05:18:19 +08:00
|
|
|
_VSTD::swap(__m_, __u.__m_);
|
|
|
|
_VSTD::swap(__owns_, __u.__owns_);
|
2010-05-12 03:42:16 +08:00
|
|
|
}
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2012-07-22 00:13:09 +08:00
|
|
|
mutex_type* release() _NOEXCEPT
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
mutex_type* __m = __m_;
|
|
|
|
__m_ = nullptr;
|
|
|
|
__owns_ = false;
|
|
|
|
return __m;
|
|
|
|
}
|
|
|
|
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2012-07-22 00:13:09 +08:00
|
|
|
bool owns_lock() const _NOEXCEPT {return __owns_;}
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2012-02-22 05:46:43 +08:00
|
|
|
_LIBCPP_EXPLICIT
|
2012-07-22 00:13:09 +08:00
|
|
|
operator bool () const _NOEXCEPT {return __owns_;}
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2012-07-22 00:13:09 +08:00
|
|
|
mutex_type* mutex() const _NOEXCEPT {return __m_;}
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Mutex>
|
|
|
|
void
|
|
|
|
unique_lock<_Mutex>::lock()
|
|
|
|
{
|
|
|
|
if (__m_ == nullptr)
|
|
|
|
__throw_system_error(EPERM, "unique_lock::lock: references null mutex");
|
|
|
|
if (__owns_)
|
|
|
|
__throw_system_error(EDEADLK, "unique_lock::lock: already locked");
|
|
|
|
__m_->lock();
|
|
|
|
__owns_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Mutex>
|
|
|
|
bool
|
|
|
|
unique_lock<_Mutex>::try_lock()
|
|
|
|
{
|
|
|
|
if (__m_ == nullptr)
|
|
|
|
__throw_system_error(EPERM, "unique_lock::try_lock: references null mutex");
|
|
|
|
if (__owns_)
|
|
|
|
__throw_system_error(EDEADLK, "unique_lock::try_lock: already locked");
|
|
|
|
__owns_ = __m_->try_lock();
|
|
|
|
return __owns_;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Mutex>
|
|
|
|
template <class _Rep, class _Period>
|
|
|
|
bool
|
|
|
|
unique_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d)
|
|
|
|
{
|
|
|
|
if (__m_ == nullptr)
|
|
|
|
__throw_system_error(EPERM, "unique_lock::try_lock_for: references null mutex");
|
|
|
|
if (__owns_)
|
|
|
|
__throw_system_error(EDEADLK, "unique_lock::try_lock_for: already locked");
|
|
|
|
__owns_ = __m_->try_lock_for(__d);
|
|
|
|
return __owns_;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Mutex>
|
|
|
|
template <class _Clock, class _Duration>
|
|
|
|
bool
|
|
|
|
unique_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
|
|
|
|
{
|
|
|
|
if (__m_ == nullptr)
|
|
|
|
__throw_system_error(EPERM, "unique_lock::try_lock_until: references null mutex");
|
|
|
|
if (__owns_)
|
|
|
|
__throw_system_error(EDEADLK, "unique_lock::try_lock_until: already locked");
|
|
|
|
__owns_ = __m_->try_lock_until(__t);
|
|
|
|
return __owns_;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Mutex>
|
|
|
|
void
|
|
|
|
unique_lock<_Mutex>::unlock()
|
|
|
|
{
|
|
|
|
if (!__owns_)
|
|
|
|
__throw_system_error(EPERM, "unique_lock::unlock: not locked");
|
|
|
|
__m_->unlock();
|
|
|
|
__owns_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Mutex>
|
2010-09-22 04:16:37 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2010-05-12 03:42:16 +08:00
|
|
|
void
|
2012-07-22 00:13:09 +08:00
|
|
|
swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT
|
|
|
|
{__x.swap(__y);}
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2013-12-24 06:14:27 +08:00
|
|
|
//enum class cv_status
|
|
|
|
_LIBCPP_DECLARE_STRONG_ENUM(cv_status)
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2013-12-24 06:14:27 +08:00
|
|
|
no_timeout,
|
|
|
|
timeout
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
2013-12-24 06:14:27 +08:00
|
|
|
_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(cv_status)
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2013-03-07 07:30:19 +08:00
|
|
|
class _LIBCPP_TYPE_VIS condition_variable
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2016-05-06 22:06:29 +08:00
|
|
|
__libcpp_condvar_t __cv_;
|
2010-05-12 03:42:16 +08:00
|
|
|
public:
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2012-07-08 04:01:52 +08:00
|
|
|
#ifndef _LIBCPP_HAS_NO_CONSTEXPR
|
2016-05-06 22:06:29 +08:00
|
|
|
constexpr condition_variable() : __cv_(_LIBCPP_CONDVAR_INITIALIZER) {}
|
2012-07-08 04:01:52 +08:00
|
|
|
#else
|
2016-05-06 22:06:29 +08:00
|
|
|
condition_variable() {__cv_ = (__libcpp_condvar_t)_LIBCPP_CONDVAR_INITIALIZER;}
|
2012-07-08 04:01:52 +08:00
|
|
|
#endif
|
2010-05-12 03:42:16 +08:00
|
|
|
~condition_variable();
|
|
|
|
|
|
|
|
private:
|
|
|
|
condition_variable(const condition_variable&); // = delete;
|
|
|
|
condition_variable& operator=(const condition_variable&); // = delete;
|
|
|
|
|
|
|
|
public:
|
2012-07-22 00:32:53 +08:00
|
|
|
void notify_one() _NOEXCEPT;
|
|
|
|
void notify_all() _NOEXCEPT;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2014-03-26 10:45:04 +08:00
|
|
|
void wait(unique_lock<mutex>& __lk) _NOEXCEPT;
|
2010-05-12 03:42:16 +08:00
|
|
|
template <class _Predicate>
|
|
|
|
void wait(unique_lock<mutex>& __lk, _Predicate __pred);
|
|
|
|
|
|
|
|
template <class _Clock, class _Duration>
|
|
|
|
cv_status
|
|
|
|
wait_until(unique_lock<mutex>& __lk,
|
|
|
|
const chrono::time_point<_Clock, _Duration>& __t);
|
|
|
|
|
|
|
|
template <class _Clock, class _Duration, class _Predicate>
|
|
|
|
bool
|
|
|
|
wait_until(unique_lock<mutex>& __lk,
|
|
|
|
const chrono::time_point<_Clock, _Duration>& __t,
|
|
|
|
_Predicate __pred);
|
|
|
|
|
|
|
|
template <class _Rep, class _Period>
|
|
|
|
cv_status
|
|
|
|
wait_for(unique_lock<mutex>& __lk,
|
|
|
|
const chrono::duration<_Rep, _Period>& __d);
|
|
|
|
|
|
|
|
template <class _Rep, class _Period, class _Predicate>
|
|
|
|
bool
|
2015-11-07 09:22:13 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-12 03:42:16 +08:00
|
|
|
wait_for(unique_lock<mutex>& __lk,
|
|
|
|
const chrono::duration<_Rep, _Period>& __d,
|
|
|
|
_Predicate __pred);
|
|
|
|
|
2016-05-06 22:06:29 +08:00
|
|
|
typedef __libcpp_condvar_t* native_handle_type;
|
2010-09-22 04:16:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__cv_;}
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void __do_timed_wait(unique_lock<mutex>& __lk,
|
2014-03-26 10:45:04 +08:00
|
|
|
chrono::time_point<chrono::system_clock, chrono::nanoseconds>) _NOEXCEPT;
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
2014-09-06 03:45:05 +08:00
|
|
|
#endif // !_LIBCPP_HAS_NO_THREADS
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
template <class _To, class _Rep, class _Period>
|
2010-09-22 04:16:37 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2010-05-12 03:42:16 +08:00
|
|
|
typename enable_if
|
|
|
|
<
|
|
|
|
chrono::__is_duration<_To>::value,
|
|
|
|
_To
|
|
|
|
>::type
|
|
|
|
__ceil(chrono::duration<_Rep, _Period> __d)
|
|
|
|
{
|
|
|
|
using namespace chrono;
|
|
|
|
_To __r = duration_cast<_To>(__d);
|
|
|
|
if (__r < __d)
|
|
|
|
++__r;
|
|
|
|
return __r;
|
|
|
|
}
|
|
|
|
|
2014-09-06 03:45:05 +08:00
|
|
|
#ifndef _LIBCPP_HAS_NO_THREADS
|
2010-05-12 03:42:16 +08:00
|
|
|
template <class _Predicate>
|
|
|
|
void
|
|
|
|
condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred)
|
|
|
|
{
|
|
|
|
while (!__pred())
|
|
|
|
wait(__lk);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Clock, class _Duration>
|
|
|
|
cv_status
|
|
|
|
condition_variable::wait_until(unique_lock<mutex>& __lk,
|
|
|
|
const chrono::time_point<_Clock, _Duration>& __t)
|
|
|
|
{
|
|
|
|
using namespace chrono;
|
2012-08-31 03:14:33 +08:00
|
|
|
wait_for(__lk, __t - _Clock::now());
|
2010-05-12 03:42:16 +08:00
|
|
|
return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Clock, class _Duration, class _Predicate>
|
|
|
|
bool
|
|
|
|
condition_variable::wait_until(unique_lock<mutex>& __lk,
|
|
|
|
const chrono::time_point<_Clock, _Duration>& __t,
|
|
|
|
_Predicate __pred)
|
|
|
|
{
|
|
|
|
while (!__pred())
|
|
|
|
{
|
|
|
|
if (wait_until(__lk, __t) == cv_status::timeout)
|
|
|
|
return __pred();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Rep, class _Period>
|
|
|
|
cv_status
|
|
|
|
condition_variable::wait_for(unique_lock<mutex>& __lk,
|
|
|
|
const chrono::duration<_Rep, _Period>& __d)
|
|
|
|
{
|
|
|
|
using namespace chrono;
|
2012-08-31 03:14:33 +08:00
|
|
|
if (__d <= __d.zero())
|
|
|
|
return cv_status::timeout;
|
|
|
|
typedef time_point<system_clock, duration<long double, nano> > __sys_tpf;
|
|
|
|
typedef time_point<system_clock, nanoseconds> __sys_tpi;
|
|
|
|
__sys_tpf _Max = __sys_tpi::max();
|
2010-11-21 03:16:30 +08:00
|
|
|
system_clock::time_point __s_now = system_clock::now();
|
|
|
|
steady_clock::time_point __c_now = steady_clock::now();
|
2012-08-31 03:14:33 +08:00
|
|
|
if (_Max - __d > __s_now)
|
|
|
|
__do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__d));
|
|
|
|
else
|
|
|
|
__do_timed_wait(__lk, __sys_tpi::max());
|
2010-11-21 03:16:30 +08:00
|
|
|
return steady_clock::now() - __c_now < __d ? cv_status::no_timeout :
|
|
|
|
cv_status::timeout;
|
2010-05-12 03:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Rep, class _Period, class _Predicate>
|
2015-11-07 09:22:13 +08:00
|
|
|
inline
|
2010-05-12 03:42:16 +08:00
|
|
|
bool
|
|
|
|
condition_variable::wait_for(unique_lock<mutex>& __lk,
|
|
|
|
const chrono::duration<_Rep, _Period>& __d,
|
|
|
|
_Predicate __pred)
|
|
|
|
{
|
2010-11-21 03:16:30 +08:00
|
|
|
return wait_until(__lk, chrono::steady_clock::now() + __d,
|
2011-07-01 05:18:19 +08:00
|
|
|
_VSTD::move(__pred));
|
2010-05-12 03:42:16 +08:00
|
|
|
}
|
|
|
|
|
2014-09-06 03:45:05 +08:00
|
|
|
#endif // !_LIBCPP_HAS_NO_THREADS
|
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
|
|
|
#endif // _LIBCPP___MUTEX_BASE
|