mirror of
https://github.com/Silicondust/libhdhomerun
synced 2025-08-19 21:13:25 -07:00
fix signal handling on non-windows platforms
This commit is contained in:
parent
7e9d651b5f
commit
578645a198
3 changed files with 24 additions and 11 deletions
|
@ -126,6 +126,7 @@ void msleep_minimum(uint64_t ms)
|
||||||
|
|
||||||
void thread_cond_init(thread_cond_t *cond)
|
void thread_cond_init(thread_cond_t *cond)
|
||||||
{
|
{
|
||||||
|
cond->signaled = FALSE;
|
||||||
pthread_mutex_init(&cond->lock, NULL);
|
pthread_mutex_init(&cond->lock, NULL);
|
||||||
pthread_cond_init(&cond->cond, 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)
|
void thread_cond_signal(thread_cond_t *cond)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&cond->lock);
|
pthread_mutex_lock(&cond->lock);
|
||||||
|
|
||||||
|
cond->signaled = TRUE;
|
||||||
pthread_cond_signal(&cond->cond);
|
pthread_cond_signal(&cond->cond);
|
||||||
|
|
||||||
pthread_mutex_unlock(&cond->lock);
|
pthread_mutex_unlock(&cond->lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void thread_cond_wait(thread_cond_t *cond)
|
void thread_cond_wait(thread_cond_t *cond)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&cond->lock);
|
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);
|
pthread_mutex_unlock(&cond->lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void thread_cond_wait_with_timeout(thread_cond_t *cond, uint64_t max_wait_time)
|
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_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);
|
pthread_mutex_unlock(&cond->lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,6 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/timeb.h>
|
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
@ -42,6 +41,7 @@ typedef uint8_t bool_t;
|
||||||
typedef void (*sig_t)(int);
|
typedef void (*sig_t)(int);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
volatile bool_t signaled;
|
||||||
pthread_mutex_t lock;
|
pthread_mutex_t lock;
|
||||||
pthread_cond_t cond;
|
pthread_cond_t cond;
|
||||||
} thread_cond_t;
|
} thread_cond_t;
|
||||||
|
|
|
@ -44,7 +44,6 @@
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/timeb.h>
|
|
||||||
|
|
||||||
#ifdef LIBHDHOMERUN_DLLEXPORT
|
#ifdef LIBHDHOMERUN_DLLEXPORT
|
||||||
#define LIBHDHOMERUN_API __declspec(dllexport)
|
#define LIBHDHOMERUN_API __declspec(dllexport)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue