|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cstdint> |
| 4 | + |
| 5 | +#include <fcntl.h> /* For O_* constants */ |
| 6 | +#include <sys/stat.h> /* For mode constants */ |
| 7 | +#include <semaphore.h> |
| 8 | +#include <errno.h> |
| 9 | + |
| 10 | +#include "libipc/utility/log.h" |
| 11 | +#include "libipc/platform/get_wait_time.h" |
| 12 | +#include "libipc/shm.h" |
| 13 | + |
| 14 | +namespace ipc { |
| 15 | +namespace detail { |
| 16 | +namespace sync { |
| 17 | + |
| 18 | +class semaphore { |
| 19 | + ipc::shm::handle shm_; |
| 20 | + sem_t *h_ = SEM_FAILED; |
| 21 | + |
| 22 | +public: |
| 23 | + semaphore() noexcept = default; |
| 24 | + ~semaphore() noexcept = default; |
| 25 | + |
| 26 | + sem_t *native() const noexcept { |
| 27 | + return h_; |
| 28 | + } |
| 29 | + |
| 30 | + bool valid() const noexcept { |
| 31 | + return h_ != SEM_FAILED; |
| 32 | + } |
| 33 | + |
| 34 | + bool open(char const *name, std::uint32_t count) noexcept { |
| 35 | + close(); |
| 36 | + if (!shm_.acquire(name, 0)) { |
| 37 | + ipc::error("[open_semaphore] fail shm.acquire: %s\n", name); |
| 38 | + return false; |
| 39 | + } |
| 40 | + h_ = ::sem_open(name, O_CREAT, 0666, static_cast<unsigned>(count)); |
| 41 | + if (h_ == SEM_FAILED) { |
| 42 | + ipc::error("fail sem_open[%d]: %s\n", errno, name); |
| 43 | + return false; |
| 44 | + } |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + void close() noexcept { |
| 49 | + if (!valid()) return; |
| 50 | + if (::sem_close(h_) != 0) { |
| 51 | + ipc::error("fail sem_close[%d]: %s\n", errno); |
| 52 | + } |
| 53 | + if (shm_.ref() == 1) { |
| 54 | + if (::sem_unlink(shm_.name()) != 0) { |
| 55 | + ipc::error("fail sem_unlink[%d]: %s\n", errno); |
| 56 | + } |
| 57 | + } |
| 58 | + shm_.release(); |
| 59 | + h_ = SEM_FAILED; |
| 60 | + } |
| 61 | + |
| 62 | + bool wait(std::uint64_t tm) noexcept { |
| 63 | + if (h == invalid()) return false; |
| 64 | + switch (tm) { |
| 65 | + case 0: |
| 66 | + return true; |
| 67 | + case invalid_value: |
| 68 | + if (::sem_wait(h_) != 0) { |
| 69 | + ipc::error("fail sem_wait[%d]: %s\n", errno); |
| 70 | + return false; |
| 71 | + } |
| 72 | + return true; |
| 73 | + default: { |
| 74 | + auto ts = detail::make_timespec(tm); |
| 75 | + if (::sem_timedwait(h, &ts) != 0) { |
| 76 | + if (errno != ETIMEDOUT) { |
| 77 | + ipc::error("fail sem_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n", |
| 78 | + errno, tm, ts.tv_sec, ts.tv_nsec); |
| 79 | + } |
| 80 | + return false; |
| 81 | + } |
| 82 | + } |
| 83 | + return true; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + bool post(std::uint32_t count) noexcept { |
| 88 | + if (h_ == invalid()) return false; |
| 89 | + for (std::uint32_t i = 0; i < count; ++i) { |
| 90 | + if (::sem_post(h_) != 0) { |
| 91 | + ipc::error("fail sem_post[%d]: %s\n", errno); |
| 92 | + return false; |
| 93 | + } |
| 94 | + } |
| 95 | + return true; |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | +} // namespace sync |
| 100 | +} // namespace detail |
| 101 | +} // namespace ipc |
0 commit comments