diff --git a/Makefile b/Makefile index 5447186b..f8bb94a2 100644 --- a/Makefile +++ b/Makefile @@ -379,7 +379,7 @@ OBJLIST = \ ${OBJDIR}win32_pthread_cond_timedwait${OBJEXT} \ ${OBJDIR}win32_pthread_cond_wait${OBJEXT} \ ${OBJDIR}win32_pthread_getspecific${OBJEXT} \ - ${OBJDIR}win32_pthread_key_create${OBJEXT} \ + ${OBJDIR}httplib_pthread_key_create${OBJEXT} \ ${OBJDIR}httplib_pthread_key_delete${OBJEXT} \ ${OBJDIR}httplib_pthread_mutex_destroy${OBJEXT} \ ${OBJDIR}httplib_pthread_mutex_init${OBJEXT} \ @@ -1426,8 +1426,7 @@ ${OBJDIR}win32_pthread_getspecific${OBJEXT} : ${SRCDIR}win32_pthread_getspeci ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h -${OBJDIR}win32_pthread_key_create${OBJEXT} : ${SRCDIR}win32_pthread_key_create.c \ - ${SRCDIR}httplib_pthread.h \ +${OBJDIR}httplib_pthread_key_create${OBJEXT} : ${SRCDIR}httplib_pthread_key_create.c \ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h diff --git a/doc/api/httplib_pthread_key_create.md b/doc/api/httplib_pthread_key_create.md new file mode 100644 index 00000000..fa0246f4 --- /dev/null +++ b/doc/api/httplib_pthread_key_create.md @@ -0,0 +1,29 @@ +# LibHTTP API Reference + +### `httplib_pthread_key_create( key, destructor );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`key`**|`pthread_key_t`|The key of the variable to set| +|**`destructor`**|`void (*destructor)(void *)`|Pointer to destructor function to be called when the key is destroyed| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Integer value with the result of the function| + +### Description + +The platform independent function `httplib_pthread_key_create()` creates a key to a memory area for thread specific data storage. 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_key_create()`. Otherwise own code is used to emulate the same behaviour. + +Please not that on systems which don't support the `pthread_key_create()` function natively that the `destructor` parameter is ignored. For multiplatform execution you should check that this will not cause problems regarding the functionality. + +### See Also + +* [`httplib_pthread_getspecific();`](httplib_pthread_getspecific.md) +* [`httplib_pthread_key_delete();`](httplib_pthread_key_delete.md) +* [`httplib_pthread_setspecific();`](httplib_pthread_setspecific.md) +* [`httplib_pthread_self();`](httplib_pthread_self.md) diff --git a/include/libhttp.h b/include/libhttp.h index 5e6d6e5b..e52fa472 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_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 ); LIBHTTP_API int httplib_pthread_mutex_init( pthread_mutex_t *mutex, const pthread_mutexattr_t *attr ); diff --git a/src/httplib_pthread.h b/src/httplib_pthread.h index 4e6542d0..101f86f2 100644 --- a/src/httplib_pthread.h +++ b/src/httplib_pthread.h @@ -37,8 +37,6 @@ 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 ); -int pthread_key_create( pthread_key_t *key, void (*destructor)(void *) ); - void * pthread_getspecific( pthread_key_t key ); #endif /* _WIN32 */ diff --git a/src/win32_pthread_key_create.c b/src/httplib_pthread_key_create.c similarity index 61% rename from src/win32_pthread_key_create.c rename to src/httplib_pthread_key_create.c index cca8d73f..56de9c5c 100644 --- a/src/win32_pthread_key_create.c +++ b/src/httplib_pthread_key_create.c @@ -22,21 +22,39 @@ * THE SOFTWARE. * * ============ - * Release: 1.8 + * Release: 2.0 */ #include "httplib_main.h" -#include "httplib_pthread.h" -#ifdef _WIN32 +/* + * int httplib_pthread_key_create( pthread_key_t *key, void (*destructor)(void *) ); + * + * The function httplib_pthread_key_create() creates a key which can be used to + * reference an area for thread dependent storage. The function returns 0 when + * succesful and a non zero value otherwise. On systems which support it, the + * function is implemented as a wrapper around pthread_key_create(). On other + * systems own code is used to emulate the same behavior. + * + * Please note that on systems without a native implementation of the function + * pthread_key_create() that the parameter destructor is ignored. + */ -int pthread_key_create( pthread_key_t *key, void (*destructor)(void *) ) { +int httplib_pthread_key_create( pthread_key_t *key, void (*destructor)(void *) ) { + +#if defined(_WIN32) + + UNUSED_PARAMETER(destructor); if ( key == NULL ) return -2; *key = TlsAlloc(); return ( *key != TLS_OUT_OF_INDEXES ) ? 0 : -1; -} /* pthread_key_create */ +#else /* _WIN32 */ + + return pthread_key_create( key, destructor ); #endif /* _WIN32 */ + +} /* httplib_pthread_key_create */ diff --git a/src/httplib_start.c b/src/httplib_start.c index ca43902d..1364ad3e 100644 --- a/src/httplib_start.c +++ b/src/httplib_start.c @@ -74,7 +74,7 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks pthread_mutexattr_settype( & XX_httplib_pthread_mutex_attr, PTHREAD_MUTEX_RECURSIVE ); #endif - if ( 0 != pthread_key_create( & XX_httplib_sTlsKey, XX_httplib_tls_dtor ) ) { + if ( 0 != httplib_pthread_key_create( & XX_httplib_sTlsKey, XX_httplib_tls_dtor ) ) { /* Fatal error - abort start. However, this situation should * never * occur in practice. */