fix errors

This commit is contained in:
mutouyun 2021-06-21 00:00:11 +08:00
parent 2d0948c042
commit 40eafcfd2a

View File

@ -37,15 +37,15 @@ public:
}
bool valid() const noexcept {
static const tmp[sizeof pthread_mutex_t] {};
static const tmp[sizeof(pthread_mutex_t)] {};
return shm_.valid()
&& (mutex_ != nullptr)
&& (std::memcmp(tmp, mutex_, sizeof pthread_mutex_t) != 0);
&& (std::memcmp(tmp, mutex_, sizeof(pthread_mutex_t)) != 0);
}
bool open(char const *name) noexcept {
close();
if (!shm_.acquire(name, sizeof pthread_mutex_t)) {
if (!shm_.acquire(name, sizeof(pthread_mutex_t))) {
ipc::error("fail shm.acquire: %s\n", name);
return false;
}
@ -93,15 +93,16 @@ public:
bool lock(std::uint64_t tm) noexcept {
for (;;) {
auto ts = detail::make_timespec(tm);
int eno = (tm == invalid_value)
? ::pthread_mutex_lock(mutex_)
: ::pthread_mutex_timedlock(mutex_, detail::make_timespec(tm));
: ::pthread_mutex_timedlock(mutex_, &ts);
switch (eno) {
case 0:
return true;
case ETIMEDOUT:
return false;
case EOWNERDEAD:
case EOWNERDEAD: {
if (shm_.ref() > 1) {
shm_.sub_ref();
}
@ -115,6 +116,7 @@ public:
ipc::error("fail pthread_mutex_lock[%d], pthread_mutex_unlock[%d]\n", eno, eno3);
return false;
}
}
break; // loop again
default:
ipc::error("fail pthread_mutex_lock[%d]\n", eno);
@ -124,13 +126,14 @@ public:
}
bool try_lock() noexcept(false) {
int eno = ::pthread_mutex_timedlock(mutex_, detail::make_timespec(0));
auto ts = detail::make_timespec(0);
int eno = ::pthread_mutex_timedlock(mutex_, &ts);
switch (eno) {
case 0:
return true;
case ETIMEDOUT:
return false;
case EOWNERDEAD:
case EOWNERDEAD: {
if (shm_.ref() > 1) {
shm_.sub_ref();
}
@ -144,6 +147,7 @@ public:
ipc::error("fail pthread_mutex_timedlock[%d], pthread_mutex_unlock[%d]\n", eno, eno3);
break;
}
}
break;
default:
ipc::error("fail pthread_mutex_timedlock[%d]\n", eno);