123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- // -*- C++ -*-
- //===---------------------- condition_variable ----------------------------===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is dual licensed under the MIT and the University of Illinois Open
- // Source Licenses. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- #ifndef _LIBCPP_SGX_CONDITION_VARIABLE
- #define _LIBCPP_SGX_CONDITION_VARIABLE
- /*
- condition_variable synopsis
- namespace std
- {
- enum class cv_status { no_timeout, timeout };
- class condition_variable
- {
- public:
- condition_variable();
- ~condition_variable();
- condition_variable(const condition_variable&) = delete;
- condition_variable& operator=(const condition_variable&) = delete;
- void notify_one() noexcept;
- void notify_all() noexcept;
- void wait(unique_lock<mutex>& lock);
- template <class Predicate>
- void wait(unique_lock<mutex>& lock, Predicate pred);
- Not supported (time API) : template <class Clock, class Duration>
- cv_status
- wait_until(unique_lock<mutex>& lock,
- const chrono::time_point<Clock, Duration>& abs_time);
- Not supported (time API) : template <class Clock, class Duration, class Predicate>
- bool
- wait_until(unique_lock<mutex>& lock,
- const chrono::time_point<Clock, Duration>& abs_time,
- Predicate pred);
- Not supported (time API) : template <class Rep, class Period>
- cv_status
- wait_for(unique_lock<mutex>& lock,
- const chrono::duration<Rep, Period>& rel_time);
- Not supported (time API) : template <class Rep, class Period, class Predicate>
- bool
- wait_for(unique_lock<mutex>& lock,
- const chrono::duration<Rep, Period>& rel_time,
- Predicate pred);
- typedef sgx_thread_cond_t* native_handle_type;
- native_handle_type native_handle();
- };
- void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
- class condition_variable_any
- {
- public:
- condition_variable_any();
- ~condition_variable_any();
- condition_variable_any(const condition_variable_any&) = delete;
- condition_variable_any& operator=(const condition_variable_any&) = delete;
- void notify_one() noexcept;
- void notify_all() noexcept;
- template <class Lock>
- void wait(Lock& lock);
- template <class Lock, class Predicate>
- void wait(Lock& lock, Predicate pred);
- Not supported (time API) : template <class Lock, class Clock, class Duration>
- cv_status
- wait_until(Lock& lock,
- const chrono::time_point<Clock, Duration>& abs_time);
- Not supported (time API) : template <class Lock, class Clock, class Duration, class Predicate>
- bool
- wait_until(Lock& lock,
- const chrono::time_point<Clock, Duration>& abs_time,
- Predicate pred);
- Not supported (time API) : template <class Lock, class Rep, class Period>
- cv_status
- wait_for(Lock& lock,
- const chrono::duration<Rep, Period>& rel_time);
- Not supported (time API) : template <class Lock, class Rep, class Period, class Predicate>
- bool
- wait_for(Lock& lock,
- const chrono::duration<Rep, Period>& rel_time,
- Predicate pred);
- };
- } // std
- */
- #include <__config>
- #include <sgx_thread.h>
- #include <support/sgx/__sgx_mutex_base>
- #include <memory>
- #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
- #pragma GCC system_header
- #endif
- _LIBCPP_BEGIN_NAMESPACE_STD
- class _LIBCPP_TYPE_VIS condition_variable_any
- {
- condition_variable __cv_;
- shared_ptr<mutex> __mut_;
- public:
- condition_variable_any();
- void notify_one() _NOEXCEPT;
- void notify_all() _NOEXCEPT;
- template <class _Lock>
- void wait(_Lock& __lock);
- template <class _Lock, class _Predicate>
- void wait(_Lock& __lock, _Predicate __pred);
- };
- inline _LIBCPP_INLINE_VISIBILITY
- condition_variable_any::condition_variable_any()
- : __mut_(make_shared<mutex>()) {}
- inline _LIBCPP_INLINE_VISIBILITY
- void
- condition_variable_any::notify_one() _NOEXCEPT
- {
- {lock_guard<mutex> __lx(*__mut_);}
- __cv_.notify_one();
- }
- inline _LIBCPP_INLINE_VISIBILITY
- void
- condition_variable_any::notify_all() _NOEXCEPT
- {
- {lock_guard<mutex> __lx(*__mut_);}
- __cv_.notify_all();
- }
- struct __lock_external
- {
- template <class _Lock>
- void operator()(_Lock* __m) {__m->lock();}
- };
- template <class _Lock>
- void
- condition_variable_any::wait(_Lock& __lock)
- {
- shared_ptr<mutex> __mut = __mut_;
- unique_lock<mutex> __lk(*__mut);
- __lock.unlock();
- unique_ptr<_Lock, __lock_external> __lxx(&__lock);
- lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);
- __cv_.wait(__lk);
- } // __mut_.unlock(), __lock.lock()
- template <class _Lock, class _Predicate>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- condition_variable_any::wait(_Lock& __lock, _Predicate __pred)
- {
- while (!__pred())
- wait(__lock);
- }
- _LIBCPP_END_NAMESPACE_STD
- #endif // _LIBCPP_SGX_CONDITION_VARIABLE
|