Skip to content

Commit ca9c5d1

Browse files
committed
ut for sem-linux
1 parent 1994243 commit ca9c5d1

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

src/libipc/platform/semaphore_linux.h

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class semaphore {
3333

3434
bool open(char const *name, std::uint32_t count) noexcept {
3535
close();
36-
if (!shm_.acquire(name, 0)) {
36+
if (!shm_.acquire(name, 1)) {
3737
ipc::error("[open_semaphore] fail shm.acquire: %s\n", name);
3838
return false;
3939
}
@@ -61,27 +61,22 @@ class semaphore {
6161

6262
bool wait(std::uint64_t tm) noexcept {
6363
if (!valid()) return false;
64-
switch (tm) {
65-
case 0:
66-
return true;
67-
case invalid_value:
64+
if (tm == invalid_value) {
6865
if (::sem_wait(h_) != 0) {
6966
ipc::error("fail sem_wait[%d]: %s\n", errno);
7067
return false;
7168
}
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;
69+
} else {
70+
auto ts = detail::make_timespec(tm);
71+
if (::sem_timedwait(h_, &ts) != 0) {
72+
if (errno != ETIMEDOUT) {
73+
ipc::error("fail sem_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
74+
errno, tm, ts.tv_sec, ts.tv_nsec);
8175
}
76+
return false;
8277
}
83-
return true;
8478
}
79+
return true;
8580
}
8681

8782
bool post(std::uint32_t count) noexcept {

src/libipc/platform/shm_linux.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void * get_mem(id_t id, std::size_t * size) {
117117
}
118118
int fd = ii->fd_;
119119
if (fd == -1) {
120-
ipc::error("fail to_mem: invalid id (fd = -1)\n");
120+
ipc::error("fail get_mem: invalid id (fd = -1)\n");
121121
return nullptr;
122122
}
123123
if (ii->size_ == 0) {
@@ -128,7 +128,7 @@ void * get_mem(id_t id, std::size_t * size) {
128128
}
129129
ii->size_ = static_cast<std::size_t>(st.st_size);
130130
if ((ii->size_ <= sizeof(info_t)) || (ii->size_ % sizeof(info_t))) {
131-
ipc::error("fail to_mem: %s, invalid size = %zd\n", ii->name_.c_str(), ii->size_);
131+
ipc::error("fail get_mem: %s, invalid size = %zd\n", ii->name_.c_str(), ii->size_);
132132
return nullptr;
133133
}
134134
}

0 commit comments

Comments
 (0)