1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-09 03:22:45 +03:00

Made httplib_pthread_cond_broadcast global

This commit is contained in:
Lammert Bies
2016-12-20 08:49:48 +01:00
parent 8b3e8aa100
commit 0ee926db97
7 changed files with 61 additions and 32 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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 );

View File

@@ -132,24 +132,22 @@ static void master_thread_run(void *thread_func_param) {
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]);
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]);
}
if ( ctx->workerthreadids[i] != 0 ) XX_httplib_join_thread(ctx->workerthreadids[i]);
}
#if !defined(NO_SSL)

View File

@@ -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 */

View File

@@ -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 */
return pthread_cond_broadcast( cv );
#endif /* _WIN32 */
} /* httplib_pthread_cond_broadcast */

View File

@@ -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;