YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
condition_variable.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <condition_variable>
6
7namespace yaclib::detail::fiber {
8
10 public:
12
14
17
18 void notify_one() noexcept;
19 void notify_all() noexcept;
20
21 void wait(std::unique_lock<yaclib::detail::fiber::Mutex>& lock) noexcept;
22
24 void wait(std::unique_lock<yaclib::detail::fiber::Mutex>& lock, Predicate predicate) {
25 WaitImplWithPredicate(lock, NoTimeoutTag{}, predicate);
26 }
27
28 template <typename Clock, typename Duration>
29 WaitStatus wait_until(std::unique_lock<yaclib::detail::fiber::Mutex>& lock,
30 const std::chrono::time_point<Clock, Duration>& time_point) {
31 return WaitImpl(lock, time_point);
32 }
33
34 template <typename Clock, typename Duration, typename Predicate>
35 bool wait_until(std::unique_lock<yaclib::detail::fiber::Mutex>& lock,
36 const std::chrono::time_point<Clock, Duration>& time_point, Predicate predicate) {
37 return WaitImplWithPredicate(lock, time_point, predicate);
38 }
39
40 template <typename Rep, typename Period>
41 WaitStatus wait_for(std::unique_lock<yaclib::detail::fiber::Mutex>& lock,
42 const std::chrono::duration<Rep, Period>& duration) {
43 return WaitImpl(lock, duration);
44 }
45
46 template <typename Rep, typename Period, typename Predicate>
47 bool wait_for(std::unique_lock<yaclib::detail::fiber::Mutex>& lock,
48 const std::chrono::duration<Rep, Period>& duration, Predicate predicate) {
49 return WaitImplWithPredicate(lock, duration, predicate);
50 }
51
52 using native_handle_type = std::condition_variable::native_handle_type;
53
55
56 private:
57 template <typename Timeout, typename Predicate>
58 bool WaitImplWithPredicate(std::unique_lock<yaclib::detail::fiber::Mutex>& lock, const Timeout& timeout,
59 const Predicate& predicate) {
60 while (!predicate()) {
61 if (WaitImpl(lock, timeout) == WaitStatus::Timeout) {
62 break;
63 }
64 }
65 if constexpr (!std::is_same_v<Timeout, NoTimeoutTag>) {
66 return predicate();
67 } else {
68 YACLIB_DEBUG(!predicate(), "Should be true");
69 return true;
70 }
71 }
72
73 template <typename Timeout>
74 WaitStatus WaitImpl(std::unique_lock<yaclib::detail::fiber::Mutex>& lock, const Timeout& timeout) {
76 lock.unlock();
77 auto status = _queue.Wait(timeout);
78 lock.lock();
80 return status;
81 }
82
83 FiberQueue _queue;
84};
85
86} // namespace yaclib::detail::fiber
void wait(std::unique_lock< yaclib::detail::fiber::Mutex > &lock) noexcept
WaitStatus wait_for(std::unique_lock< yaclib::detail::fiber::Mutex > &lock, const std::chrono::duration< Rep, Period > &duration)
WaitStatus wait_until(std::unique_lock< yaclib::detail::fiber::Mutex > &lock, const std::chrono::time_point< Clock, Duration > &time_point)
bool wait_until(std::unique_lock< yaclib::detail::fiber::Mutex > &lock, const std::chrono::time_point< Clock, Duration > &time_point, Predicate predicate)
bool wait_for(std::unique_lock< yaclib::detail::fiber::Mutex > &lock, const std::chrono::duration< Rep, Period > &duration, Predicate predicate)
std::condition_variable::native_handle_type native_handle_type
WaitStatus Wait(NoTimeoutTag)
Definition queue.cpp:5
#define YACLIB_DEBUG(cond, message)
Definition log.hpp:84
void InjectFault() noexcept
Definition inject.cpp:11
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25