diff --git a/Makefile b/Makefile index 54c4af55..043b596d 100644 --- a/Makefile +++ b/Makefile @@ -382,7 +382,7 @@ OBJLIST = \ ${OBJDIR}win32_pthread_key_create${OBJEXT} \ ${OBJDIR}win32_pthread_key_delete${OBJEXT} \ ${OBJDIR}win32_pthread_mutex_destroy${OBJEXT} \ - ${OBJDIR}win32_pthread_mutex_init${OBJEXT} \ + ${OBJDIR}httplib_pthread_mutex_init${OBJEXT} \ ${OBJDIR}httplib_pthread_mutex_lock${OBJEXT} \ ${OBJDIR}httplib_pthread_mutex_trylock${OBJEXT} \ ${OBJDIR}httplib_pthread_mutex_unlock${OBJEXT} \ @@ -1441,8 +1441,7 @@ ${OBJDIR}win32_pthread_mutex_destroy${OBJEXT} : ${SRCDIR}win32_pthread_mutex_ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h -${OBJDIR}win32_pthread_mutex_init${OBJEXT} : ${SRCDIR}win32_pthread_mutex_init.c \ - ${SRCDIR}httplib_pthread.h \ +${OBJDIR}httplib_pthread_mutex_init${OBJEXT} : ${SRCDIR}httplib_pthread_mutex_init.c \ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h diff --git a/doc/api/httplib_pthread_mutex_init.md b/doc/api/httplib_pthread_mutex_init.md new file mode 100644 index 00000000..55b94f91 --- /dev/null +++ b/doc/api/httplib_pthread_mutex_init.md @@ -0,0 +1,29 @@ +# LibHTTP API Reference + +### `httplib_pthread_mutex_init( mutex, attr );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`mutex`**|`pthread_mutex_t`|The key to the mutex to initialize| +|**`attr`**|`const pthread_mutexattr_t`|Optional attributes for the initialization| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Integer value with the result of the function| + +### Description + +The platform independent function `httplib_pthread_mutex_init()` is used to initialize a mutex. The function returns **0** if this is successful, or an error code if it fails. On systems which support it, this function is implemented as a direct call to `pthread_mutex_init()`. On other systems own code is used to emulate the same functionality. + +Please not that on systems which do not support `pthread_mutex_init()` natively that the `attr` parameter is ignored. + +### See Also + +* [`httplib_pthread_mutex_destroy();`](httplib_pthread_mutex_destroy.md) +* [`httplib_pthread_mutex_lock();`](httplib_pthread_mutex_lock.md) +* [`httplib_pthread_mutex_trylock();`](httplib_pthread_mutex_trylock.md) +* [`httplib_pthread_mutex_unlock();`](httplib_pthread_mutex_unlock.md) diff --git a/include/libhttp.h b/include/libhttp.h index 220de4ad..ab3bd37a 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_mutex_init( pthread_mutex_t *mutex, const pthread_mutexattr_t *attr ); LIBHTTP_API int httplib_pthread_mutex_lock( pthread_mutex_t *mutex ); LIBHTTP_API int httplib_pthread_mutex_trylock( pthread_mutex_t *mutex ); LIBHTTP_API int httplib_pthread_mutex_unlock( pthread_mutex_t *mutex ); diff --git a/src/httplib_event_queue.c b/src/httplib_event_queue.c index f91e0c3f..a5b0524a 100644 --- a/src/httplib_event_queue.c +++ b/src/httplib_event_queue.c @@ -102,7 +102,7 @@ void *event_create(void) { struct posix_event *ret = XX_httplib_malloc(sizeof(struct posix_event)); if ( ret == NULL ) return NULL; - if (0 != pthread_mutex_init(&(ret->mutex), NULL)) { + if (0 != httplib_pthread_mutex_init( & ret->mutex, NULL ) ) { /* pthread mutex not available */ XX_httplib_free(ret); return NULL; diff --git a/src/httplib_pthread.h b/src/httplib_pthread.h index 902c6e99..4122c3b9 100644 --- a/src/httplib_pthread.h +++ b/src/httplib_pthread.h @@ -41,7 +41,6 @@ int pthread_key_create( pthread_key_t *key, void (*_ignored)(void *) ); int pthread_key_delete( pthread_key_t key ); int pthread_mutex_destroy( pthread_mutex_t *mutex ); -int pthread_mutex_init( pthread_mutex_t *mutex, void *unused ); void * pthread_getspecific( pthread_key_t key ); diff --git a/src/win32_pthread_mutex_init.c b/src/httplib_pthread_mutex_init.c similarity index 66% rename from src/win32_pthread_mutex_init.c rename to src/httplib_pthread_mutex_init.c index f2ae9425..64ba02d3 100644 --- a/src/win32_pthread_mutex_init.c +++ b/src/httplib_pthread_mutex_init.c @@ -22,21 +22,34 @@ * THE SOFTWARE. * * ============ - * Release: 1.8 + * Release: 2.0 */ #include "httplib_main.h" -#include "httplib_pthread.h" + +/* + * int httplib_pthread_mutex_init( pthread_mutex_t *mutex, const pthread_mutexattr_t *attr ); + * + * The platform independent function httplib_pthread_mutex_init() is used to + * initialize a mutex for future locking. If the function succeeds the value 0 + * is returned, otherwise an error code. On systems which support it this + * function is a wrapper around pthread_mutex_init(). On ither systems own code + * is used to emulate equivalent behaviour. + */ + +int httplib_pthread_mutex_init( pthread_mutex_t *mutex, const pthread_mutexattr_t *attr ) { #if defined(_WIN32) -int pthread_mutex_init( pthread_mutex_t *mutex, void *unused ) { - - (void)unused; + UNUSED_PARAMETER(attr); *mutex = CreateMutex( NULL, FALSE, NULL ); return (*mutex == NULL) ? -1 : 0; -} /* pthread_mutex_init */ +#else /* _WIN32 */ -#endif /* _WIN32 */ + return pthread_mutex_init( mutex, attr ); + +#endif + +} /* httplib_pthread_mutex_init */ diff --git a/src/httplib_timer.c b/src/httplib_timer.c index 95211403..216613e9 100644 --- a/src/httplib_timer.c +++ b/src/httplib_timer.c @@ -176,7 +176,7 @@ static void * timer_thread( void *thread_func_param ) { static int timers_init( struct httplib_context *ctx ) { ctx->timers = (struct ttimers *)httplib_calloc(sizeof(struct ttimers), 1); - pthread_mutex_init(&ctx->timers->mutex, NULL); + httplib_pthread_mutex_init( & ctx->timers->mutex, NULL ); /* Start timer thread */ httplib_start_thread_with_id(timer_thread, ctx, &ctx->timers->threadid);