clean up my pain

This commit is contained in:
Desour 2024-10-07 16:42:05 +02:00
parent 5f577e674e
commit 6d5194d495
2 changed files with 4 additions and 31 deletions

View File

@ -128,10 +128,8 @@ static void post(IPCChannelBuffer *buf) noexcept
// timeout is absolute (using cond_clockid) // timeout is absolute (using cond_clockid)
// returns false on timeout // returns false on timeout
static bool wait(IPCChannelBuffer *buf, const struct timespec *timeout, const char *timestr = nullptr) noexcept static bool wait(IPCChannelBuffer *buf, const struct timespec *timeout) noexcept
{ {
if (timestr)
errorstream << timestr << std::endl;
bool timed_out = false; bool timed_out = false;
pthread_mutex_lock(&buf->mutex); pthread_mutex_lock(&buf->mutex);
while (!buf->posted) { while (!buf->posted) {
@ -140,16 +138,8 @@ static bool wait(IPCChannelBuffer *buf, const struct timespec *timeout, const ch
if (err == ETIMEDOUT) { if (err == ETIMEDOUT) {
timed_out = true; timed_out = true;
break; break;
} else if (err == EINTR || err == EOWNERDEAD || err == ENOTRECOVERABLE || err == EPERM || err == EINVAL) {
continue;
} else if (err != 0) {
pthread_mutex_unlock(&buf->mutex);
auto msg = "err: " + std::to_string(err);
FATAL_ERROR(msg.c_str());
bool ret = wait(buf, timeout, msg.c_str());
errorstream << msg << std::endl;
return ret;
} }
FATAL_ERROR_IF(err != 0 && err != EINTR, "pthread_cond_timedwait failed");
} else { } else {
pthread_cond_wait(&buf->cond, &buf->mutex); pthread_cond_wait(&buf->cond, &buf->mutex);
} }
@ -187,8 +177,6 @@ static bool wait_in(IPCChannelEnd::Dir *dir, u64 timeout_ms_abs)
#else #else
struct timespec timeout; struct timespec timeout;
struct timespec *timeoutp = nullptr; struct timespec *timeoutp = nullptr;
char timestr[201];
timestr[0] = '\0';
if (timeout_ms_abs > 0) { if (timeout_ms_abs > 0) {
u64 tnow = porting::getTimeMs(); u64 tnow = porting::getTimeMs();
if (tnow > timeout_ms_abs) if (tnow > timeout_ms_abs)
@ -211,14 +199,9 @@ static bool wait_in(IPCChannelEnd::Dir *dir, u64 timeout_ms_abs)
timeout.tv_sec += 1; timeout.tv_sec += 1;
} }
timeoutp = &timeout; timeoutp = &timeout;
time_t timeout_tt = timeout.tv_sec + timeout.tv_nsec / 1000'000'000L;
tm timeout_tm;
localtime_r(&timeout_tt, &timeout_tm);
strftime(timestr, 200, "%F %T ", &timeout_tm);
} }
return wait(dir->buf_in, timeoutp, timestr); return wait(dir->buf_in, timeoutp);
#endif #endif
} }
@ -248,7 +231,6 @@ IPCChannelBuffer::IPCChannelBuffer()
#if defined(IPC_CHANNEL_IMPLEMENTATION_POSIX) #if defined(IPC_CHANNEL_IMPLEMENTATION_POSIX)
pthread_condattr_t condattr; pthread_condattr_t condattr;
pthread_mutexattr_t mutexattr; pthread_mutexattr_t mutexattr;
clockid_t cond_clockid;
if (pthread_condattr_init(&condattr) != 0) if (pthread_condattr_init(&condattr) != 0)
goto error_condattr_init; goto error_condattr_init;
if (pthread_mutexattr_init(&mutexattr) != 0) if (pthread_mutexattr_init(&mutexattr) != 0)
@ -257,26 +239,17 @@ IPCChannelBuffer::IPCChannelBuffer()
goto error_condattr_setpshared; goto error_condattr_setpshared;
if (pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED) != 0) if (pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED) != 0)
goto error_mutexattr_setpshared; goto error_mutexattr_setpshared;
if (pthread_condattr_getclock(&condattr, &cond_clockid) != 0)
goto error_condattr_getclock;
if (pthread_cond_init(&cond, &condattr) != 0) if (pthread_cond_init(&cond, &condattr) != 0)
goto error_cond_init; goto error_cond_init;
if (pthread_mutex_init(&mutex, &mutexattr) != 0) if (pthread_mutex_init(&mutex, &mutexattr) != 0)
goto error_mutex_init; goto error_mutex_init;
pthread_mutexattr_destroy(&mutexattr); pthread_mutexattr_destroy(&mutexattr);
pthread_condattr_destroy(&condattr); pthread_condattr_destroy(&condattr);
/*
{
std::string bla = std::string("realt: ") + std::to_string(CLOCK_REALTIME) + " cond_clockid: " + std::to_string(cond_clockid);
FATAL_ERROR(bla.c_str());
}*/
FATAL_ERROR_IF(cond_clockid != CLOCK_REALTIME, "wrong clock");
return; return;
error_mutex_init: error_mutex_init:
pthread_cond_destroy(&cond); pthread_cond_destroy(&cond);
error_cond_init: error_cond_init:
error_condattr_getclock:
error_mutexattr_setpshared: error_mutexattr_setpshared:
error_condattr_setpshared: error_condattr_setpshared:
pthread_mutexattr_destroy(&mutexattr); pthread_mutexattr_destroy(&mutexattr);

View File

@ -30,7 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#if defined(_WIN32) #if defined(_WIN32)
#define IPC_CHANNEL_IMPLEMENTATION_WIN32 #define IPC_CHANNEL_IMPLEMENTATION_WIN32
#elif defined(__linux__) && 0 #elif defined(__linux__)
#define IPC_CHANNEL_IMPLEMENTATION_LINUX_FUTEX #define IPC_CHANNEL_IMPLEMENTATION_LINUX_FUTEX
#else #else
#define IPC_CHANNEL_IMPLEMENTATION_POSIX #define IPC_CHANNEL_IMPLEMENTATION_POSIX