diff --git a/src/libipc/platform/waiter_linux.h b/src/libipc/platform/waiter_linux.h index eb9a3b1..0efc20e 100755 --- a/src/libipc/platform/waiter_linux.h +++ b/src/libipc/platform/waiter_linux.h @@ -224,8 +224,6 @@ public: static bool wait(handle_t h, std::size_t tm = invalid_value) { if (h == invalid()) return false; switch (tm) { - case 0: - return true; case invalid_value: IPC_SEMAPHORE_FUNC_(sem_wait, h); default: { diff --git a/src/libipc/waiter_helper.h b/src/libipc/waiter_helper.h index a32035b..f866cf1 100644 --- a/src/libipc/waiter_helper.h +++ b/src/libipc/waiter_helper.h @@ -13,7 +13,7 @@ namespace detail { struct waiter_helper { struct wait_counter { - std::atomic waiting_ { 0 }; + std::atomic waiting_ { 0 }; long counter_ = 0; }; @@ -40,7 +40,7 @@ struct waiter_helper { { IPC_UNUSED_ auto guard = ctrl.get_lock(); if (!std::forward(pred)()) return true; - counter.counter_ += 1; + counter.counter_ = counter.waiting_.load(std::memory_order_relaxed); } mtx.unlock(); @@ -69,6 +69,11 @@ struct waiter_helper { return ret; } + template + static void clear_handshake(Ctrl & ctrl) { + while (ctrl.handshake_wait(0)) ; + } + template static bool notify(Ctrl & ctrl) { auto & counter = ctrl.counter(); @@ -77,6 +82,7 @@ struct waiter_helper { } bool ret = true; IPC_UNUSED_ auto guard = ctrl.get_lock(); + clear_handshake(ctrl); if (counter.counter_ > 0) { ret = ctrl.sema_post(1); counter.counter_ -= 1; @@ -93,11 +99,13 @@ struct waiter_helper { } bool ret = true; IPC_UNUSED_ auto guard = ctrl.get_lock(); + clear_handshake(ctrl); if (counter.counter_ > 0) { ret = ctrl.sema_post(counter.counter_); + auto tm = default_timeout / counter.counter_; do { counter.counter_ -= 1; - ret = ret && ctrl.handshake_wait(default_timeout); + ret = ret && ctrl.handshake_wait(tm); } while (counter.counter_ > 0); } return ret; @@ -116,6 +124,7 @@ struct waiter_helper { } bool ret = true; IPC_UNUSED_ auto guard = ctrl.get_lock(); + clear_handshake(ctrl); if (counter.counter_ > 0) { ret = ctrl.sema_post(counter.counter_); counter.counter_ -= 1;