ut for sem-linux

This commit is contained in:
mutouyun 2021-09-12 22:05:08 +08:00
parent 1994243bec
commit ca9c5d10da
2 changed files with 12 additions and 17 deletions

View File

@ -33,7 +33,7 @@ public:
bool open(char const *name, std::uint32_t count) noexcept { bool open(char const *name, std::uint32_t count) noexcept {
close(); close();
if (!shm_.acquire(name, 0)) { if (!shm_.acquire(name, 1)) {
ipc::error("[open_semaphore] fail shm.acquire: %s\n", name); ipc::error("[open_semaphore] fail shm.acquire: %s\n", name);
return false; return false;
} }
@ -61,27 +61,22 @@ public:
bool wait(std::uint64_t tm) noexcept { bool wait(std::uint64_t tm) noexcept {
if (!valid()) return false; if (!valid()) return false;
switch (tm) { if (tm == invalid_value) {
case 0:
return true;
case invalid_value:
if (::sem_wait(h_) != 0) { if (::sem_wait(h_) != 0) {
ipc::error("fail sem_wait[%d]: %s\n", errno); ipc::error("fail sem_wait[%d]: %s\n", errno);
return false; return false;
} }
return true; } else {
default: { auto ts = detail::make_timespec(tm);
auto ts = detail::make_timespec(tm); if (::sem_timedwait(h_, &ts) != 0) {
if (::sem_timedwait(h_, &ts) != 0) { if (errno != ETIMEDOUT) {
if (errno != ETIMEDOUT) { ipc::error("fail sem_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
ipc::error("fail sem_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n", errno, tm, ts.tv_sec, ts.tv_nsec);
errno, tm, ts.tv_sec, ts.tv_nsec);
}
return false;
} }
return false;
} }
return true;
} }
return true;
} }
bool post(std::uint32_t count) noexcept { bool post(std::uint32_t count) noexcept {

View File

@ -117,7 +117,7 @@ void * get_mem(id_t id, std::size_t * size) {
} }
int fd = ii->fd_; int fd = ii->fd_;
if (fd == -1) { if (fd == -1) {
ipc::error("fail to_mem: invalid id (fd = -1)\n"); ipc::error("fail get_mem: invalid id (fd = -1)\n");
return nullptr; return nullptr;
} }
if (ii->size_ == 0) { if (ii->size_ == 0) {
@ -128,7 +128,7 @@ void * get_mem(id_t id, std::size_t * size) {
} }
ii->size_ = static_cast<std::size_t>(st.st_size); ii->size_ = static_cast<std::size_t>(st.st_size);
if ((ii->size_ <= sizeof(info_t)) || (ii->size_ % sizeof(info_t))) { if ((ii->size_ <= sizeof(info_t)) || (ii->size_ % sizeof(info_t))) {
ipc::error("fail to_mem: %s, invalid size = %zd\n", ii->name_.c_str(), ii->size_); ipc::error("fail get_mem: %s, invalid size = %zd\n", ii->name_.c_str(), ii->size_);
return nullptr; return nullptr;
} }
} }