mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
ut for sem-linux
This commit is contained in:
parent
1994243bec
commit
ca9c5d10da
@ -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,27 +61,22 @@ 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: {
|
||||
auto ts = detail::make_timespec(tm);
|
||||
if (::sem_timedwait(h_, &ts) != 0) {
|
||||
if (errno != ETIMEDOUT) {
|
||||
ipc::error("fail sem_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
|
||||
errno, tm, ts.tv_sec, ts.tv_nsec);
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
auto ts = detail::make_timespec(tm);
|
||||
if (::sem_timedwait(h_, &ts) != 0) {
|
||||
if (errno != ETIMEDOUT) {
|
||||
ipc::error("fail sem_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
|
||||
errno, tm, ts.tv_sec, ts.tv_nsec);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool post(std::uint32_t count) noexcept {
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user