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

Made httplib_pthread_mutex_init global

This commit is contained in:
Lammert Bies
2016-12-19 16:33:32 +01:00
parent 248327c04f
commit e45f1187cd
7 changed files with 54 additions and 13 deletions

View File

@@ -382,7 +382,7 @@ OBJLIST = \
${OBJDIR}win32_pthread_key_create${OBJEXT} \ ${OBJDIR}win32_pthread_key_create${OBJEXT} \
${OBJDIR}win32_pthread_key_delete${OBJEXT} \ ${OBJDIR}win32_pthread_key_delete${OBJEXT} \
${OBJDIR}win32_pthread_mutex_destroy${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_lock${OBJEXT} \
${OBJDIR}httplib_pthread_mutex_trylock${OBJEXT} \ ${OBJDIR}httplib_pthread_mutex_trylock${OBJEXT} \
${OBJDIR}httplib_pthread_mutex_unlock${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 \ ${SRCDIR}httplib_main.h \
${INCDIR}libhttp.h ${INCDIR}libhttp.h
${OBJDIR}win32_pthread_mutex_init${OBJEXT} : ${SRCDIR}win32_pthread_mutex_init.c \ ${OBJDIR}httplib_pthread_mutex_init${OBJEXT} : ${SRCDIR}httplib_pthread_mutex_init.c \
${SRCDIR}httplib_pthread.h \
${SRCDIR}httplib_main.h \ ${SRCDIR}httplib_main.h \
${INCDIR}libhttp.h ${INCDIR}libhttp.h

View File

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

View File

@@ -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 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_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_lock( pthread_mutex_t *mutex );
LIBHTTP_API int httplib_pthread_mutex_trylock( 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 ); LIBHTTP_API int httplib_pthread_mutex_unlock( pthread_mutex_t *mutex );

View File

@@ -102,7 +102,7 @@ void *event_create(void) {
struct posix_event *ret = XX_httplib_malloc(sizeof(struct posix_event)); struct posix_event *ret = XX_httplib_malloc(sizeof(struct posix_event));
if ( ret == NULL ) return NULL; 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 */ /* pthread mutex not available */
XX_httplib_free(ret); XX_httplib_free(ret);
return NULL; return NULL;

View File

@@ -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_key_delete( pthread_key_t key );
int pthread_mutex_destroy( pthread_mutex_t *mutex ); 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 ); void * pthread_getspecific( pthread_key_t key );

View File

@@ -22,21 +22,34 @@
* 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_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) #if defined(_WIN32)
int pthread_mutex_init( pthread_mutex_t *mutex, void *unused ) { UNUSED_PARAMETER(attr);
(void)unused;
*mutex = CreateMutex( NULL, FALSE, NULL ); *mutex = CreateMutex( NULL, FALSE, NULL );
return (*mutex == NULL) ? -1 : 0; return (*mutex == NULL) ? -1 : 0;
} /* pthread_mutex_init */ #else /* _WIN32 */
#endif /* _WIN32 */ return pthread_mutex_init( mutex, attr );
#endif
} /* httplib_pthread_mutex_init */

View File

@@ -176,7 +176,7 @@ static void * timer_thread( void *thread_func_param ) {
static int timers_init( struct httplib_context *ctx ) { static int timers_init( struct httplib_context *ctx ) {
ctx->timers = (struct ttimers *)httplib_calloc(sizeof(struct ttimers), 1); 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 */ /* Start timer thread */
httplib_start_thread_with_id(timer_thread, ctx, &ctx->timers->threadid); httplib_start_thread_with_id(timer_thread, ctx, &ctx->timers->threadid);