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

Made httplib_pthread_key_create global

This commit is contained in:
Lammert Bies
2016-12-19 18:14:11 +01:00
parent 799d4dad65
commit 1a3447e2a1
6 changed files with 56 additions and 11 deletions

View File

@@ -379,7 +379,7 @@ OBJLIST = \
${OBJDIR}win32_pthread_cond_timedwait${OBJEXT} \ ${OBJDIR}win32_pthread_cond_timedwait${OBJEXT} \
${OBJDIR}win32_pthread_cond_wait${OBJEXT} \ ${OBJDIR}win32_pthread_cond_wait${OBJEXT} \
${OBJDIR}win32_pthread_getspecific${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_key_delete${OBJEXT} \
${OBJDIR}httplib_pthread_mutex_destroy${OBJEXT} \ ${OBJDIR}httplib_pthread_mutex_destroy${OBJEXT} \
${OBJDIR}httplib_pthread_mutex_init${OBJEXT} \ ${OBJDIR}httplib_pthread_mutex_init${OBJEXT} \
@@ -1426,8 +1426,7 @@ ${OBJDIR}win32_pthread_getspecific${OBJEXT} : ${SRCDIR}win32_pthread_getspeci
${SRCDIR}httplib_main.h \ ${SRCDIR}httplib_main.h \
${INCDIR}libhttp.h ${INCDIR}libhttp.h
${OBJDIR}win32_pthread_key_create${OBJEXT} : ${SRCDIR}win32_pthread_key_create.c \ ${OBJDIR}httplib_pthread_key_create${OBJEXT} : ${SRCDIR}httplib_pthread_key_create.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_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)

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_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_key_delete( pthread_key_t key );
LIBHTTP_API int httplib_pthread_mutex_destroy( pthread_mutex_t *mutex ); 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 ); LIBHTTP_API int httplib_pthread_mutex_init( pthread_mutex_t *mutex, const pthread_mutexattr_t *attr );

View File

@@ -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_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_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 ); void * pthread_getspecific( pthread_key_t key );
#endif /* _WIN32 */ #endif /* _WIN32 */

View File

@@ -22,21 +22,39 @@
* THE SOFTWARE. * THE SOFTWARE.
* *
* ============ * ============
* Release: 1.8 * Release: 2.0
*/ */
#include "httplib_main.h" #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; if ( key == NULL ) return -2;
*key = TlsAlloc(); *key = TlsAlloc();
return ( *key != TLS_OUT_OF_INDEXES ) ? 0 : -1; return ( *key != TLS_OUT_OF_INDEXES ) ? 0 : -1;
} /* pthread_key_create */ #else /* _WIN32 */
return pthread_key_create( key, destructor );
#endif /* _WIN32 */ #endif /* _WIN32 */
} /* httplib_pthread_key_create */

View File

@@ -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 ); pthread_mutexattr_settype( & XX_httplib_pthread_mutex_attr, PTHREAD_MUTEX_RECURSIVE );
#endif #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 /* Fatal error - abort start. However, this situation should
* never * never
* occur in practice. */ * occur in practice. */