From f9be8dbfe43fdd8ba2361075f661f3ea716c88c0 Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Mon, 19 Dec 2016 18:27:47 +0100 Subject: [PATCH] Made httplib_pthread_getspecific global --- Makefile | 5 ++-- doc/api/httplib_pthread_getspecific.md | 26 +++++++++++++++++++ include/libhttp.h | 1 + src/httplib_pthread.h | 2 -- ...ecific.c => httplib_pthread_getspecific.c} | 26 +++++++++++++++---- src/httplib_ssl_id_callback.c | 2 +- src/httplib_tls_dtor.c | 2 +- 7 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 doc/api/httplib_pthread_getspecific.md rename src/{win32_pthread_getspecific.c => httplib_pthread_getspecific.c} (65%) diff --git a/Makefile b/Makefile index f8bb94a2..0fa0a309 100644 --- a/Makefile +++ b/Makefile @@ -378,7 +378,7 @@ OBJLIST = \ ${OBJDIR}win32_pthread_cond_signal${OBJEXT} \ ${OBJDIR}win32_pthread_cond_timedwait${OBJEXT} \ ${OBJDIR}win32_pthread_cond_wait${OBJEXT} \ - ${OBJDIR}win32_pthread_getspecific${OBJEXT} \ + ${OBJDIR}httplib_pthread_getspecific${OBJEXT} \ ${OBJDIR}httplib_pthread_key_create${OBJEXT} \ ${OBJDIR}httplib_pthread_key_delete${OBJEXT} \ ${OBJDIR}httplib_pthread_mutex_destroy${OBJEXT} \ @@ -1421,8 +1421,7 @@ ${OBJDIR}win32_pthread_cond_wait${OBJEXT} : ${SRCDIR}win32_pthread_cond_wait. ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h -${OBJDIR}win32_pthread_getspecific${OBJEXT} : ${SRCDIR}win32_pthread_getspecific.c \ - ${SRCDIR}httplib_pthread.h \ +${OBJDIR}httplib_pthread_getspecific${OBJEXT} : ${SRCDIR}httplib_pthread_getspecific.c \ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h diff --git a/doc/api/httplib_pthread_getspecific.md b/doc/api/httplib_pthread_getspecific.md new file mode 100644 index 00000000..c492760b --- /dev/null +++ b/doc/api/httplib_pthread_getspecific.md @@ -0,0 +1,26 @@ +# LibHTTP API Reference + +### `httplib_pthread_getspecific( key );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`key`**|`pthread_key_t`|The key of the variable to retrieve| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`void *`|Pointer to the memory area or NULL of no area could be found| + +### Description + +The platform independent function `httplib_pthread_getspecific()` is used to retrieve a memory area for a thread registered previously with a call to [`httplib_pthread_setspecific()`](httplib_pthread_setspecific.md). If no area could be found, the value `NULL` is returned instead. On systems which support it, the functionality is implemented as a direct call to `pthread_setspecific()`. On other systems own code is used to emulate the same behaviour. + +### See Also + +* [`httplib_pthread_key_create();`](httplib_pthread_key_create.md) +* [`httplib_pthread_key_delete();`](httplib_pthread_key_delete.md) +* [`httplib_pthread_self();`](httplib_pthread_self.md) +* [`httplib_pthread_setspecific();`](httplib_pthread_setspecific.md) diff --git a/include/libhttp.h b/include/libhttp.h index e52fa472..8eba7fec 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 void * httplib_pthread_getspecific( pthread_key_t key ); LIBHTTP_API int httplib_pthread_key_create( pthread_key_t *key, void (*destructor)(void *) ); LIBHTTP_API int httplib_pthread_key_delete( pthread_key_t key ); LIBHTTP_API int httplib_pthread_mutex_destroy( pthread_mutex_t *mutex ); diff --git a/src/httplib_pthread.h b/src/httplib_pthread.h index 101f86f2..9be57e55 100644 --- a/src/httplib_pthread.h +++ b/src/httplib_pthread.h @@ -37,6 +37,4 @@ int pthread_cond_signal( pthread_cond_t *cv ); int pthread_cond_timedwait( pthread_cond_t *cv, pthread_mutex_t *mutex, const struct timespec *abstime ); int pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mutex ); -void * pthread_getspecific( pthread_key_t key ); - #endif /* _WIN32 */ diff --git a/src/win32_pthread_getspecific.c b/src/httplib_pthread_getspecific.c similarity index 65% rename from src/win32_pthread_getspecific.c rename to src/httplib_pthread_getspecific.c index c19b5e42..d2292a23 100644 --- a/src/win32_pthread_getspecific.c +++ b/src/httplib_pthread_getspecific.c @@ -22,18 +22,34 @@ * THE SOFTWARE. * * ============ - * Release: 1.8 + * Release: 2.0 */ #include "httplib_main.h" -#include "httplib_pthread.h" + +/* + * void *httplib_pthread_getspecific( pthread_key_t key ); + * + * Thei platform independent function httplib_pthread_getspecific() returns + * data registered for a specific thread with a call to the function + * httplib_pthread_setspecific(). If no data could be found the value NULL is + * returned. + * + * If supported by the platform, the function is implemented as a wrapper + * around the pthread_getspecific() function. On other platforms equivalent + * code for that system is used. + */ + +void *httplib_pthread_getspecific( pthread_key_t key ) { #if defined(_WIN32) -void * pthread_getspecific(pthread_key_t key) { + return TlsGetValue( key ); - return TlsGetValue(key); +#else /* _WIN32 */ -} /* pthread_getspecific */ + return pthread_getspecific( key ); #endif /* _WIN32 */ + +} /* httplib_pthread_getspecific */ diff --git a/src/httplib_ssl_id_callback.c b/src/httplib_ssl_id_callback.c index ec4c8513..9c2b73d3 100644 --- a/src/httplib_ssl_id_callback.c +++ b/src/httplib_ssl_id_callback.c @@ -60,7 +60,7 @@ unsigned long XX_httplib_ssl_id_callback( void ) { if (sizeof(pthread_t) > sizeof(unsigned long)) { /* This is the problematic case for CRYPTO_set_id_callback: * The OS pthread_t can not be cast to unsigned long. */ - struct httplib_workerTLS *tls = (struct httplib_workerTLS *)pthread_getspecific(XX_httplib_sTlsKey); + struct httplib_workerTLS *tls = httplib_pthread_getspecific( XX_httplib_sTlsKey ); if (tls == NULL) { /* SSL called from an unknown thread: Create some thread index. */ diff --git a/src/httplib_tls_dtor.c b/src/httplib_tls_dtor.c index 84a81d2d..3b5722e1 100644 --- a/src/httplib_tls_dtor.c +++ b/src/httplib_tls_dtor.c @@ -40,7 +40,7 @@ void XX_httplib_tls_dtor( void *key ) { struct httplib_workerTLS *tls = (struct httplib_workerTLS *)key; - /* key == pthread_getspecific( XX_httplib_sTlsKey ); */ + /* key == httplib_pthread_getspecific( XX_httplib_sTlsKey ); */ if ( tls != NULL ) {