diff --git a/Makefile b/Makefile index 89f65a84..59bac782 100644 --- a/Makefile +++ b/Makefile @@ -374,7 +374,7 @@ OBJLIST = \ ${OBJDIR}win32_clock_gettime${OBJEXT} \ ${OBJDIR}win32_pthread_cond_broadcast${OBJEXT} \ ${OBJDIR}win32_pthread_cond_destroy${OBJEXT} \ - ${OBJDIR}win32_pthread_cond_init${OBJEXT} \ + ${OBJDIR}httplib_pthread_cond_init${OBJEXT} \ ${OBJDIR}httplib_pthread_cond_signal${OBJEXT} \ ${OBJDIR}httplib_pthread_cond_timedwait${OBJEXT} \ ${OBJDIR}httplib_pthread_cond_wait${OBJEXT} \ @@ -1401,8 +1401,7 @@ ${OBJDIR}win32_pthread_cond_destroy${OBJEXT} : ${SRCDIR}win32_pthread_cond_de ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h -${OBJDIR}win32_pthread_cond_init${OBJEXT} : ${SRCDIR}win32_pthread_cond_init.c \ - ${SRCDIR}httplib_pthread.h \ +${OBJDIR}httplib_pthread_cond_init${OBJEXT} : ${SRCDIR}httplib_pthread_cond_init.c \ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h diff --git a/doc/api/httplib_pthread_cond_init.md b/doc/api/httplib_pthread_cond_init.md new file mode 100644 index 00000000..17f34fb4 --- /dev/null +++ b/doc/api/httplib_pthread_cond_init.md @@ -0,0 +1,29 @@ +# LibHTTP API Reference + +### `httplib_pthread_cond_init( cv, attr );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`cv`**|`pthread_cond_t *`|Storage for the condition variable when the function is successful| +|**`attr`**|`const pthread_condattr_t *`|Optional attributes for creating the condition variable| + + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Integer value with a success or error code of the function call| + +### Description + +The platform independent function `htptlib_pthread_cond_init()` allocates a condition variable which can be used for letting threads wait for a specific condition. The variable is returned in a location pointed to by a parameter. If the function is successful the value **0** is returned. Otherwise the function returns an error code. On systems which support it, the functionality is implemented as a direct call to `pthrea_cond_init()`. Otherwise own code is used which emulates the same behaviour. Please note that the `attr` parameter is ignored when the function is used on Windows. + +### See Also + +* [`httplib_pthread_cond_broadcast();`](httplib_pthread_cond_broadcast.md) +* [`httplib_pthread_cond_destroy();`](httplib_pthread_cond_destroy.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 a7dc55fd..fbf6b181 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_init( pthread_cond_t *cv, const pthread_condattr_t *attr ); LIBHTTP_API int httplib_pthread_cond_signal( pthread_cond_t *cv ); 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 ); diff --git a/src/httplib_event_queue.c b/src/httplib_event_queue.c index 24c51ebd..1369460b 100644 --- a/src/httplib_event_queue.c +++ b/src/httplib_event_queue.c @@ -107,23 +107,27 @@ void *event_create(void) { XX_httplib_free(ret); return NULL; } - if (0 != pthread_cond_init(&(ret->cond), NULL)) { + if (0 != httplib_pthread_cond_init( & ret->cond, NULL)) { /* pthread cond not available */ httplib_pthread_mutex_destroy( & ret->mutex ); XX_httplib_free(ret); return NULL; } - return (void *)ret; + return ret; } /* event_create */ -int event_wait(void *eventhdl) { +int event_wait( void *eventhdl ) { - struct posix_event *ev = (struct posix_event *)eventhdl; - httplib_pthread_mutex_lock( & ev->mutex ); + struct posix_event *ev; + + ev = eventhdl; + + httplib_pthread_mutex_lock( & ev->mutex ); httplib_pthread_cond_wait( & ev->cond, & ev->mutex ); - httplib_pthread_mutex_unlock( & ev->mutex ); + httplib_pthread_mutex_unlock( & ev->mutex ); + return 1; } /* event_wait */ diff --git a/src/httplib_pthread.h b/src/httplib_pthread.h index 4c346df6..1c52f164 100644 --- a/src/httplib_pthread.h +++ b/src/httplib_pthread.h @@ -32,6 +32,5 @@ 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 pthread_condattr_t *attr ); #endif /* _WIN32 */ diff --git a/src/win32_pthread_cond_init.c b/src/httplib_pthread_cond_init.c similarity index 62% rename from src/win32_pthread_cond_init.c rename to src/httplib_pthread_cond_init.c index ab0984d2..1ac928db 100644 --- a/src/win32_pthread_cond_init.c +++ b/src/httplib_pthread_cond_init.c @@ -22,16 +22,30 @@ * THE SOFTWARE. * * ============ - * Release: 1.8 + * Release: 2.0 */ #include "httplib_main.h" -#include "httplib_pthread.h" + +/* + * int httplib_pthread_cond_init( pthread_cond_t *cv, const pthread_condattr_t *attr ); + * + * The platform independent function httplib_pthread_cond_init() can be used to + * create a new condition variable with the attributes as specificied by a + * parameter. The function returns 0 when successful and an error code if a + * problem occurs. + * + * On systems which support it, the function is a wrapper arounnd the function + * pthread_cond_init(), otherwise equivalent functionality is implemented in + * own code. + * + * Please note that the attr parameter is ignored in the Windows implementation + */ + +int httplib_pthread_cond_init( pthread_cond_t *cv, const pthread_condattr_t *attr ) { #if defined(_WIN32) -int pthread_cond_init( pthread_cond_t *cv, const void *attr ) { - UNUSED_PARAMETER(attr); InitializeCriticalSection( & cv->threadIdSec ); @@ -39,6 +53,10 @@ int pthread_cond_init( pthread_cond_t *cv, const void *attr ) { return 0; -} /* pthread_cond_init */ +#else /* _WIN32 */ -#endif /* _WIN32 */ + return pthread_cond_init( cv, attr ); + +#endif /* _WIN32 */ + +} /* httplib_pthread_cond_init */ diff --git a/src/httplib_start.c b/src/httplib_start.c index 1364ad3e..71ec8441 100644 --- a/src/httplib_start.c +++ b/src/httplib_start.c @@ -99,8 +99,8 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks ok = 0 == pthread_mutex_init( & ctx->thread_mutex, &XX_httplib_pthread_mutex_attr ); #if !defined(ALTERNATIVE_QUEUE) - ok &= 0 == pthread_cond_init( & ctx->sq_empty, NULL ); - ok &= 0 == pthread_cond_init( & ctx->sq_full, NULL ); + ok &= 0 == httplib_pthread_cond_init( & ctx->sq_empty, NULL ); + ok &= 0 == httplib_pthread_cond_init( & ctx->sq_full, NULL ); #endif ok &= 0 == pthread_mutex_init( & ctx->nonce_mutex, & XX_httplib_pthread_mutex_attr ); if ( ! ok ) {