Skip to content

Commit baf645e

Browse files
committed
修正 recv timeout 接口cpu占用过高的问题
1 parent d80bea9 commit baf645e

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/libipc/platform/waiter_linux.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,6 @@ class sem_helper {
224224
static bool wait(handle_t h, std::size_t tm = invalid_value) {
225225
if (h == invalid()) return false;
226226
switch (tm) {
227-
case 0:
228-
return true;
229227
case invalid_value:
230228
IPC_SEMAPHORE_FUNC_(sem_wait, h);
231229
default: {

src/libipc/waiter_helper.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace detail {
1313
struct waiter_helper {
1414

1515
struct wait_counter {
16-
std::atomic<unsigned> waiting_ { 0 };
16+
std::atomic<long> waiting_ { 0 };
1717
long counter_ = 0;
1818
};
1919

@@ -40,7 +40,7 @@ struct waiter_helper {
4040
{
4141
IPC_UNUSED_ auto guard = ctrl.get_lock();
4242
if (!std::forward<F>(pred)()) return true;
43-
counter.counter_ += 1;
43+
counter.counter_ = counter.waiting_.load(std::memory_order_relaxed);
4444
}
4545
mtx.unlock();
4646

@@ -69,6 +69,11 @@ struct waiter_helper {
6969
return ret;
7070
}
7171

72+
template <typename Ctrl>
73+
static void clear_handshake(Ctrl & ctrl) {
74+
while (ctrl.handshake_wait(0)) ;
75+
}
76+
7277
template <typename Ctrl>
7378
static bool notify(Ctrl & ctrl) {
7479
auto & counter = ctrl.counter();
@@ -77,6 +82,7 @@ struct waiter_helper {
7782
}
7883
bool ret = true;
7984
IPC_UNUSED_ auto guard = ctrl.get_lock();
85+
clear_handshake(ctrl);
8086
if (counter.counter_ > 0) {
8187
ret = ctrl.sema_post(1);
8288
counter.counter_ -= 1;
@@ -93,11 +99,13 @@ struct waiter_helper {
9399
}
94100
bool ret = true;
95101
IPC_UNUSED_ auto guard = ctrl.get_lock();
102+
clear_handshake(ctrl);
96103
if (counter.counter_ > 0) {
97104
ret = ctrl.sema_post(counter.counter_);
105+
auto tm = default_timeout / counter.counter_;
98106
do {
99107
counter.counter_ -= 1;
100-
ret = ret && ctrl.handshake_wait(default_timeout);
108+
ret = ret && ctrl.handshake_wait(tm);
101109
} while (counter.counter_ > 0);
102110
}
103111
return ret;
@@ -116,6 +124,7 @@ struct waiter_helper {
116124
}
117125
bool ret = true;
118126
IPC_UNUSED_ auto guard = ctrl.get_lock();
127+
clear_handshake(ctrl);
119128
if (counter.counter_ > 0) {
120129
ret = ctrl.sema_post(counter.counter_);
121130
counter.counter_ -= 1;

0 commit comments

Comments
 (0)