From a9cb81bee9c3f82515c5a1713e2cc905021efc8b Mon Sep 17 00:00:00 2001 From: mutouyun Date: Mon, 20 Sep 2021 16:05:35 +0800 Subject: [PATCH] missing file --- src/libipc/waiter.h | 70 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/libipc/waiter.h diff --git a/src/libipc/waiter.h b/src/libipc/waiter.h new file mode 100644 index 0000000..6c2cb45 --- /dev/null +++ b/src/libipc/waiter.h @@ -0,0 +1,70 @@ +#pragma once + +#include +#include +#include + +#include "libipc/def.h" +#include "libipc/mutex.h" +#include "libipc/condition.h" +#include "libipc/platform/detail.h" + +namespace ipc { +namespace detail { + +class waiter { + ipc::sync::condition cond_; + ipc::sync::mutex lock_; + +public: + waiter() = default; + waiter(char const *name) { + open(name); + } + + ~waiter() { + close(); + } + + bool valid() const noexcept { + return cond_.valid() && lock_.valid(); + } + + bool open(char const *name) noexcept { + if (!cond_.open((std::string{"_waiter_cond_"} + name).c_str())) { + return false; + } + if (!lock_.open((std::string{"_waiter_lock_"} + name).c_str())) { + cond_.close(); + return false; + } + return valid(); + } + + void close() noexcept { + cond_.close(); + lock_.close(); + } + + template + bool wait_if(F &&pred, std::uint64_t tm = ipc::invalid_value) noexcept { + IPC_UNUSED_ std::lock_guard guard {lock_}; + while (std::forward(pred)()) { + if (!cond_.wait(lock_, tm)) return false; + } + return true; + } + + bool notify() noexcept { + std::lock_guard{lock_}; // barrier + return cond_.notify(); + } + + bool broadcast() noexcept { + std::lock_guard{lock_}; // barrier + return cond_.broadcast(); + } +}; + +} // namespace detail +} // namespace ipc