diff --git a/Makefile b/Makefile index 0fa0a309..f1a742f8 100644 --- a/Makefile +++ b/Makefile @@ -377,7 +377,7 @@ OBJLIST = \ ${OBJDIR}win32_pthread_cond_init${OBJEXT} \ ${OBJDIR}win32_pthread_cond_signal${OBJEXT} \ ${OBJDIR}win32_pthread_cond_timedwait${OBJEXT} \ - ${OBJDIR}win32_pthread_cond_wait${OBJEXT} \ + ${OBJDIR}httplib_pthread_cond_wait${OBJEXT} \ ${OBJDIR}httplib_pthread_getspecific${OBJEXT} \ ${OBJDIR}httplib_pthread_key_create${OBJEXT} \ ${OBJDIR}httplib_pthread_key_delete${OBJEXT} \ @@ -1416,8 +1416,7 @@ ${OBJDIR}win32_pthread_cond_timedwait${OBJEXT} : ${SRCDIR}win32_pthread_cond_ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h -${OBJDIR}win32_pthread_cond_wait${OBJEXT} : ${SRCDIR}win32_pthread_cond_wait.c \ - ${SRCDIR}httplib_pthread.h \ +${OBJDIR}httplib_pthread_cond_wait${OBJEXT} : ${SRCDIR}httplib_pthread_cond_wait.c \ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h diff --git a/doc/APIReference.md b/doc/APIReference.md index a03b3c20..f0ff84c4 100644 --- a/doc/APIReference.md +++ b/doc/APIReference.md @@ -100,6 +100,12 @@ LibHTTP is often used as HTTP and HTTPS library inside a larger application. A * [`httplib_lock_connection( conn );`](api/httplib_lock_connection.md) * [`httplib_lock_context( ctx );`](api/httplib_lock_context.md) * [`httplib_poll( pfd, nfds, timeout );`](api/httplib_poll.md) +* [`httplib_pthread_cond_broadcast( cv );`](api/httplib_pthread_cond_broadcast.md) +* [`httplib_pthread_cond_destroy( cv );`](api/httplib_pthread_cond_destroy.md) +* [`httplib_pthread_cond_init( cv, attr );`](api/httplib_pthread_cond_init.md) +* [`httplib_pthread_cond_signal( cv );`](api/httplib_pthread_cond_signal.md) +* [`httplib_pthread_cond_timedwait( cv, mutex, abstime );`](api/httplib_pthread_cond_timedwait.md) +* [`httplib_pthread_cond_wait( cv, mutex );`](api/httplib_pthread_cond_wait.md) * [`httplib_pthread_getspecific( key );`](api/httplib_getspecific.md) * [`httplib_pthread_key_create( key, destructor );`](api/httplib_key_create.md) * [`httplib_pthread_key_delete( key );`](api/httplib_pthread_key_delete.md) diff --git a/doc/api/httplib_pthread_cond_wait.md b/doc/api/httplib_pthread_cond_wait.md new file mode 100644 index 00000000..41af0745 --- /dev/null +++ b/doc/api/httplib_pthread_cond_wait.md @@ -0,0 +1,28 @@ +# LibHTTP API Reference + +### `httplib_pthread_cond_wait( cv, mutex );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`cv`**|`pthread_cond_t`|The condition to wait for| +|**`mutex`**|`pthread_mutex_t *`|The mutex to release when the condition is met| + +### 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_wait()` is used to wait for a specific condition to be met. After the condition is met, the specified mutex is unlocked. If the function succeeds, the value **0** is returned, otherwise the return value is an error code. On systems which support it, the functionality is implemented as a direct call to `pthread_cond_wait()`. Otherwise an OS dependent alternative function is called. + +### See Also + +* [`httplib_pthread_cond_broadcast();`](httplib_pthread_cond_broadcast.md) +* [`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) diff --git a/include/libhttp.h b/include/libhttp.h index 8eba7fec..d62cdbf2 100644 --- a/include/libhttp.h +++ b/include/libhttp.h @@ -985,6 +985,8 @@ 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_timedwait( pthread_cond_t *cv, pthread_mutex_t *mutex, const struct timespec *abstime ); +LIBHTTP_API int httplib_pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mutex ); LIBHTTP_API void * httplib_pthread_getspecific( pthread_key_t key ); LIBHTTP_API int httplib_pthread_key_create( pthread_key_t *key, void (*destructor)(void *) ); LIBHTTP_API int httplib_pthread_key_delete( pthread_key_t key ); diff --git a/src/httplib_consume_socket.c b/src/httplib_consume_socket.c index 90475e49..ac938714 100644 --- a/src/httplib_consume_socket.c +++ b/src/httplib_consume_socket.c @@ -60,7 +60,8 @@ int XX_httplib_consume_socket( struct httplib_context *ctx, struct socket *sp, i /* If the queue is empty, wait. We're idle at this point. */ while (ctx->sq_head == ctx->sq_tail && ctx->stop_flag == 0) { - pthread_cond_wait(&ctx->sq_full, &ctx->thread_mutex); + + httplib_pthread_cond_wait( & ctx->sq_full, & ctx->thread_mutex ); } /* If we're stopping, sq_head may be equal to sq_tail. */ diff --git a/src/httplib_event_queue.c b/src/httplib_event_queue.c index e25b32c8..af8a1e36 100644 --- a/src/httplib_event_queue.c +++ b/src/httplib_event_queue.c @@ -122,7 +122,7 @@ int event_wait(void *eventhdl) { struct posix_event *ev = (struct posix_event *)eventhdl; httplib_pthread_mutex_lock( & ev->mutex ); - pthread_cond_wait(&(ev->cond), &(ev->mutex)); + httplib_pthread_cond_wait( & ev->cond, & ev->mutex ); httplib_pthread_mutex_unlock( & ev->mutex ); return 1; diff --git a/src/httplib_produce_socket.c b/src/httplib_produce_socket.c index 21b3e893..a0feaf52 100644 --- a/src/httplib_produce_socket.c +++ b/src/httplib_produce_socket.c @@ -69,7 +69,8 @@ void XX_httplib_produce_socket(struct httplib_context *ctx, const struct socket /* If the queue is full, wait */ while (ctx->stop_flag == 0 && ctx->sq_head - ctx->sq_tail >= QUEUE_SIZE(ctx)) { - pthread_cond_wait(&ctx->sq_empty, &ctx->thread_mutex); + + httplib_pthread_cond_wait( & ctx->sq_empty, & ctx->thread_mutex ); } if (ctx->sq_head - ctx->sq_tail < QUEUE_SIZE(ctx)) { diff --git a/src/httplib_pthread.h b/src/httplib_pthread.h index 9be57e55..7662a786 100644 --- a/src/httplib_pthread.h +++ b/src/httplib_pthread.h @@ -32,9 +32,8 @@ extern int XX_httplib_thread_idx_max; int pthread_cond_broadcast( pthread_cond_t *cv ); int pthread_cond_destroy( pthread_cond_t *cv ); -int pthread_cond_init( pthread_cond_t *cv, const void *unused ); +int pthread_cond_init( pthread_cond_t *cv, const pthread_condattr_t *attr ); int pthread_cond_signal( pthread_cond_t *cv ); int pthread_cond_timedwait( pthread_cond_t *cv, pthread_mutex_t *mutex, const struct timespec *abstime ); -int pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mutex ); #endif /* _WIN32 */ diff --git a/src/win32_pthread_cond_wait.c b/src/httplib_pthread_cond_wait.c similarity index 69% rename from src/win32_pthread_cond_wait.c rename to src/httplib_pthread_cond_wait.c index f4858fa3..4ff5c463 100644 --- a/src/win32_pthread_cond_wait.c +++ b/src/httplib_pthread_cond_wait.c @@ -22,18 +22,29 @@ * THE SOFTWARE. * * ============ - * Release: 1.8 + * Release: 2.0 */ #include "httplib_main.h" -#include "httplib_pthread.h" + +/* + * int httplib_pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mutex ); + * + * The function httplib_pthread_cond_wait() performs a wait until a condition + * is met and then releases the specified mutex. The function returns 0 on + * success and a non zero error code at failure. + */ + +int httplib_pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mutex ) { #if defined(_WIN32) -int pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mutex ) { + return httplib_pthread_cond_timedwait( cv, mutex, NULL ); - return pthread_cond_timedwait(cv, mutex, NULL); +#else /* _WIN32 */ -} /* pthread_cond_wait */ + return pthread_cond_wait( cv, mutex ); -#endif /* _WIN32 */ +#endif /* _WIN32 */ + +} /* httplib_pthread_cond_wait */