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 {
close();
if (!shm_.acquire(name, 0)) {
if (!shm_.acquire(name, 1)) {
ipc::error("[open_semaphore] fail shm.acquire: %s\n", name);
return false;
}
@ -61,16 +61,12 @@ public:
bool wait(std::uint64_t tm) noexcept {
if (!valid()) return false;
switch (tm) {
case 0:
return true;
case invalid_value:
if (tm == invalid_value) {
if (::sem_wait(h_) != 0) {
ipc::error("fail sem_wait[%d]: %s\n", errno);
return false;
}
return true;
default: {
} else {
auto ts = detail::make_timespec(tm);
if (::sem_timedwait(h_, &ts) != 0) {
if (errno != ETIMEDOUT) {
@ -82,7 +78,6 @@ public:
}
return true;
}
}
bool post(std::uint32_t count) noexcept {
if (!valid()) return false;

View File

@ -117,7 +117,7 @@ void * get_mem(id_t id, std::size_t * size) {
}
int fd = ii->fd_;
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;
}
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);
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;
}
}