Skip to content

Commit d946ad0

Browse files
committed
modify interface of sync.condition
1 parent 4ddc1d0 commit d946ad0

File tree

10 files changed

+33
-195
lines changed

10 files changed

+33
-195
lines changed

include/libipc/condition.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class IPC_EXPORT condition {
2727
void close() noexcept;
2828

2929
bool wait(ipc::sync::mutex &mtx, std::uint64_t tm = ipc::invalid_value) noexcept;
30-
bool notify() noexcept;
31-
bool broadcast() noexcept;
30+
bool notify(ipc::sync::mutex &mtx) noexcept;
31+
bool broadcast(ipc::sync::mutex &mtx) noexcept;
3232

3333
private:
3434
class condition_;

src/libipc/platform/linux/condition.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class condition {
115115
return true;
116116
}
117117

118-
bool notify() noexcept {
118+
bool notify(ipc::sync::mutex &) noexcept {
119119
if (!valid()) return false;
120120
int eno;
121121
if ((eno = ::pthread_cond_signal(cond_)) != 0) {
@@ -125,7 +125,7 @@ class condition {
125125
return true;
126126
}
127127

128-
bool broadcast() noexcept {
128+
bool broadcast(ipc::sync::mutex &) noexcept {
129129
if (!valid()) return false;
130130
int eno;
131131
if ((eno = ::pthread_cond_broadcast(cond_)) != 0) {

src/libipc/platform/win/condition.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class condition {
8989
return rs && rl;
9090
}
9191

92-
bool notify() noexcept {
92+
bool notify(ipc::sync::mutex &) noexcept {
9393
if (!valid()) return false;
9494
auto &cnt = counter();
9595
if (!lock_.lock()) return false;
@@ -101,7 +101,7 @@ class condition {
101101
return lock_.unlock() && ret;
102102
}
103103

104-
bool broadcast() noexcept {
104+
bool broadcast(ipc::sync::mutex &) noexcept {
105105
if (!valid()) return false;
106106
auto &cnt = counter();
107107
if (!lock_.lock()) return false;

src/libipc/sync/condition.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ bool condition::wait(ipc::sync::mutex &mtx, std::uint64_t tm) noexcept {
5858
return impl(p_)->cond_.wait(mtx, tm);
5959
}
6060

61-
bool condition::notify() noexcept {
62-
return impl(p_)->cond_.notify();
61+
bool condition::notify(ipc::sync::mutex &mtx) noexcept {
62+
return impl(p_)->cond_.notify(mtx);
6363
}
6464

65-
bool condition::broadcast() noexcept {
66-
return impl(p_)->cond_.broadcast();
65+
bool condition::broadcast(ipc::sync::mutex &mtx) noexcept {
66+
return impl(p_)->cond_.broadcast(mtx);
6767
}
6868

6969
} // namespace sync

src/libipc/waiter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ class waiter {
6363

6464
bool notify() noexcept {
6565
std::lock_guard<ipc::sync::mutex>{lock_}; // barrier
66-
return cond_.notify();
66+
return cond_.notify(lock_);
6767
}
6868

6969
bool broadcast() noexcept {
7070
std::lock_guard<ipc::sync::mutex>{lock_}; // barrier
71-
return cond_.broadcast();
71+
return cond_.broadcast(lock_);
7272
}
7373

7474
bool quit_waiting() {

test/profiler/README.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

test/profiler/profiler.cpp

Lines changed: 0 additions & 77 deletions
This file was deleted.

test/profiler/profiler.h

Lines changed: 0 additions & 35 deletions
This file was deleted.

test/profiler/rdtsc.h

Lines changed: 0 additions & 52 deletions
This file was deleted.

test/test_sync.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ TEST(Sync, Condition) {
113113
auto job = [&que](int num) {
114114
ipc::sync::condition cond {"test-cond"};
115115
ipc::sync::mutex lock {"test-mutex"};
116-
for (;;) {
116+
for (int i = 0; i < 10; ++i) {
117117
int val = 0;
118118
{
119119
std::lock_guard<ipc::sync::mutex> guard {lock};
@@ -123,6 +123,19 @@ TEST(Sync, Condition) {
123123
val = que.front();
124124
que.pop_front();
125125
}
126+
EXPECT_NE(val, 0);
127+
std::printf("test-cond-%d: %d\n", num, val);
128+
}
129+
for (;;) {
130+
int val = 0;
131+
{
132+
std::lock_guard<ipc::sync::mutex> guard {lock};
133+
while (que.empty()) {
134+
EXPECT_TRUE(cond.wait(lock, 1000));
135+
}
136+
val = que.front();
137+
que.pop_front();
138+
}
126139
if (val == 0) {
127140
std::printf("test-cond-%d: exit.\n", num);
128141
return;
@@ -139,25 +152,25 @@ TEST(Sync, Condition) {
139152
{
140153
std::lock_guard<ipc::sync::mutex> guard {lock};
141154
que.push_back(i);
155+
ASSERT_TRUE(cond.notify(lock));
142156
}
143-
cond.notify();
144157
std::this_thread::sleep_for(std::chrono::milliseconds(20));
145158
}
146159
for (int i = 1; i < 100; ++i) {
147160
{
148161
std::lock_guard<ipc::sync::mutex> guard {lock};
149162
que.push_back(i);
163+
ASSERT_TRUE(cond.broadcast(lock));
150164
}
151-
cond.broadcast();
152165
std::this_thread::sleep_for(std::chrono::milliseconds(20));
153166
}
154167
{
155168
std::lock_guard<ipc::sync::mutex> guard {lock};
156169
for (int i = 0; i < (int)test_conds.size(); ++i) {
157170
que.push_back(0);
158171
}
172+
ASSERT_TRUE(cond.broadcast(lock));
159173
}
160-
cond.broadcast();
161174

162175
for (auto &t : test_conds) t.join();
163176
}
@@ -171,7 +184,7 @@ TEST(Sync, ConditionRobust) {
171184
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 2\n");
172185
ipc::sync::mutex lock {"test-mutex"};
173186
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 3\n");
174-
lock.lock();
187+
ASSERT_TRUE(lock.lock());
175188
std::thread unlock {[] {
176189
printf("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 1\n");
177190
ipc::sync::condition cond {"test-cond"};
@@ -183,13 +196,13 @@ TEST(Sync, ConditionRobust) {
183196
}
184197
std::this_thread::sleep_for(std::chrono::seconds(1));
185198
printf("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 4\n");
186-
cond.broadcast();
199+
ASSERT_TRUE(cond.broadcast(lock));
187200
printf("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 5\n");
188201
}};
189202
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 4\n");
190-
cond.wait(lock);
203+
ASSERT_TRUE(cond.wait(lock));
191204
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 5\n");
192-
lock.unlock();
205+
ASSERT_TRUE(lock.unlock());
193206
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 6\n");
194207
unlock.join();
195208
}

0 commit comments

Comments
 (0)