File tree Expand file tree Collapse file tree 1 file changed +70
-0
lines changed Expand file tree Collapse file tree 1 file changed +70
-0
lines changed Original file line number Diff line number Diff line change 1+ #pragma once
2+
3+ #include < utility>
4+ #include < string>
5+ #include < mutex>
6+
7+ #include " libipc/def.h"
8+ #include " libipc/mutex.h"
9+ #include " libipc/condition.h"
10+ #include " libipc/platform/detail.h"
11+
12+ namespace ipc {
13+ namespace detail {
14+
15+ class waiter {
16+ ipc::sync::condition cond_;
17+ ipc::sync::mutex lock_;
18+
19+ public:
20+ waiter () = default ;
21+ waiter (char const *name) {
22+ open (name);
23+ }
24+
25+ ~waiter () {
26+ close ();
27+ }
28+
29+ bool valid () const noexcept {
30+ return cond_.valid () && lock_.valid ();
31+ }
32+
33+ bool open (char const *name) noexcept {
34+ if (!cond_.open ((std::string{" _waiter_cond_" } + name).c_str ())) {
35+ return false ;
36+ }
37+ if (!lock_.open ((std::string{" _waiter_lock_" } + name).c_str ())) {
38+ cond_.close ();
39+ return false ;
40+ }
41+ return valid ();
42+ }
43+
44+ void close () noexcept {
45+ cond_.close ();
46+ lock_.close ();
47+ }
48+
49+ template <typename F>
50+ bool wait_if (F &&pred, std::uint64_t tm = ipc::invalid_value) noexcept {
51+ IPC_UNUSED_ std::lock_guard<ipc::sync::mutex> guard {lock_};
52+ while (std::forward<F>(pred)()) {
53+ if (!cond_.wait (lock_, tm)) return false ;
54+ }
55+ return true ;
56+ }
57+
58+ bool notify () noexcept {
59+ std::lock_guard<ipc::sync::mutex>{lock_}; // barrier
60+ return cond_.notify ();
61+ }
62+
63+ bool broadcast () noexcept {
64+ std::lock_guard<ipc::sync::mutex>{lock_}; // barrier
65+ return cond_.broadcast ();
66+ }
67+ };
68+
69+ } // namespace detail
70+ } // namespace ipc
You can’t perform that action at this time.
0 commit comments