adjust naming

This commit is contained in:
mutouyun 2019-02-13 07:09:10 +08:00
parent ed6af29264
commit fd92e94e51

View File

@ -68,56 +68,58 @@ public:
} }
private: private:
semaphore& s(handle_t& h) { return std::get<0>(h); } semaphore& sem(handle_t& h) { return std::get<0>(h); }
semaphore& w(handle_t& h) { return std::get<1>(h); } semaphore& han(handle_t& h) { return std::get<1>(h); }
mutex & x(handle_t& h) { return std::get<2>(h); } mutex & mtx(handle_t& h) { return std::get<2>(h); }
public: public:
handle_t open(char const * name) { handle_t open(char const * name) {
if (name == nullptr || name[0] == '\0') return invalid(); if (name == nullptr || name[0] == '\0') return invalid();
std::string n = name; std::string n = name;
return handle_t { return handle_t {
semaphore {}.open(ipc::detail::to_tchar(n + "__S__")), semaphore {}.open(ipc::detail::to_tchar(n + "__SEM__")),
semaphore {}.open(ipc::detail::to_tchar(n + "__W__")), semaphore {}.open(ipc::detail::to_tchar(n + "__HAN__")),
mutex {}.open(ipc::detail::to_tchar(n + "__X__")) mutex {}.open(ipc::detail::to_tchar(n + "__MTX__"))
}; };
} }
void close(handle_t& h) { void close(handle_t& h) {
if (h == invalid()) return; if (h == invalid()) return;
x(h).close(); mtx(h).close();
w(h).close(); han(h).close();
s(h).close(); sem(h).close();
} }
template <typename F> template <typename F>
bool wait_if(handle_t& h, F&& pred) { bool wait_if(handle_t& h, F&& pred) {
if (h == invalid()) return false; if (h == invalid()) return false;
{ {
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(x(h)); IPC_UNUSED_ auto guard = ipc::detail::unique_lock(mtx(h));
if (!std::forward<F>(pred)()) return true; if (!std::forward<F>(pred)()) return true;
++ counter_; ++ counter_;
} }
return s(h).wait() && w(h).post(); bool ret_s = sem(h).wait();
bool ret_h = han(h).post();
return ret_s && ret_h;
} }
void notify(handle_t& h) { void notify(handle_t& h) {
if (h == invalid()) return; if (h == invalid()) return;
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(x(h)); IPC_UNUSED_ auto guard = ipc::detail::unique_lock(mtx(h));
if (counter_ > 0) { if (counter_ > 0) {
s(h).post(); sem(h).post();
-- counter_; -- counter_;
w(h).wait(); han(h).wait();
} }
} }
void broadcast(handle_t& h) { void broadcast(handle_t& h) {
if (h == invalid()) return; if (h == invalid()) return;
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(x(h)); IPC_UNUSED_ auto guard = ipc::detail::unique_lock(mtx(h));
s(h).post(counter_); sem(h).post(counter_);
while (counter_ > 0) { while (counter_ > 0) {
-- counter_; -- counter_;
w(h).wait(); han(h).wait();
} }
} }
}; };