From 0ee926db97bc203bcb2f811919cfd895119c0582 Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Tue, 20 Dec 2016 08:49:48 +0100 Subject: [PATCH] Made httplib_pthread_cond_broadcast global --- Makefile | 5 ++-- doc/api/httplib_pthread_cond_broadcast.md | 27 +++++++++++++++++ include/libhttp.h | 1 + src/httplib_master_thread.c | 18 +++++------- src/httplib_pthread.h | 8 ----- ...ast.c => httplib_pthread_cond_broadcast.c} | 29 ++++++++++++++----- src/httplib_pthread_cond_destroy.c | 5 ++-- 7 files changed, 61 insertions(+), 32 deletions(-) create mode 100644 doc/api/httplib_pthread_cond_broadcast.md rename src/{win32_pthread_cond_broadcast.c => httplib_pthread_cond_broadcast.c} (68%) diff --git a/Makefile b/Makefile index adeadcaa..2f746664 100644 --- a/Makefile +++ b/Makefile @@ -372,7 +372,7 @@ OBJLIST = \ ${OBJDIR}httplib_write${OBJEXT} \ ${OBJDIR}osx_clock_gettime${OBJEXT} \ ${OBJDIR}win32_clock_gettime${OBJEXT} \ - ${OBJDIR}win32_pthread_cond_broadcast${OBJEXT} \ + ${OBJDIR}httplib_pthread_cond_broadcast${OBJEXT} \ ${OBJDIR}httplib_pthread_cond_destroy${OBJEXT} \ ${OBJDIR}httplib_pthread_cond_init${OBJEXT} \ ${OBJDIR}httplib_pthread_cond_signal${OBJEXT} \ @@ -1391,8 +1391,7 @@ ${OBJDIR}win32_clock_gettime${OBJEXT} : ${SRCDIR}win32_clock_gettime.c \ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h -${OBJDIR}win32_pthread_cond_broadcast${OBJEXT} : ${SRCDIR}win32_pthread_cond_broadcast.c \ - ${SRCDIR}httplib_pthread.h \ +${OBJDIR}httplib_pthread_cond_broadcast${OBJEXT} : ${SRCDIR}httplib_pthread_cond_broadcast.c \ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h diff --git a/doc/api/httplib_pthread_cond_broadcast.md b/doc/api/httplib_pthread_cond_broadcast.md new file mode 100644 index 00000000..940c848d --- /dev/null +++ b/doc/api/httplib_pthread_cond_broadcast.md @@ -0,0 +1,27 @@ +# LibHTTP API Reference + +### `httplib_pthread_cond_broadcast( cv );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`cv`**|`pthread_cond_t *`|The condition which a thread is waiting on| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Integer value with a success or error code of the function call| + +### Description + +The platform independent function `httplib_pthread_cond_broadcast()` unlocks all threads waiting on a specific condition. The function returns **0** when successful and an error code otherwise. On systems which support it, the functionality is implemented as a direct call to `pthread_cond_broadcast()`. Otherwise an OS dependent alternative implementation is used to emulate the same behavior. + +### See Also + +* [`httplib_pthread_cond_destroy();`](httplib_pthread_cond_destroy.md) +* [`httplib_pthread_cond_init();`](httplib_pthread_cond_init.md) +* [`httplib_pthread_cond_signal();`](httplib_pthread_cond_signal.md) +* [`httplib_pthread_cond_timedwait();`](httplib_pthread_cond_timedwait.md) +* [`httplib_pthread_cond_wait();`](httplib_pthread_cond_wait.md) diff --git a/include/libhttp.h b/include/libhttp.h index bfe37858..423943fd 100644 --- a/include/libhttp.h +++ b/include/libhttp.h @@ -985,6 +985,7 @@ LIBHTTP_API int httplib_kill( pid_t pid, int sig_num ); LIBHTTP_API int httplib_mkdir( const char *path, int mode ); LIBHTTP_API DIR * httplib_opendir( const char *name ); LIBHTTP_API int httplib_poll( struct pollfd *pfd, unsigned int nfds, int timeout ); +LIBHTTP_API int httplib_pthread_cond_broadcast( pthread_cond_t *cv ); LIBHTTP_API int httplib_pthread_cond_destroy( pthread_cond_t *cv ); LIBHTTP_API int httplib_pthread_cond_init( pthread_cond_t *cv, const pthread_condattr_t *attr ); LIBHTTP_API int httplib_pthread_cond_signal( pthread_cond_t *cv ); diff --git a/src/httplib_master_thread.c b/src/httplib_master_thread.c index c8be75c1..66d5daff 100644 --- a/src/httplib_master_thread.c +++ b/src/httplib_master_thread.c @@ -131,25 +131,23 @@ static void master_thread_run(void *thread_func_param) { /* Wakeup workers that are waiting for connections to handle. */ httplib_pthread_mutex_lock( & ctx->thread_mutex ); #if defined(ALTERNATIVE_QUEUE) - for (i = 0; i < ctx->cfg_worker_threads; i++) { - event_signal(ctx->client_wait_events[i]); + for (i=0; icfg_worker_threads; i++) { + + event_signal( ctx->client_wait_events[i]i ); /* Since we know all sockets, we can shutdown the connections. */ - if (ctx->client_socks[i].in_use) { - shutdown(ctx->client_socks[i].sock, SHUTDOWN_BOTH); - } + if (ctx->client_socks[i].in_use) shutdown( ctx->client_socks[i].sock, SHUTDOWN_BOTH ); } #else - pthread_cond_broadcast(&ctx->sq_full); + httplib_pthread_cond_broadcast( & ctx->sq_full ); #endif httplib_pthread_mutex_unlock( & ctx->thread_mutex ); /* Join all worker threads to avoid leaking threads. */ workerthreadcount = ctx->cfg_worker_threads; - for (i = 0; i < workerthreadcount; i++) { - if (ctx->workerthreadids[i] != 0) { - XX_httplib_join_thread(ctx->workerthreadids[i]); - } + for (i=0; iworkerthreadids[i] != 0 ) XX_httplib_join_thread(ctx->workerthreadids[i]); } #if !defined(NO_SSL) diff --git a/src/httplib_pthread.h b/src/httplib_pthread.h index 6216373c..8c345626 100644 --- a/src/httplib_pthread.h +++ b/src/httplib_pthread.h @@ -25,11 +25,3 @@ extern pthread_mutex_t * XX_httplib_ssl_mutexes; extern pthread_key_t XX_httplib_sTlsKey; extern int XX_httplib_thread_idx_max; - - - -#if defined(_WIN32) - -int pthread_cond_broadcast( pthread_cond_t *cv ); - -#endif /* _WIN32 */ diff --git a/src/win32_pthread_cond_broadcast.c b/src/httplib_pthread_cond_broadcast.c similarity index 68% rename from src/win32_pthread_cond_broadcast.c rename to src/httplib_pthread_cond_broadcast.c index 1ee0eaa8..be4ba8cb 100644 --- a/src/win32_pthread_cond_broadcast.c +++ b/src/httplib_pthread_cond_broadcast.c @@ -22,24 +22,37 @@ * THE SOFTWARE. * * ============ - * Release: 1.8 + * Release: 2.0 */ #include "httplib_main.h" -#include "httplib_pthread.h" + +/* + * int httplib_pthread_cond_broadcast( pthread_cond_t *cv ); + * + * The platform independent function httplib_pthread_cond_broadcast() unblocks + * all threads waiting for a specific condition. If the function is successful + * the value is returned, otherwise an error code. + * + * On systems which support it, the function is a wrapper around the function + * pthread_cond_broadcast(). On other systems own code is used to emulate the + * same behaviour. + */ + +int httplib_pthread_cond_broadcast( pthread_cond_t *cv ) { #if defined(_WIN32) -int pthread_cond_broadcast( pthread_cond_t *cv ) { - EnterCriticalSection( & cv->threadIdSec ); - while ( cv->waiting_thread ) httplib_pthread_cond_signal( cv ); - LeaveCriticalSection( & cv->threadIdSec ); return 0; -} /* pthread_cond_broadcast */ +#else /* _WIN32 */ -#endif /* _WIN32 */ + return pthread_cond_broadcast( cv ); + +#endif /* _WIN32 */ + +} /* httplib_pthread_cond_broadcast */ diff --git a/src/httplib_pthread_cond_destroy.c b/src/httplib_pthread_cond_destroy.c index 8852ce8e..f0cc3e19 100644 --- a/src/httplib_pthread_cond_destroy.c +++ b/src/httplib_pthread_cond_destroy.c @@ -44,11 +44,10 @@ int httplib_pthread_cond_destroy( pthread_cond_t *cv ) { int retval; EnterCriticalSection( & cv->threadIdSec ); - retval = ( cv->waiting_thread == NULL ) ? 0 : -1; - LeaveCriticalSection( & cv->threadIdSec ); - DeleteCriticalSection( & cv->threadIdSec ); + + if ( ! retval ) DeleteCriticalSection( & cv->threadIdSec ); return retval;