1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-09 03:22:45 +03:00

Made pthread_mutex_unlock global

This commit is contained in:
Lammert Bies
2016-12-19 15:19:56 +01:00
parent 8f8bc40eaa
commit 32af4a5d21
16 changed files with 77 additions and 29 deletions

View File

@@ -385,7 +385,7 @@ OBJLIST = \
${OBJDIR}win32_pthread_mutex_init${OBJEXT} \ ${OBJDIR}win32_pthread_mutex_init${OBJEXT} \
${OBJDIR}win32_pthread_mutex_lock${OBJEXT} \ ${OBJDIR}win32_pthread_mutex_lock${OBJEXT} \
${OBJDIR}win32_pthread_mutex_trylock${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_self${OBJEXT} \
${OBJDIR}httplib_pthread_setspecific${OBJEXT} \ ${OBJDIR}httplib_pthread_setspecific${OBJEXT} \
${OBJDIR}wince_gmtime${OBJEXT} \ ${OBJDIR}wince_gmtime${OBJEXT} \
@@ -1456,8 +1456,7 @@ ${OBJDIR}win32_pthread_mutex_trylock${OBJEXT} : ${SRCDIR}win32_pthread_mutex_
${SRCDIR}httplib_main.h \ ${SRCDIR}httplib_main.h \
${INCDIR}libhttp.h ${INCDIR}libhttp.h
${OBJDIR}win32_pthread_mutex_unlock${OBJEXT} : ${SRCDIR}win32_pthread_mutex_unlock.c \ ${OBJDIR}httplib_pthread_mutex_unlock${OBJEXT} : ${SRCDIR}httplib_pthread_mutex_unlock.c \
${SRCDIR}httplib_pthread.h \
${SRCDIR}httplib_main.h \ ${SRCDIR}httplib_main.h \
${INCDIR}libhttp.h ${INCDIR}libhttp.h

View File

@@ -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_connection( conn );`](api/httplib_lock_connection.md)
* [`httplib_lock_context( ctx );`](api/httplib_lock_context.md) * [`httplib_lock_context( ctx );`](api/httplib_lock_context.md)
* [`httplib_poll( pfd, nfds, timeout );`](api/httplib_poll.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_self();`](api/httplib_pthread_self.md)
* [`httplib_pthread_setspecific( key, value );`](api/httplib_pthread_setspecific.md) * [`httplib_pthread_setspecific( key, value );`](api/httplib_pthread_setspecific.md)
* [`httplib_start_thread( f, p );`](api/httplib_start_thread.md) * [`httplib_start_thread( f, p );`](api/httplib_start_thread.md)

View File

@@ -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)

View File

@@ -71,6 +71,7 @@
#define SIGKILL (0) #define SIGKILL (0)
typedef HANDLE pthread_t; typedef HANDLE pthread_t;
typedef HANDLE pthread_mutex_t;
typedef DWORD pthread_key_t; typedef DWORD pthread_key_t;
#else /* _WIN32 */ #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 int httplib_mkdir( const char *path, int mode );
LIBHTTP_API DIR * httplib_opendir( const char *name ); 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_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 pthread_t httplib_pthread_self( void );
LIBHTTP_API int httplib_pthread_setspecific( pthread_key_t key, const void *value ); LIBHTTP_API int httplib_pthread_setspecific( pthread_key_t key, const void *value );
LIBHTTP_API struct dirent * httplib_readdir( DIR *dir ); LIBHTTP_API struct dirent * httplib_readdir( DIR *dir );

View File

@@ -77,7 +77,7 @@ int XX_httplib_consume_socket( struct httplib_context *ctx, struct socket *sp, i
} }
pthread_cond_signal(&ctx->sq_empty); pthread_cond_signal(&ctx->sq_empty);
pthread_mutex_unlock(&ctx->thread_mutex); httplib_pthread_mutex_unlock( & ctx->thread_mutex );
return !ctx->stop_flag; return !ctx->stop_flag;
#undef QUEUE_SIZE #undef QUEUE_SIZE

View File

@@ -123,7 +123,7 @@ int event_wait(void *eventhdl) {
struct posix_event *ev = (struct posix_event *)eventhdl; struct posix_event *ev = (struct posix_event *)eventhdl;
pthread_mutex_lock(&(ev->mutex)); pthread_mutex_lock(&(ev->mutex));
pthread_cond_wait(&(ev->cond), &(ev->mutex)); pthread_cond_wait(&(ev->cond), &(ev->mutex));
pthread_mutex_unlock(&(ev->mutex)); httplib_pthread_mutex_unlock( & ev->mutex );
return 1; return 1;
} /* event_wait */ } /* event_wait */
@@ -134,7 +134,7 @@ int event_signal(void *eventhdl) {
struct posix_event *ev = (struct posix_event *)eventhdl; struct posix_event *ev = (struct posix_event *)eventhdl;
pthread_mutex_lock(&(ev->mutex)); pthread_mutex_lock(&(ev->mutex));
pthread_cond_signal(&(ev->cond)); pthread_cond_signal(&(ev->cond));
pthread_mutex_unlock(&(ev->mutex)); httplib_pthread_mutex_unlock( & ev->mutex );
return 1; return 1;
} /* event_signal */ } /* event_signal */

View File

@@ -51,6 +51,6 @@ void httplib_lock_connection( struct httplib_connection *conn ) {
void httplib_unlock_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 */ } /* httplib_unlock_connection */

View File

@@ -50,6 +50,6 @@ void httplib_lock_context( struct httplib_context *ctx ) {
void httplib_unlock_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 */ } /* httplib_unlock_context */

View File

@@ -270,7 +270,6 @@ typedef long off_t;
#define fileno(x) (_fileno(x)) #define fileno(x) (_fileno(x))
#endif /* !fileno MINGW #defines fileno */ #endif /* !fileno MINGW #defines fileno */
typedef HANDLE pthread_mutex_t;
typedef struct { typedef struct {
CRITICAL_SECTION threadIdSec; CRITICAL_SECTION threadIdSec;
struct httplib_workerTLS * waiting_thread; /* The chain of threads */ struct httplib_workerTLS * waiting_thread; /* The chain of threads */

View File

@@ -142,7 +142,7 @@ static void master_thread_run(void *thread_func_param) {
#else #else
pthread_cond_broadcast(&ctx->sq_full); pthread_cond_broadcast(&ctx->sq_full);
#endif #endif
pthread_mutex_unlock(&ctx->thread_mutex); httplib_pthread_mutex_unlock( & ctx->thread_mutex );
/* Join all worker threads to avoid leaking threads. */ /* Join all worker threads to avoid leaking threads. */
workerthreadcount = ctx->cfg_worker_threads; workerthreadcount = ctx->cfg_worker_threads;

View File

@@ -78,7 +78,7 @@ void XX_httplib_produce_socket(struct httplib_context *ctx, const struct socket
} }
pthread_cond_signal(&ctx->sq_full); pthread_cond_signal(&ctx->sq_full);
pthread_mutex_unlock(&ctx->thread_mutex); httplib_pthread_mutex_unlock( & ctx->thread_mutex );
#undef QUEUE_SIZE #undef QUEUE_SIZE
} /* XX_httplib_produce_socket */ } /* XX_httplib_produce_socket */

View File

@@ -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_init( pthread_mutex_t *mutex, void *unused );
int pthread_mutex_lock( pthread_mutex_t *mutex ); int pthread_mutex_lock( pthread_mutex_t *mutex );
int pthread_mutex_trylock( 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 ); void * pthread_getspecific( pthread_key_t key );

View File

@@ -22,18 +22,32 @@
* THE SOFTWARE. * THE SOFTWARE.
* *
* ============ * ============
* Release: 1.8 * Release: 2.0
*/ */
#include "httplib_main.h" #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) #if defined(_WIN32)
int pthread_mutex_unlock( pthread_mutex_t *mutex ) {
return ( ReleaseMutex( *mutex ) == 0 ) ? -1 : 0; return ( ReleaseMutex( *mutex ) == 0 ) ? -1 : 0;
} /* pthread_mutex_unlock */ #else /* _WIN32 */
return pthread_mutex_unlock( mutex );
#endif /* _WIN32 */ #endif /* _WIN32 */
} /* httplib_pthread_mutex_unlock */

View File

@@ -40,10 +40,10 @@ void XX_httplib_send_authorization_request( struct httplib_connection *conn ) {
uint64_t nonce = (uint64_t)(conn->ctx->start_time); 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; nonce += conn->ctx->nonce_count;
++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; nonce ^= conn->ctx->auth_nonce_mask;
conn->status_code = 401; conn->status_code = 401;

View File

@@ -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 XX_httplib_ssl_locking_callback( int mode, int mutex_num, const char *file, int line ) {
(void)line; UNUSED_PARAMETER(line);
(void)file; UNUSED_PARAMETER(file);
if ( mode & 1 ) pthread_mutex_lock( & XX_httplib_ssl_mutexes[mutex_num] ); 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 */ } /* XX_httplib_ssl_locking_callback */

View File

@@ -96,7 +96,7 @@ static int timer_add( struct httplib_context *ctx, double next_time, double peri
ctx->timers->timers[u].arg = arg; ctx->timers->timers[u].arg = arg;
ctx->timers->timer_count++; ctx->timers->timer_count++;
} }
pthread_mutex_unlock(&ctx->timers->mutex); httplib_pthread_mutex_unlock( & ctx->timers->mutex );
return error; return error;
} /* timer_add */ } /* 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->timers[u - 1] = ctx->timers->timers[u];
} }
ctx->timers->timer_count--; ctx->timers->timer_count--;
pthread_mutex_unlock(&ctx->timers->mutex); httplib_pthread_mutex_unlock( & ctx->timers->mutex );
re_schedule = t.action(t.arg); re_schedule = t.action(t.arg);
if (re_schedule && (t.period > 0)) { if (re_schedule && (t.period > 0)) {
timer_add(ctx, t.time + t.period, t.period, 0, t.action, t.arg); timer_add(ctx, t.time + t.period, t.period, 0, t.action, t.arg);
} }
continue; continue;
} else {
pthread_mutex_unlock(&ctx->timers->mutex);
} }
else httplib_pthread_mutex_unlock( & ctx->timers->mutex );
httplib_sleep(1); httplib_sleep(1);
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);
d = (double)now.tv_sec + (double)now.tv_nsec * 1.0E-9; d = (double)now.tv_sec + (double)now.tv_nsec * 1.0E-9;