diff --git a/Makefile b/Makefile index d5ed702c..93c76448 100644 --- a/Makefile +++ b/Makefile @@ -384,7 +384,7 @@ OBJLIST = \ ${OBJDIR}win32_pthread_mutex_destroy${OBJEXT} \ ${OBJDIR}win32_pthread_mutex_init${OBJEXT} \ ${OBJDIR}win32_pthread_mutex_lock${OBJEXT} \ - ${OBJDIR}win32_pthread_mutex_trylock${OBJEXT} \ + ${OBJDIR}httplib_pthread_mutex_trylock${OBJEXT} \ ${OBJDIR}httplib_pthread_mutex_unlock${OBJEXT} \ ${OBJDIR}httplib_pthread_self${OBJEXT} \ ${OBJDIR}httplib_pthread_setspecific${OBJEXT} \ @@ -1451,8 +1451,7 @@ ${OBJDIR}win32_pthread_mutex_lock${OBJEXT} : ${SRCDIR}win32_pthread_mutex_loc ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h -${OBJDIR}win32_pthread_mutex_trylock${OBJEXT} : ${SRCDIR}win32_pthread_mutex_trylock.c \ - ${SRCDIR}httplib_pthread.h \ +${OBJDIR}httplib_pthread_mutex_trylock${OBJEXT} : ${SRCDIR}httplib_pthread_mutex_trylock.c \ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h diff --git a/doc/api/httplib_pthread_mutex_trylock.md b/doc/api/httplib_pthread_mutex_trylock.md new file mode 100644 index 00000000..f6bb3561 --- /dev/null +++ b/doc/api/httplib_pthread_mutex_trylock.md @@ -0,0 +1,29 @@ +# LibHTTP API Reference + +### `httplib_pthread_mutex_trylock( mutex );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`mutex`**|`pthread_mutex_t`|The key to the mutex to try to lock| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Integer value with the result of the function| + +### Description + +The platform independent function `httplib_pthread_mutex_trylock()` is used to try to lock a mutex. The function returns **0** if this is succesful, or an error code if it fails. Depending on the error code the parameter my be invalid, or the mutex is already locked. On systems which support it, this function is implemented as a direct call to `pthread_mutex_trylock()`. On other systems own code is used to emulate the same functionality. + +### 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_unlock();`](httplib_pthread_mutex_unlock.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 c674899f..bf7120f4 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_trylock( pthread_mutex_t *mutex ); 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 ); diff --git a/src/httplib_pthread.h b/src/httplib_pthread.h index 6028ca2f..6af50924 100644 --- a/src/httplib_pthread.h +++ b/src/httplib_pthread.h @@ -43,7 +43,6 @@ 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 ); int pthread_mutex_lock( pthread_mutex_t *mutex ); -int pthread_mutex_trylock( pthread_mutex_t *mutex ); void * pthread_getspecific( pthread_key_t key ); diff --git a/src/win32_pthread_mutex_trylock.c b/src/httplib_pthread_mutex_trylock.c similarity index 72% rename from src/win32_pthread_mutex_trylock.c rename to src/httplib_pthread_mutex_trylock.c index 5c3f98be..7c6649c4 100644 --- a/src/win32_pthread_mutex_trylock.c +++ b/src/httplib_pthread_mutex_trylock.c @@ -22,16 +22,23 @@ * THE SOFTWARE. * * ============ - * Release: 1.8 + * Release: 2.0 */ #include "httplib_main.h" -#include "httplib_pthread.h" + +/* + * The platform independent function httplib_pthread_mutex_trylock() tries to + * put a lock on a mutex. It checks the state of the mutex and returns a value + * accordingly. On systems which support it, this function is just a wrapper + * around pthread_mutex_trylock(). On other systems the functionality is + * emulated with own code. + */ + +int httplib_pthread_mutex_trylock( pthread_mutex_t *mutex ) { #if defined(_WIN32) -int pthread_mutex_trylock( pthread_mutex_t *mutex ) { - switch ( WaitForSingleObject( *mutex, 0 ) ) { case WAIT_OBJECT_0: return 0; @@ -39,6 +46,10 @@ int pthread_mutex_trylock( pthread_mutex_t *mutex ) { } return -1; -} /* pthread_mutex_trylock */ +#else /* _WIN32 */ -#endif /* _WIN32 */ + return pthread_mutex_trylock( mutex ); + +#endif /* _WIN32 */ + +} /* httplib_pthread_mutex_trylock */