From 578645a1984853a7a1639df8bfbbd1abe1d4c36a Mon Sep 17 00:00:00 2001 From: Nick Kelsey Date: Fri, 29 Apr 2016 19:00:34 -0700 Subject: [PATCH] fix signal handling on non-windows platforms --- hdhomerun_os_posix.c | 32 +++++++++++++++++++++++--------- hdhomerun_os_posix.h | 2 +- hdhomerun_os_windows.h | 1 - 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/hdhomerun_os_posix.c b/hdhomerun_os_posix.c index 604d5e4..ffb2458 100644 --- a/hdhomerun_os_posix.c +++ b/hdhomerun_os_posix.c @@ -126,6 +126,7 @@ void msleep_minimum(uint64_t ms) void thread_cond_init(thread_cond_t *cond) { + cond->signaled = FALSE; pthread_mutex_init(&cond->lock, NULL); pthread_cond_init(&cond->cond, NULL); } @@ -137,28 +138,41 @@ void thread_cond_dispose(thread_cond_t *cond) void thread_cond_signal(thread_cond_t *cond) { pthread_mutex_lock(&cond->lock); + + cond->signaled = TRUE; pthread_cond_signal(&cond->cond); + pthread_mutex_unlock(&cond->lock); } void thread_cond_wait(thread_cond_t *cond) { pthread_mutex_lock(&cond->lock); - pthread_cond_wait(&cond->cond, &cond->lock); + + if (!cond->signaled) { + pthread_cond_wait(&cond->cond, &cond->lock); + } + + cond->signaled = FALSE; pthread_mutex_unlock(&cond->lock); } void thread_cond_wait_with_timeout(thread_cond_t *cond, uint64_t max_wait_time) { - struct timespec ts; - clock_realtime_timespec(&ts); - - uint64_t tv_nsec = (uint64_t)ts.tv_nsec + (max_wait_time * 1000000); - ts.tv_nsec = (long)(tv_nsec % 1000000000); - ts.tv_sec += (time_t)(tv_nsec / 1000000000); - pthread_mutex_lock(&cond->lock); - pthread_cond_timedwait(&cond->cond, &cond->lock, &ts); + + if (!cond->signaled) { + struct timespec ts; + clock_realtime_timespec(&ts); + + uint64_t tv_nsec = (uint64_t)ts.tv_nsec + (max_wait_time * 1000000); + ts.tv_nsec = (long)(tv_nsec % 1000000000); + ts.tv_sec += (time_t)(tv_nsec / 1000000000); + + pthread_cond_timedwait(&cond->cond, &cond->lock, &ts); + } + + cond->signaled = FALSE; pthread_mutex_unlock(&cond->lock); } diff --git a/hdhomerun_os_posix.h b/hdhomerun_os_posix.h index e20ee61..3b52854 100644 --- a/hdhomerun_os_posix.h +++ b/hdhomerun_os_posix.h @@ -30,7 +30,6 @@ #include #include #include -#include #include #include #include @@ -42,6 +41,7 @@ typedef uint8_t bool_t; typedef void (*sig_t)(int); typedef struct { + volatile bool_t signaled; pthread_mutex_t lock; pthread_cond_t cond; } thread_cond_t; diff --git a/hdhomerun_os_windows.h b/hdhomerun_os_windows.h index ed8a6a2..33811e2 100644 --- a/hdhomerun_os_windows.h +++ b/hdhomerun_os_windows.h @@ -44,7 +44,6 @@ #include #include #include -#include #ifdef LIBHDHOMERUN_DLLEXPORT #define LIBHDHOMERUN_API __declspec(dllexport)