From 32af4a5d219b01bc51763683e0b76a8df8d9127e Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Mon, 19 Dec 2016 15:19:56 +0100 Subject: [PATCH] Made pthread_mutex_unlock global --- Makefile | 5 ++-- doc/APIReference.md | 5 ++++ doc/api/httplib_pthread_mutex_unlock.md | 29 +++++++++++++++++++ include/libhttp.h | 2 ++ src/httplib_consume_socket.c | 2 +- src/httplib_event_queue.c | 4 +-- src/httplib_lock_unlock_connection.c | 2 +- src/httplib_lock_unlock_context.c | 2 +- src/httplib_main.h | 1 - src/httplib_master_thread.c | 2 +- src/httplib_produce_socket.c | 2 +- src/httplib_pthread.h | 1 - ...nlock.c => httplib_pthread_mutex_unlock.c} | 26 +++++++++++++---- src/httplib_send_authorization_request.c | 4 +-- src/httplib_ssl_locking_callback.c | 6 ++-- src/httplib_timer.c | 13 +++++---- 16 files changed, 77 insertions(+), 29 deletions(-) create mode 100644 doc/api/httplib_pthread_mutex_unlock.md rename src/{win32_pthread_mutex_unlock.c => httplib_pthread_mutex_unlock.c} (68%) diff --git a/Makefile b/Makefile index 0c233551..d5ed702c 100644 --- a/Makefile +++ b/Makefile @@ -385,7 +385,7 @@ OBJLIST = \ ${OBJDIR}win32_pthread_mutex_init${OBJEXT} \ ${OBJDIR}win32_pthread_mutex_lock${OBJEXT} \ ${OBJDIR}win32_pthread_mutex_trylock${OBJEXT} \ - ${OBJDIR}win32_pthread_mutex_unlock${OBJEXT} \ + ${OBJDIR}httplib_pthread_mutex_unlock${OBJEXT} \ ${OBJDIR}httplib_pthread_self${OBJEXT} \ ${OBJDIR}httplib_pthread_setspecific${OBJEXT} \ ${OBJDIR}wince_gmtime${OBJEXT} \ @@ -1456,8 +1456,7 @@ ${OBJDIR}win32_pthread_mutex_trylock${OBJEXT} : ${SRCDIR}win32_pthread_mutex_ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h -${OBJDIR}win32_pthread_mutex_unlock${OBJEXT} : ${SRCDIR}win32_pthread_mutex_unlock.c \ - ${SRCDIR}httplib_pthread.h \ +${OBJDIR}httplib_pthread_mutex_unlock${OBJEXT} : ${SRCDIR}httplib_pthread_mutex_unlock.c \ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h diff --git a/doc/APIReference.md b/doc/APIReference.md index 26f8152f..d65013a4 100644 --- a/doc/APIReference.md +++ b/doc/APIReference.md @@ -100,6 +100,11 @@ 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_mutex_destroy( mutex );`](api/httplib_pthread_mutex_destroy.md) +* [`httplib_pthread_mutex_init( mutex, attr );`](api/httplib_pthread_mutex_init.md) +* [`httplib_pthread_mutex_lock( mutex );`](api/httplib_pthread_mutex_lock.md) +* [`httplib_pthread_mutex_trylock( mutex );`](api/httplib_pthread_mutex_trylock.md) +* [`httplib_pthread_mutex_unlock( mutex );`](api/httplib_pthread_mutex_unlock.md) * [`httplib_pthread_self();`](api/httplib_pthread_self.md) * [`httplib_pthread_setspecific( key, value );`](api/httplib_pthread_setspecific.md) * [`httplib_start_thread( f, p );`](api/httplib_start_thread.md) diff --git a/doc/api/httplib_pthread_mutex_unlock.md b/doc/api/httplib_pthread_mutex_unlock.md new file mode 100644 index 00000000..431ac62f --- /dev/null +++ b/doc/api/httplib_pthread_mutex_unlock.md @@ -0,0 +1,29 @@ +# LibHTTP API Reference + +### `httplib_pthread_mutex_unlock( mutex );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`mutex`**|`pthread_mutex_t`|The key to the mutex to unlock| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Integer value with the result of the function| + +### Description + +The platform independent function `httplib_pthread_mutex_unlock()` is used to remove a lock on a mutex after processing a critical section is finished. The function returns **0** when successful, or a non zero value with error number if something went wrong. On systems which support it, this function is implemented as a direct call to `pthread_mutex_unlock()`. For other systems an equivalent functionality has been coded in own code. + +### See Also + +* [`httplib_pthread_mutex_destroy();`](httplib_pthread_mutex_destroy.md) +* [`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_getspecific();`](httplib_pthread_getspecific.md) +* [`httplib_pthread_self();`](httplib_pthread_self.md) diff --git a/include/libhttp.h b/include/libhttp.h index ff17e9ae..c674899f 100644 --- a/include/libhttp.h +++ b/include/libhttp.h @@ -71,6 +71,7 @@ #define SIGKILL (0) typedef HANDLE pthread_t; +typedef HANDLE pthread_mutex_t; typedef DWORD pthread_key_t; #else /* _WIN32 */ @@ -984,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_unlock( pthread_mutex_t *mutex ); LIBHTTP_API pthread_t httplib_pthread_self( void ); LIBHTTP_API int httplib_pthread_setspecific( pthread_key_t key, const void *value ); LIBHTTP_API struct dirent * httplib_readdir( DIR *dir ); diff --git a/src/httplib_consume_socket.c b/src/httplib_consume_socket.c index ebb00de6..c6fd59e3 100644 --- a/src/httplib_consume_socket.c +++ b/src/httplib_consume_socket.c @@ -77,7 +77,7 @@ int XX_httplib_consume_socket( struct httplib_context *ctx, struct socket *sp, i } pthread_cond_signal(&ctx->sq_empty); - pthread_mutex_unlock(&ctx->thread_mutex); + httplib_pthread_mutex_unlock( & ctx->thread_mutex ); return !ctx->stop_flag; #undef QUEUE_SIZE diff --git a/src/httplib_event_queue.c b/src/httplib_event_queue.c index c7a56cb1..b8d7980c 100644 --- a/src/httplib_event_queue.c +++ b/src/httplib_event_queue.c @@ -123,7 +123,7 @@ int event_wait(void *eventhdl) { struct posix_event *ev = (struct posix_event *)eventhdl; pthread_mutex_lock(&(ev->mutex)); pthread_cond_wait(&(ev->cond), &(ev->mutex)); - pthread_mutex_unlock(&(ev->mutex)); + httplib_pthread_mutex_unlock( & ev->mutex ); return 1; } /* event_wait */ @@ -134,7 +134,7 @@ int event_signal(void *eventhdl) { struct posix_event *ev = (struct posix_event *)eventhdl; pthread_mutex_lock(&(ev->mutex)); pthread_cond_signal(&(ev->cond)); - pthread_mutex_unlock(&(ev->mutex)); + httplib_pthread_mutex_unlock( & ev->mutex ); return 1; } /* event_signal */ diff --git a/src/httplib_lock_unlock_connection.c b/src/httplib_lock_unlock_connection.c index f82bc78c..e4acfc35 100644 --- a/src/httplib_lock_unlock_connection.c +++ b/src/httplib_lock_unlock_connection.c @@ -51,6 +51,6 @@ void httplib_lock_connection( struct httplib_connection *conn ) { void httplib_unlock_connection( struct httplib_connection *conn ) { - if ( conn != NULL ) pthread_mutex_unlock( & conn->mutex ); + if ( conn != NULL ) httplib_pthread_mutex_unlock( & conn->mutex ); } /* httplib_unlock_connection */ diff --git a/src/httplib_lock_unlock_context.c b/src/httplib_lock_unlock_context.c index 4d2f8722..b9c87ce5 100644 --- a/src/httplib_lock_unlock_context.c +++ b/src/httplib_lock_unlock_context.c @@ -50,6 +50,6 @@ void httplib_lock_context( struct httplib_context *ctx ) { void httplib_unlock_context( struct httplib_context *ctx ) { - if ( ctx != NULL ) pthread_mutex_unlock( & ctx->nonce_mutex ); + if ( ctx != NULL ) httplib_pthread_mutex_unlock( & ctx->nonce_mutex ); } /* httplib_unlock_context */ diff --git a/src/httplib_main.h b/src/httplib_main.h index 2fc580f1..e8d044a4 100644 --- a/src/httplib_main.h +++ b/src/httplib_main.h @@ -270,7 +270,6 @@ typedef long off_t; #define fileno(x) (_fileno(x)) #endif /* !fileno MINGW #defines fileno */ -typedef HANDLE pthread_mutex_t; typedef struct { CRITICAL_SECTION threadIdSec; struct httplib_workerTLS * waiting_thread; /* The chain of threads */ diff --git a/src/httplib_master_thread.c b/src/httplib_master_thread.c index 4ed38996..e5de57af 100644 --- a/src/httplib_master_thread.c +++ b/src/httplib_master_thread.c @@ -142,7 +142,7 @@ static void master_thread_run(void *thread_func_param) { #else pthread_cond_broadcast(&ctx->sq_full); #endif - pthread_mutex_unlock(&ctx->thread_mutex); + httplib_pthread_mutex_unlock( & ctx->thread_mutex ); /* Join all worker threads to avoid leaking threads. */ workerthreadcount = ctx->cfg_worker_threads; diff --git a/src/httplib_produce_socket.c b/src/httplib_produce_socket.c index 9bd6a918..013ff0dc 100644 --- a/src/httplib_produce_socket.c +++ b/src/httplib_produce_socket.c @@ -78,7 +78,7 @@ void XX_httplib_produce_socket(struct httplib_context *ctx, const struct socket } pthread_cond_signal(&ctx->sq_full); - pthread_mutex_unlock(&ctx->thread_mutex); + httplib_pthread_mutex_unlock( & ctx->thread_mutex ); #undef QUEUE_SIZE } /* XX_httplib_produce_socket */ diff --git a/src/httplib_pthread.h b/src/httplib_pthread.h index 759fabf4..6028ca2f 100644 --- a/src/httplib_pthread.h +++ b/src/httplib_pthread.h @@ -44,7 +44,6 @@ int pthread_mutex_destroy( pthread_mutex_t *mutex ); int pthread_mutex_init( pthread_mutex_t *mutex, void *unused ); int pthread_mutex_lock( pthread_mutex_t *mutex ); int pthread_mutex_trylock( pthread_mutex_t *mutex ); -int pthread_mutex_unlock( pthread_mutex_t *mutex ); void * pthread_getspecific( pthread_key_t key ); diff --git a/src/win32_pthread_mutex_unlock.c b/src/httplib_pthread_mutex_unlock.c similarity index 68% rename from src/win32_pthread_mutex_unlock.c rename to src/httplib_pthread_mutex_unlock.c index 6319bd5d..8277dc49 100644 --- a/src/win32_pthread_mutex_unlock.c +++ b/src/httplib_pthread_mutex_unlock.c @@ -22,18 +22,32 @@ * THE SOFTWARE. * * ============ - * Release: 1.8 + * Release: 2.0 */ #include "httplib_main.h" -#include "httplib_pthread.h" + +/* + * int httplib_pthread_mutex_unlock( pthread_mutex_t *mutex ); + * + * The function httplib_pthread_mutex_unlock() is a platform independent + * function to unlock a mutex. On systems which support it, a call to the + * function pthread_mutex_unlock() is issued. Otherwise an alternative + * implementation is used with comparable semantics. + * + * The function returns 0 when successful and -1 on failure. + */ + +int httplib_pthread_mutex_unlock( pthread_mutex_t *mutex ) { #if defined(_WIN32) -int pthread_mutex_unlock( pthread_mutex_t *mutex ) { - return ( ReleaseMutex( *mutex ) == 0 ) ? -1 : 0; -} /* pthread_mutex_unlock */ +#else /* _WIN32 */ -#endif /* _WIN32 */ + return pthread_mutex_unlock( mutex ); + +#endif /* _WIN32 */ + +} /* httplib_pthread_mutex_unlock */ diff --git a/src/httplib_send_authorization_request.c b/src/httplib_send_authorization_request.c index 2e1280f1..37966ba4 100644 --- a/src/httplib_send_authorization_request.c +++ b/src/httplib_send_authorization_request.c @@ -40,10 +40,10 @@ void XX_httplib_send_authorization_request( struct httplib_connection *conn ) { uint64_t nonce = (uint64_t)(conn->ctx->start_time); - (void)pthread_mutex_lock(&conn->ctx->nonce_mutex); + pthread_mutex_lock(&conn->ctx->nonce_mutex); nonce += conn->ctx->nonce_count; ++conn->ctx->nonce_count; - (void)pthread_mutex_unlock(&conn->ctx->nonce_mutex); + httplib_pthread_mutex_unlock( & conn->ctx->nonce_mutex ); nonce ^= conn->ctx->auth_nonce_mask; conn->status_code = 401; diff --git a/src/httplib_ssl_locking_callback.c b/src/httplib_ssl_locking_callback.c index 48602a6d..a135dce2 100644 --- a/src/httplib_ssl_locking_callback.c +++ b/src/httplib_ssl_locking_callback.c @@ -42,11 +42,11 @@ pthread_mutex_t *XX_httplib_ssl_mutexes; void XX_httplib_ssl_locking_callback( int mode, int mutex_num, const char *file, int line ) { - (void)line; - (void)file; + UNUSED_PARAMETER(line); + UNUSED_PARAMETER(file); if ( mode & 1 ) pthread_mutex_lock( & XX_httplib_ssl_mutexes[mutex_num] ); - else pthread_mutex_unlock( & XX_httplib_ssl_mutexes[mutex_num] ); + else httplib_pthread_mutex_unlock( & XX_httplib_ssl_mutexes[mutex_num] ); } /* XX_httplib_ssl_locking_callback */ diff --git a/src/httplib_timer.c b/src/httplib_timer.c index 92604f3b..fc89dacb 100644 --- a/src/httplib_timer.c +++ b/src/httplib_timer.c @@ -90,13 +90,13 @@ static int timer_add( struct httplib_context *ctx, double next_time, double peri break; } } - ctx->timers->timers[u].time = next_time; + ctx->timers->timers[u].time = next_time; ctx->timers->timers[u].period = period; ctx->timers->timers[u].action = action; - ctx->timers->timers[u].arg = arg; + ctx->timers->timers[u].arg = arg; ctx->timers->timer_count++; } - pthread_mutex_unlock(&ctx->timers->mutex); + httplib_pthread_mutex_unlock( & ctx->timers->mutex ); return error; } /* timer_add */ @@ -136,15 +136,16 @@ static void timer_thread_run( void *thread_func_param ) { ctx->timers->timers[u - 1] = ctx->timers->timers[u]; } ctx->timers->timer_count--; - pthread_mutex_unlock(&ctx->timers->mutex); + httplib_pthread_mutex_unlock( & ctx->timers->mutex ); re_schedule = t.action(t.arg); if (re_schedule && (t.period > 0)) { timer_add(ctx, t.time + t.period, t.period, 0, t.action, t.arg); } continue; - } else { - pthread_mutex_unlock(&ctx->timers->mutex); } + + else httplib_pthread_mutex_unlock( & ctx->timers->mutex ); + httplib_sleep(1); clock_gettime(CLOCK_MONOTONIC, &now); d = (double)now.tv_sec + (double)now.tv_nsec * 1.0E-9;