19 #include <condition_variable>
21 #include "priority_mutex.h"
24 typedef std::chrono::system_clock clock_t;
27 typedef std::condition_variable::native_handle_type native_handle_type;
35 void notify_one() noexcept {
36 std::lock_guard<std::mutex> lock(*m_mutex);
40 void notify_all() noexcept {
41 std::lock_guard<std::mutex> lock(*m_mutex);
45 template<
typename Lock>
46 void wait(Lock& _lock) {
47 std::shared_ptr<std::mutex> _mutex = m_mutex;
48 std::unique_lock<std::mutex> my_lock(*_mutex);
49 Unlock<Lock> unlock(_lock);
53 std::unique_lock<std::mutex> my_lock2(std::move(my_lock));
54 m_cond.wait(my_lock2);
57 template<
typename Lock,
typename Predicate>
58 void wait(Lock& lock, Predicate p) {
59 while (!p()) { wait(lock); }
62 template<
typename Lock,
typename Clock,
typename Duration>
63 std::cv_status wait_until(Lock& _lock,
64 const std::chrono::time_point<Clock, Duration>& atime) {
65 std::shared_ptr<std::mutex> _mutex = m_mutex;
66 std::unique_lock<std::mutex> my_lock(*_mutex);
67 Unlock<Lock> unlock(_lock);
71 std::unique_lock<std::mutex> my_lock2(std::move(my_lock));
72 return m_cond.wait_until(my_lock2, atime);
75 template<
typename Lock,
typename Clock,
typename Duration,
typename Predicate>
76 bool wait_until(Lock& lock,
77 const std::chrono::time_point<Clock, Duration>& atime, Predicate p) {
79 if (wait_until(lock, atime) == std::cv_status::timeout) {
86 template<
typename Lock,
typename Rep,
typename Period>
87 std::cv_status wait_for(Lock& lock,
const std::chrono::duration<Rep, Period>& rtime) {
88 return wait_until(lock, clock_t::now() + rtime);
91 template<
typename Lock,
typename Rep,
typename Period,
typename Predicate>
92 bool wait_for(Lock& lock,
const std::chrono::duration<Rep, Period>& rtime,
94 return wait_until(lock, clock_t::now() + rtime, std::move(p));
97 native_handle_type native_handle() {
98 return m_cond.native_handle();
102 std::condition_variable m_cond;
103 std::shared_ptr<std::mutex> m_mutex;
106 template<
typename Lock>
108 explicit Unlock(Lock& lk) : m_lock(lk) { lk.unlock(); }
111 if (std::uncaught_exception()) {
112 try { m_lock.lock(); }
catch(...) {}
119 Unlock(
const Unlock&) =
delete;
120 Unlock& operator=(
const Unlock&) =
delete;
Definition: priority_condition_variable.h:23