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 {
282
282
}
283
283
284
284
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 ();
288
288
}
289
289
290
290
auto acc () {
Original file line number Diff line number Diff line change 3
3
#include < utility>
4
4
#include < string>
5
5
#include < mutex>
6
+ #include < atomic>
6
7
7
8
#include " libipc/def.h"
8
9
#include " libipc/mutex.h"
@@ -15,6 +16,7 @@ namespace detail {
15
16
class waiter {
16
17
ipc::sync::condition cond_;
17
18
ipc::sync::mutex lock_;
19
+ std::atomic<bool > quit_ {false };
18
20
19
21
public:
20
22
waiter () = default ;
@@ -31,6 +33,7 @@ class waiter {
31
33
}
32
34
33
35
bool open (char const *name) noexcept {
36
+ quit_.store (false , std::memory_order_relaxed);
34
37
if (!cond_.open ((std::string{" _waiter_cond_" } + name).c_str ())) {
35
38
return false ;
36
39
}
@@ -49,7 +52,10 @@ class waiter {
49
52
template <typename F>
50
53
bool wait_if (F &&pred, std::uint64_t tm = ipc::invalid_value) noexcept {
51
54
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
+ }()) {
53
59
if (!cond_.wait (lock_, tm)) return false ;
54
60
}
55
61
return true ;
@@ -65,7 +71,9 @@ class waiter {
65
71
return cond_.broadcast ();
66
72
}
67
73
68
- void quit_waiting () {
74
+ bool quit_waiting () {
75
+ quit_.store (true , std::memory_order_release);
76
+ return broadcast ();
69
77
}
70
78
};
71
79
Original file line number Diff line number Diff line change @@ -35,6 +35,34 @@ TEST(Waiter, broadcast) {
35
35
}
36
36
37
37
TEST (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 " ;
38
66
}
39
67
40
68
} // internal-linkage
You can’t perform that action at this time.
0 commit comments