|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cstdint> |
| 4 | +#include <cstring> |
| 5 | + |
| 6 | +#include <pthread.h> |
| 7 | + |
| 8 | +#include "libipc/platform/get_wait_time.h" |
| 9 | +#include "libipc/utility/log.h" |
| 10 | +#include "libipc/utility/scope_guard.h" |
| 11 | +#include "libipc/mutex.h" |
| 12 | +#include "libipc/shm.h" |
| 13 | + |
| 14 | +namespace ipc { |
| 15 | +namespace detail { |
| 16 | +namespace sync { |
| 17 | + |
| 18 | +class condition { |
| 19 | + ipc::shm::handle shm_; |
| 20 | + pthread_cond_t *cond_ = nullptr; |
| 21 | + |
| 22 | + pthread_cond_t *acquire_cond(char const *name) { |
| 23 | + if (!shm_.acquire(name, sizeof(pthread_cond_t))) { |
| 24 | + ipc::error("[acquire_cond] fail shm.acquire: %s\n", name); |
| 25 | + return nullptr; |
| 26 | + } |
| 27 | + return static_cast<pthread_cond_t *>(shm_.get()); |
| 28 | + } |
| 29 | + |
| 30 | +public: |
| 31 | + condition() = default; |
| 32 | + ~condition() = default; |
| 33 | + |
| 34 | + pthread_cond_t const *native() const noexcept { |
| 35 | + return cond_; |
| 36 | + } |
| 37 | + |
| 38 | + pthread_cond_t *native() noexcept { |
| 39 | + return cond_; |
| 40 | + } |
| 41 | + |
| 42 | + bool valid() const noexcept { |
| 43 | + static const char tmp[sizeof(pthread_cond_t)] {}; |
| 44 | + return (cond_ != nullptr) |
| 45 | + && (std::memcmp(tmp, cond_, sizeof(pthread_cond_t)) != 0); |
| 46 | + } |
| 47 | + |
| 48 | + bool open(char const *name) noexcept { |
| 49 | + close(); |
| 50 | + if ((cond_ = acquire_cond(name)) == nullptr) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + if (shm_.ref() > 1) { |
| 54 | + return valid(); |
| 55 | + } |
| 56 | + ::pthread_cond_destroy(cond_); |
| 57 | + auto finally = ipc::guard([this] { close(); }); // close when failed |
| 58 | + // init condition |
| 59 | + int eno; |
| 60 | + pthread_condattr_t cond_attr; |
| 61 | + if ((eno = ::pthread_condattr_init(&cond_attr)) != 0) { |
| 62 | + ipc::error("fail pthread_condattr_init[%d]\n", eno); |
| 63 | + return false; |
| 64 | + } |
| 65 | + IPC_UNUSED_ auto guard_cond_attr = unique_ptr(&cond_attr, ::pthread_condattr_destroy); |
| 66 | + if ((eno = ::pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED)) != 0) { |
| 67 | + ipc::error("fail pthread_condattr_setpshared[%d]\n", eno); |
| 68 | + return false; |
| 69 | + } |
| 70 | + *cond_ = PTHREAD_COND_INITIALIZER; |
| 71 | + if ((eno = ::pthread_cond_init(cond_, &cond_attr)) != 0) { |
| 72 | + ipc::error("fail pthread_cond_init[%d]\n", eno); |
| 73 | + return false; |
| 74 | + } |
| 75 | + finally.dismiss(); |
| 76 | + return valid(); |
| 77 | + } |
| 78 | + |
| 79 | + void close() noexcept { |
| 80 | + if ((shm_.ref() <= 1) && cond_ != nullptr) { |
| 81 | + int eno; |
| 82 | + if ((eno = ::pthread_cond_destroy(cond_)) != 0) { |
| 83 | + ipc::error("fail pthread_cond_destroy[%d]\n", eno); |
| 84 | + } |
| 85 | + } |
| 86 | + shm_.release(); |
| 87 | + cond_ = nullptr; |
| 88 | + } |
| 89 | + |
| 90 | + bool wait(ipc::sync::mutex &mtx, std::uint64_t tm) noexcept { |
| 91 | + if (!valid()) return false; |
| 92 | + switch (tm) { |
| 93 | + case invalid_value: { |
| 94 | + int eno; |
| 95 | + if ((eno = ::pthread_cond_wait(cond_, static_cast<pthread_mutex_t *>(mtx.native()))) != 0) { |
| 96 | + ipc::error("fail pthread_cond_wait[%d]\n", eno); |
| 97 | + return false; |
| 98 | + } |
| 99 | + } |
| 100 | + break; |
| 101 | + default: { |
| 102 | + auto ts = detail::make_timespec(tm); |
| 103 | + int eno; |
| 104 | + if ((eno = ::pthread_cond_timedwait(cond_, static_cast<pthread_mutex_t *>(mtx.native()), &ts)) != 0) { |
| 105 | + if (eno != ETIMEDOUT) { |
| 106 | + ipc::error("fail pthread_cond_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n", |
| 107 | + eno, tm, ts.tv_sec, ts.tv_nsec); |
| 108 | + } |
| 109 | + return false; |
| 110 | + } |
| 111 | + } |
| 112 | + break; |
| 113 | + } |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + bool notify() noexcept { |
| 118 | + if (!valid()) return false; |
| 119 | + int eno; |
| 120 | + if ((eno = ::pthread_cond_signal(cond_)) != 0) { |
| 121 | + ipc::error("fail pthread_cond_signal[%d]\n", eno); |
| 122 | + return false; |
| 123 | + } |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + bool broadcast() noexcept { |
| 128 | + if (!valid()) return false; |
| 129 | + int eno; |
| 130 | + if ((eno = ::pthread_cond_broadcast(cond_)) != 0) { |
| 131 | + ipc::error("fail pthread_cond_broadcast[%d]\n", eno); |
| 132 | + return false; |
| 133 | + } |
| 134 | + return true; |
| 135 | + } |
| 136 | +}; |
| 137 | + |
| 138 | +} // namespace sync |
| 139 | +} // namespace detail |
| 140 | +} // namespace ipc |
0 commit comments