File tree Expand file tree Collapse file tree 3 files changed +41
-5
lines changed Expand file tree Collapse file tree 3 files changed +41
-5
lines changed Original file line number Diff line number Diff line change @@ -282,9 +282,9 @@ struct conn_info_head {
282282 }
283283
284284 void quit_waiting () {
285- // cc_waiter_.quit_waiting();
286- // wt_waiter_.quit_waiting();
287- // rd_waiter_.quit_waiting();
285+ cc_waiter_.quit_waiting ();
286+ wt_waiter_.quit_waiting ();
287+ rd_waiter_.quit_waiting ();
288288 }
289289
290290 auto acc () {
Original file line number Diff line number Diff line change 33#include < utility>
44#include < string>
55#include < mutex>
6+ #include < atomic>
67
78#include " libipc/def.h"
89#include " libipc/mutex.h"
@@ -15,6 +16,7 @@ namespace detail {
1516class waiter {
1617 ipc::sync::condition cond_;
1718 ipc::sync::mutex lock_;
19+ std::atomic<bool > quit_ {false };
1820
1921public:
2022 waiter () = default ;
@@ -31,6 +33,7 @@ class waiter {
3133 }
3234
3335 bool open (char const *name) noexcept {
36+ quit_.store (false , std::memory_order_relaxed);
3437 if (!cond_.open ((std::string{" _waiter_cond_" } + name).c_str ())) {
3538 return false ;
3639 }
@@ -49,7 +52,10 @@ class waiter {
4952 template <typename F>
5053 bool wait_if (F &&pred, std::uint64_t tm = ipc::invalid_value) noexcept {
5154 IPC_UNUSED_ std::lock_guard<ipc::sync::mutex> guard {lock_};
52- while (std::forward<F>(pred)()) {
55+ while ([this , &pred] {
56+ return !quit_.load (std::memory_order_relaxed)
57+ && std::forward<F>(pred)();
58+ }()) {
5359 if (!cond_.wait (lock_, tm)) return false ;
5460 }
5561 return true ;
@@ -65,7 +71,9 @@ class waiter {
6571 return cond_.broadcast ();
6672 }
6773
68- void quit_waiting () {
74+ bool quit_waiting () {
75+ quit_.store (true , std::memory_order_release);
76+ return broadcast ();
6977 }
7078};
7179
Original file line number Diff line number Diff line change @@ -35,6 +35,34 @@ TEST(Waiter, broadcast) {
3535}
3636
3737TEST (Waiter, quit_waiting) {
38+ ipc::detail::waiter waiter;
39+ EXPECT_TRUE (waiter.open (" test-ipc-waiter" ));
40+
41+ std::thread t1 {
42+ [&waiter] {
43+ EXPECT_TRUE (waiter.wait_if ([] { return true ; }));
44+ }
45+ };
46+
47+ bool quit = false ;
48+ std::thread t2 {
49+ [&quit] {
50+ ipc::detail::waiter waiter {" test-ipc-waiter" };
51+ EXPECT_TRUE (waiter.wait_if ([&quit] { return !quit; }));
52+ }
53+ };
54+
55+ std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
56+ EXPECT_TRUE (waiter.quit_waiting ());
57+ t1.join ();
58+ ASSERT_TRUE (t2.joinable ());
59+
60+ EXPECT_TRUE (waiter.open (" test-ipc-waiter" ));
61+ std::cout << " nofify quit...\n " ;
62+ quit = true ;
63+ EXPECT_TRUE (waiter.notify ());
64+ t2.join ();
65+ std::cout << " quit... \n " ;
3866}
3967
4068} // internal-linkage
You can’t perform that action at this time.
0 commit comments