diff --git a/Makefile b/Makefile index 043b596d..215398a5 100644 --- a/Makefile +++ b/Makefile @@ -381,7 +381,7 @@ OBJLIST = \ ${OBJDIR}win32_pthread_getspecific${OBJEXT} \ ${OBJDIR}win32_pthread_key_create${OBJEXT} \ ${OBJDIR}win32_pthread_key_delete${OBJEXT} \ - ${OBJDIR}win32_pthread_mutex_destroy${OBJEXT} \ + ${OBJDIR}httplib_pthread_mutex_destroy${OBJEXT} \ ${OBJDIR}httplib_pthread_mutex_init${OBJEXT} \ ${OBJDIR}httplib_pthread_mutex_lock${OBJEXT} \ ${OBJDIR}httplib_pthread_mutex_trylock${OBJEXT} \ @@ -1436,8 +1436,7 @@ ${OBJDIR}win32_pthread_key_delete${OBJEXT} : ${SRCDIR}win32_pthread_key_delet ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h -${OBJDIR}win32_pthread_mutex_destroy${OBJEXT} : ${SRCDIR}win32_pthread_mutex_destroy.c \ - ${SRCDIR}httplib_pthread.h \ +${OBJDIR}httplib_pthread_mutex_destroy${OBJEXT} : ${SRCDIR}httplib_pthread_mutex_destroy.c \ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h diff --git a/doc/api/httplib_pthread_mutex_destroy.md b/doc/api/httplib_pthread_mutex_destroy.md new file mode 100644 index 00000000..faa953b5 --- /dev/null +++ b/doc/api/httplib_pthread_mutex_destroy.md @@ -0,0 +1,26 @@ +# LibHTTP API Reference + +### `httplib_pthread_mutex_destroy( mutex );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`mutex`**|`pthread_mutex_t`|The key to the mutex to destroy| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Integer value with the result of the function| + +### Description + +The platform independent function `httplib_pthread_mutex_destroy()` destroys a mutex and frees the allocated resources. The function returns **0** if this is successful, or an error code if it fails. If a mutex is locked by another thread the function will return with an error code. On systems which support it, this function is implemented as a wrapper around `pthread_mutex_destroy()`. On other systems own code is used to emulate the same functionality. + +### See Also + +* [`httplib_pthread_mutex_init();`](httplib_pthread_mutex_init.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 ab3bd37a..03fda20b 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_destroy( pthread_mutex_t *mutex ); 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 ); diff --git a/src/httplib_close_connection.c b/src/httplib_close_connection.c index 909e7376..63a337d6 100644 --- a/src/httplib_close_connection.c +++ b/src/httplib_close_connection.c @@ -108,7 +108,7 @@ void httplib_close_connection( struct httplib_connection *conn ) { httplib_free( client_ctx->workerthreadids ); httplib_free( client_ctx ); - pthread_mutex_destroy( & conn->mutex ); + httplib_pthread_mutex_destroy( & conn->mutex ); httplib_free( conn ); } diff --git a/src/httplib_event_queue.c b/src/httplib_event_queue.c index a5b0524a..e25b32c8 100644 --- a/src/httplib_event_queue.c +++ b/src/httplib_event_queue.c @@ -109,7 +109,7 @@ void *event_create(void) { } if (0 != pthread_cond_init(&(ret->cond), NULL)) { /* pthread cond not available */ - pthread_mutex_destroy(&(ret->mutex)); + httplib_pthread_mutex_destroy( & ret->mutex ); XX_httplib_free(ret); return NULL; } @@ -144,7 +144,7 @@ void event_destroy(void *eventhdl) { struct posix_event *ev = (struct posix_event *)eventhdl; pthread_cond_destroy(&(ev->cond)); - pthread_mutex_destroy(&(ev->mutex)); + httplib_pthread_mutex_destroy( & ev->mutex ); XX_httplib_free(ev); } /* event_destroy */ diff --git a/src/httplib_free_context.c b/src/httplib_free_context.c index d29f8d8a..291cb373 100644 --- a/src/httplib_free_context.c +++ b/src/httplib_free_context.c @@ -50,7 +50,7 @@ void XX_httplib_free_context( struct httplib_context *ctx ) { /* All threads exited, no sync is needed. Destroy thread mutex and * condvars */ - pthread_mutex_destroy(&ctx->thread_mutex); + httplib_pthread_mutex_destroy( & ctx->thread_mutex ); #if defined(ALTERNATIVE_QUEUE) XX_httplib_free(ctx->client_socks); for (i = 0; (unsigned)i < ctx->cfg_worker_threads; i++) { @@ -63,10 +63,10 @@ void XX_httplib_free_context( struct httplib_context *ctx ) { #endif /* Destroy other context global data structures mutex */ - pthread_mutex_destroy(&ctx->nonce_mutex); + httplib_pthread_mutex_destroy( & ctx->nonce_mutex ); #if defined(USE_TIMERS) - timers_exit(ctx); + timers_exit( ctx ); #endif /* Deallocate config parameters */ diff --git a/src/httplib_pthread.h b/src/httplib_pthread.h index 4122c3b9..c1b9cdfd 100644 --- a/src/httplib_pthread.h +++ b/src/httplib_pthread.h @@ -40,8 +40,6 @@ int pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mutex ); 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 ); - void * pthread_getspecific( pthread_key_t key ); #endif /* _WIN32 */ diff --git a/src/win32_pthread_mutex_destroy.c b/src/httplib_pthread_mutex_destroy.c similarity index 66% rename from src/win32_pthread_mutex_destroy.c rename to src/httplib_pthread_mutex_destroy.c index f22214fb..1e769de1 100644 --- a/src/win32_pthread_mutex_destroy.c +++ b/src/httplib_pthread_mutex_destroy.c @@ -22,18 +22,31 @@ * THE SOFTWARE. * * ============ - * Release: 1.8 + * Release: 2.0 */ #include "httplib_main.h" -#include "httplib_pthread.h" + +/* + * int httplib_pthread_mutex_destroy( pthread_mutex_t *mutex ); + * + * The function httplib_pthread_mutex_destroy() provides a platform independent + * way to destroy a mutex. The function returns 0 when successful, and a non + * zero value if an error occures. On systems which support it the function is + * implemented as a wrapper around pthread_mutex_destroy(). On other systems + * own code is used with equivalent functionality. + */ + +int httplib_pthread_mutex_destroy( pthread_mutex_t *mutex ) { #if defined(_WIN32) -int pthread_mutex_destroy( pthread_mutex_t *mutex ) { + return ( CloseHandle( *mutex ) == 0 ) ? -1 : 0; - return ( CloseHandle(*mutex) == 0 ) ? -1 : 0; +#else /* _WIN32 */ -} /* pthread_mutex_destroy */ + return pthread_mutex_destroy( mutex ); #endif /* _WIN32 */ + +} /* httplib_pthread_mutex_destroy */ diff --git a/src/httplib_timer.c b/src/httplib_timer.c index 216613e9..29d09e4b 100644 --- a/src/httplib_timer.c +++ b/src/httplib_timer.c @@ -189,7 +189,7 @@ static void timers_exit( struct httplib_context *ctx ) { if ( ctx->timers != NULL ) { - pthread_mutex_destroy( & ctx->timers->mutex ); + httplib_pthread_mutex_destroy( & ctx->timers->mutex ); httplib_free( ctx->timers ); } diff --git a/src/httplib_uninitialize_ssl.c b/src/httplib_uninitialize_ssl.c index b49ce3b5..e5f9eea2 100644 --- a/src/httplib_uninitialize_ssl.c +++ b/src/httplib_uninitialize_ssl.c @@ -41,8 +41,9 @@ #if !defined(NO_SSL) void XX_httplib_uninitialize_ssl( struct httplib_context *ctx ) { + UNUSED_PARAMETER(ctx); + int i; - (void)ctx; if (httplib_atomic_dec(&XX_httplib_cryptolib_users) == 0) { @@ -60,7 +61,7 @@ void XX_httplib_uninitialize_ssl( struct httplib_context *ctx ) { ERR_remove_state(0); for (i = 0; i < CRYPTO_num_locks(); i++) { - pthread_mutex_destroy(&XX_httplib_ssl_mutexes[i]); + httplib_pthread_mutex_destroy( & XX_httplib_ssl_mutexes[i] ); } httplib_free( XX_httplib_ssl_mutexes ); XX_httplib_ssl_mutexes = NULL; diff --git a/src/httplib_worker_thread.c b/src/httplib_worker_thread.c index 122bd121..ac63c872 100644 --- a/src/httplib_worker_thread.c +++ b/src/httplib_worker_thread.c @@ -154,7 +154,7 @@ static void *worker_thread_run( struct worker_thread_args *thread_args ) { #if defined(_WIN32) CloseHandle( tls.pthread_cond_helper_mutex ); #endif - pthread_mutex_destroy( & conn->mutex ); + httplib_pthread_mutex_destroy( & conn->mutex ); httplib_free( conn ); return NULL;