From 4cd956f09e2a824fad935386f6f66d5199b4c9ad Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Mon, 19 Dec 2016 14:42:22 +0100 Subject: [PATCH] Made httplib_pthread_setspecific() global --- Makefile | 5 ++-- doc/APIReference.md | 1 + doc/api/httplib_pthread_self.md | 3 +++ doc/api/httplib_pthread_setspecific.md | 25 ++++++++++++++++++ include/libhttp.h | 2 ++ src/httplib_main.h | 1 - src/httplib_pthread.h | 1 - ...ecific.c => httplib_pthread_setspecific.c} | 26 +++++++++++-------- 8 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 doc/api/httplib_pthread_setspecific.md rename src/{win32_pthread_setspecific.c => httplib_pthread_setspecific.c} (67%) diff --git a/Makefile b/Makefile index e64dca58..0c233551 100644 --- a/Makefile +++ b/Makefile @@ -387,7 +387,7 @@ OBJLIST = \ ${OBJDIR}win32_pthread_mutex_trylock${OBJEXT} \ ${OBJDIR}win32_pthread_mutex_unlock${OBJEXT} \ ${OBJDIR}httplib_pthread_self${OBJEXT} \ - ${OBJDIR}win32_pthread_setspecific${OBJEXT} \ + ${OBJDIR}httplib_pthread_setspecific${OBJEXT} \ ${OBJDIR}wince_gmtime${OBJEXT} \ ${OBJDIR}wince_gmtime_s${OBJEXT} \ ${OBJDIR}wince_localtime${OBJEXT} \ @@ -1465,8 +1465,7 @@ ${OBJDIR}httplib_pthread_self${OBJEXT} : ${SRCDIR}httplib_pthread_self.c ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h -${OBJDIR}win32_pthread_setspecific${OBJEXT} : ${SRCDIR}win32_pthread_setspecific.c \ - ${SRCDIR}httplib_pthread.h \ +${OBJDIR}httplib_pthread_setspecific${OBJEXT} : ${SRCDIR}httplib_pthread_setspecific.c \ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h diff --git a/doc/APIReference.md b/doc/APIReference.md index bb73115e..26f8152f 100644 --- a/doc/APIReference.md +++ b/doc/APIReference.md @@ -101,6 +101,7 @@ LibHTTP is often used as HTTP and HTTPS library inside a larger application. A * [`httplib_lock_context( ctx );`](api/httplib_lock_context.md) * [`httplib_poll( pfd, nfds, timeout );`](api/httplib_poll.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) * [`httplib_unlock_connection( conn );`](api/httplib_unlock_connection.md) * [`httplib_unlock_context( ctx );`](api/httplib_unlock_context.md) diff --git a/doc/api/httplib_pthread_self.md b/doc/api/httplib_pthread_self.md index 39c20f34..a4e18548 100644 --- a/doc/api/httplib_pthread_self.md +++ b/doc/api/httplib_pthread_self.md @@ -17,3 +17,6 @@ The function `httplib_pthread_self()` is a platform indepent function which returns an identifier which identifies the current thread. On systems which support it this is done with a call to the `pthread_self()` function. On other systems an OS specific function is used with equivalent functionality. ### See Also + +* [`httplib_pthread_getspecific();`](httplib_pthread_getspecific.md) +* [`httplib_pthread_setspecific();`](httplib_pthread_setspecific.md) diff --git a/doc/api/httplib_pthread_setspecific.md b/doc/api/httplib_pthread_setspecific.md new file mode 100644 index 00000000..ca7f0da0 --- /dev/null +++ b/doc/api/httplib_pthread_setspecific.md @@ -0,0 +1,25 @@ +# LibHTTP API Reference + +### `httplib_pthread_setspecific( key, value );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`key`**|`pthread_key_t`|The key of the variable to set| +|**`value`**|`const void *`|The value to be assigned to the key| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Integer value with the result of the function| + +### Description + +The platform independent function `httplib_pthread_setspecific()` is used to set a key value for a previously obtained thread specific key. The function returns **0** when successful and an error code if something went wrong. On systems which support it, the functionality is implemented as a direct call to `pthread_setspecific()`. Otherwise an OS dependent alternative function is called. + +### See Also + +* [`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 c7337129..ff17e9ae 100644 --- a/include/libhttp.h +++ b/include/libhttp.h @@ -71,6 +71,7 @@ #define SIGKILL (0) typedef HANDLE pthread_t; +typedef DWORD pthread_key_t; #else /* _WIN32 */ @@ -984,6 +985,7 @@ 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 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 ); LIBHTTP_API int httplib_remove( const char *path ); LIBHTTP_API void httplib_set_alloc_callback_func( httplib_alloc_callback_func log_func ); diff --git a/src/httplib_main.h b/src/httplib_main.h index 5a060c67..2fc580f1 100644 --- a/src/httplib_main.h +++ b/src/httplib_main.h @@ -271,7 +271,6 @@ typedef long off_t; #endif /* !fileno MINGW #defines fileno */ typedef HANDLE pthread_mutex_t; -typedef DWORD pthread_key_t; typedef struct { CRITICAL_SECTION threadIdSec; struct httplib_workerTLS * waiting_thread; /* The chain of threads */ diff --git a/src/httplib_pthread.h b/src/httplib_pthread.h index 7ca408b7..759fabf4 100644 --- a/src/httplib_pthread.h +++ b/src/httplib_pthread.h @@ -47,6 +47,5 @@ int pthread_mutex_trylock( pthread_mutex_t *mutex ); int pthread_mutex_unlock( pthread_mutex_t *mutex ); void * pthread_getspecific( pthread_key_t key ); -int pthread_setspecific( pthread_key_t key, void *value ); #endif /* _WIN32 */ diff --git a/src/win32_pthread_setspecific.c b/src/httplib_pthread_setspecific.c similarity index 67% rename from src/win32_pthread_setspecific.c rename to src/httplib_pthread_setspecific.c index 4b01f584..73e07ae9 100644 --- a/src/win32_pthread_setspecific.c +++ b/src/httplib_pthread_setspecific.c @@ -26,25 +26,29 @@ */ #include "httplib_main.h" -#include "httplib_pthread.h" - -#if defined(_WIN32) /* - * int pthread_set_specific( pthread_key_t key, void *value ); + * int httplib_pthread_setspecific( pthread_key_t key, const void *value ); * - * The function pthread_set_specific() sets a key value for a previously - * obtained thread specific key. The function returns 0 when succesful, or an - * error code if the function failed. This function is not available on all - * platforms and the implementation here is a replacement for Windows based - * systems. + * The platform independent function httplib_pthread_setspecific() is used to + * set a key value for a previously obtained thread specific key. The function + * returns 0 when successful, or an error code if the function failed. On + * systems which support it, the functionality is implemented as a direct call + * to the pthread_setspecific() function. Otherwise an OS dependent alternative + * function call is used. */ -int pthread_setspecific( pthread_key_t key, void *value ) { +int httplib_pthread_setspecific( pthread_key_t key, const void *value ) { + +#if defined(_WIN32) if ( TlsSetValue( key, value ) ) return 0; return GetLastError(); -} /* pthread_setspecific */ +#else /* _WIN32 */ + + return pthread_setspecific( key, value ); #endif /* _WIN32 */ + +} /* httplib_pthread_setspecific */