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