for linux

This commit is contained in:
mutouyun 2020-09-20 11:57:27 +08:00
parent e51855f1df
commit 1323dc736b
3 changed files with 29 additions and 20 deletions

View File

@ -258,20 +258,23 @@ public:
} }
private: private:
using wait_flags = waiter_helper::wait_flags;
using wait_counter = waiter_helper::wait_counter;
mutex lock_; mutex lock_;
waiter_helper::wait_counter cnt_; wait_counter cnt_;
struct contrl { struct contrl {
waiter_holder * me_; waiter_holder * me_;
waiter_helper::wait_flags * flags_; wait_flags * flags_;
handle_t const & h_; handle_t const & h_;
waiter_helper::wait_flags & flags() noexcept { wait_flags & flags() noexcept {
assert(flags_ != nullptr); assert(flags_ != nullptr);
return *flags_; return *flags_;
} }
waiter_helper::wait_counter & counter() noexcept { wait_counter & counter() noexcept {
return me_->cnt_; return me_->cnt_;
} }
@ -297,8 +300,6 @@ private:
}; };
public: public:
using wait_flags = waiter_helper::wait_flags;
handle_t open_h(ipc::string && name) { handle_t open_h(ipc::string && name) {
auto sem = sem_helper::open(("__WAITER_HELPER_SEM__" + name).c_str(), 0); auto sem = sem_helper::open(("__WAITER_HELPER_SEM__" + name).c_str(), 0);
if (sem == sem_helper::invalid()) { if (sem == sem_helper::invalid()) {
@ -334,7 +335,14 @@ public:
bool wait_if(handle_t const & h, wait_flags * flags, F&& pred, std::size_t tm = invalid_value) { bool wait_if(handle_t const & h, wait_flags * flags, F&& pred, std::size_t tm = invalid_value) {
assert(flags != nullptr); assert(flags != nullptr);
contrl ctrl { this, flags, h }; contrl ctrl { this, flags, h };
return waiter_helper::wait_if(ctrl, mtx, std::forward<F>(pred), tm);
class non_mutex {
public:
void lock () noexcept {}
void unlock() noexcept {}
} nm;
return waiter_helper::wait_if(ctrl, nm, std::forward<F>(pred), tm);
} }
bool notify(handle_t const & h) { bool notify(handle_t const & h) {
@ -385,7 +393,7 @@ public:
} }
template <typename F> template <typename F>
bool wait_if(handle_t h, waiter_holder::wait_flags * flags, F && pred, std::size_t tm = invalid_value) { bool wait_if(handle_t h, waiter_helper::wait_flags * flags, F && pred, std::size_t tm = invalid_value) {
if (h == invalid()) return false; if (h == invalid()) return false;
return helper_.wait_if(h, flags, std::forward<F>(pred), tm); return helper_.wait_if(h, flags, std::forward<F>(pred), tm);
} }
@ -400,7 +408,7 @@ public:
return helper_.broadcast(h); return helper_.broadcast(h);
} }
bool quit_waiting(handle_t h, waiter_holder::wait_flags * flags) { bool quit_waiting(handle_t h, waiter_helper::wait_flags * flags) {
if (h == invalid()) return false; if (h == invalid()) return false;
return helper_.quit_waiting(h, flags); return helper_.quit_waiting(h, flags);
} }

View File

@ -78,20 +78,23 @@ public:
}; };
class condition { class condition {
using wait_flags = waiter_helper::wait_flags;
using wait_counter = waiter_helper::wait_counter;
mutex lock_; mutex lock_;
semaphore sema_, handshake_; semaphore sema_, handshake_;
waiter_helper::wait_counter * cnt_ = nullptr; wait_counter * cnt_ = nullptr;
struct contrl { struct contrl {
condition * me_; condition * me_;
waiter_helper::wait_flags * flags_; wait_flags * flags_;
waiter_helper::wait_flags & flags() noexcept { wait_flags & flags() noexcept {
assert(flags_ != nullptr); assert(flags_ != nullptr);
return *flags_; return *flags_;
} }
waiter_helper::wait_counter & counter() noexcept { wait_counter & counter() noexcept {
assert(me_->cnt_ != nullptr); assert(me_->cnt_ != nullptr);
return *(me_->cnt_); return *(me_->cnt_);
} }
@ -118,8 +121,6 @@ class condition {
}; };
public: public:
using wait_flags = waiter_helper::wait_flags;
friend bool operator==(condition const & c1, condition const & c2) { friend bool operator==(condition const & c1, condition const & c2) {
return c1.cnt_ == c2.cnt_; return c1.cnt_ == c2.cnt_;
} }
@ -134,7 +135,7 @@ public:
mutex ::remove((ipc::string{ "__COND_MTX__" } + name).c_str()); mutex ::remove((ipc::string{ "__COND_MTX__" } + name).c_str());
} }
bool open(ipc::string const & name, waiter_helper::wait_counter * cnt) { bool open(ipc::string const & name, wait_counter * cnt) {
if (lock_ .open("__COND_MTX__" + name) && if (lock_ .open("__COND_MTX__" + name) &&
sema_ .open("__COND_SEM__" + name) && sema_ .open("__COND_SEM__" + name) &&
handshake_.open("__COND_HAN__" + name)) { handshake_.open("__COND_HAN__" + name)) {
@ -201,7 +202,7 @@ public:
} }
template <typename F> template <typename F>
bool wait_if(handle_t& h, handle_t::wait_flags * flags, F&& pred, std::size_t tm = invalid_value) { bool wait_if(handle_t& h, waiter_helper::wait_flags * flags, F&& pred, std::size_t tm = invalid_value) {
if (h == invalid()) return false; if (h == invalid()) return false;
class non_mutex { class non_mutex {
@ -223,7 +224,7 @@ public:
return h.broadcast(); return h.broadcast();
} }
bool quit_waiting(handle_t& h, handle_t::wait_flags * flags) { bool quit_waiting(handle_t& h, waiter_helper::wait_flags * flags) {
if (h == invalid()) return false; if (h == invalid()) return false;
return h.quit_waiting(flags); return h.quit_waiting(flags);
} }

View File

@ -24,7 +24,7 @@ class condition_impl : public ipc::detail::condition {
using base_t = ipc::detail::condition; using base_t = ipc::detail::condition;
ipc::shm::handle cnt_h_; ipc::shm::handle cnt_h_;
base_t::wait_flags flags_; waiter_helper::wait_flags flags_;
public: public:
static void remove(char const * name) { static void remove(char const * name) {
@ -192,7 +192,7 @@ public:
private: private:
waiter_t* w_ = nullptr; waiter_t* w_ = nullptr;
waiter_t::handle_t h_ = waiter_t::invalid(); waiter_t::handle_t h_ = waiter_t::invalid();
waiter_t::handle_t::wait_flags flags_; waiter_helper::wait_flags flags_;
public: public:
waiter_wrapper() = default; waiter_wrapper() = default;