From 4a122b57a048bc63c85896e184ebce445b97c03e Mon Sep 17 00:00:00 2001 From: Andrey Malets Date: Mon, 22 Jan 2018 18:38:52 +0500 Subject: [PATCH] Add and use a define to check if pthread_cancel is available. --- configure.ac | 1 + src/sb_thread.c | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index 371995a..86c4280 100644 --- a/configure.ac +++ b/configure.ac @@ -340,6 +340,7 @@ isatty \ memalign \ memset \ posix_memalign \ +pthread_cancel \ pthread_yield \ setvbuf \ sqrt \ diff --git a/src/sb_thread.c b/src/sb_thread.c index f37aafd..cd868e7 100644 --- a/src/sb_thread.c +++ b/src/sb_thread.c @@ -33,7 +33,9 @@ # include #endif +#ifndef HAVE_PTHREAD_CANCEL #include +#endif #include "sb_thread.h" #include "sb_rand.h" @@ -86,6 +88,9 @@ void sb_thread_done(void) free(threads); } +#ifndef HAVE_PTHREAD_CANCEL +#define PTHREAD_CANCELED ((void *) -1) + struct sb_thread_proxy { void *(*start_routine) (void *); void *arg; @@ -95,9 +100,6 @@ static int thread_cancel_signal = SIGUSR1; static void thread_cancel_handler(int sig) { -#ifndef PTHREAD_CANCELED -# define PTHREAD_CANCELED ((void *) -1) -#endif if (sig == thread_cancel_signal) pthread_exit(PTHREAD_CANCELED); } @@ -119,10 +121,14 @@ static void* thread_start_routine_proxy(void *arg) { install_thread_signal_handler(); return start_routine(real_arg); } +#endif int sb_thread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg) { +#ifdef HAVE_PTHREAD_CANCEL + return pthread_create(thread, attr, start_routine, arg); +#else struct sb_thread_proxy *proxy = malloc(sizeof(struct sb_thread_proxy)); if (!proxy) { @@ -136,6 +142,7 @@ int sb_thread_create(pthread_t *thread, const pthread_attr_t *attr, free(proxy); } return rv; +#endif } int sb_thread_join(pthread_t thread, void **retval) @@ -145,7 +152,11 @@ int sb_thread_join(pthread_t thread, void **retval) int sb_thread_cancel(pthread_t thread) { +#ifdef HAVE_PTHREAD_CANCEL + return pthread_cancel(thread); +#else return pthread_kill(thread, thread_cancel_signal); +#endif } int sb_thread_create_workers(void *(*worker_routine)(void*))