From b67b84dafd896604f33a3e8fab8edbdc23f7d1e3 Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Mon, 19 Dec 2016 14:02:32 +0100 Subject: [PATCH] Made httplib_pthread_self global --- Makefile | 5 ++-- doc/APIReference.md | 1 + include/libhttp.h | 4 ++++ src/httplib_main.h | 1 - src/httplib_master_thread.c | 2 +- src/httplib_pthread.h | 1 - ..._pthread_self.c => httplib_pthread_self.c} | 23 +++++++++++-------- src/httplib_set_thread_name.c | 2 +- src/httplib_ssl_id_callback.c | 2 +- 9 files changed, 23 insertions(+), 18 deletions(-) rename src/{win32_pthread_self.c => httplib_pthread_self.c} (73%) diff --git a/Makefile b/Makefile index adead3a4..e64dca58 100644 --- a/Makefile +++ b/Makefile @@ -386,7 +386,7 @@ OBJLIST = \ ${OBJDIR}win32_pthread_mutex_lock${OBJEXT} \ ${OBJDIR}win32_pthread_mutex_trylock${OBJEXT} \ ${OBJDIR}win32_pthread_mutex_unlock${OBJEXT} \ - ${OBJDIR}win32_pthread_self${OBJEXT} \ + ${OBJDIR}httplib_pthread_self${OBJEXT} \ ${OBJDIR}win32_pthread_setspecific${OBJEXT} \ ${OBJDIR}wince_gmtime${OBJEXT} \ ${OBJDIR}wince_gmtime_s${OBJEXT} \ @@ -1461,8 +1461,7 @@ ${OBJDIR}win32_pthread_mutex_unlock${OBJEXT} : ${SRCDIR}win32_pthread_mutex_u ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h -${OBJDIR}win32_pthread_self${OBJEXT} : ${SRCDIR}win32_pthread_self.c \ - ${SRCDIR}httplib_pthread.h \ +${OBJDIR}httplib_pthread_self${OBJEXT} : ${SRCDIR}httplib_pthread_self.c \ ${SRCDIR}httplib_main.h \ ${INCDIR}libhttp.h diff --git a/doc/APIReference.md b/doc/APIReference.md index 4dfe9635..bb73115e 100644 --- a/doc/APIReference.md +++ b/doc/APIReference.md @@ -100,6 +100,7 @@ LibHTTP is often used as HTTP and HTTPS library inside a larger application. A * [`httplib_lock_connection( conn );`](api/httplib_lock_connection.md) * [`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_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/include/libhttp.h b/include/libhttp.h index 376d8b34..c7337129 100644 --- a/include/libhttp.h +++ b/include/libhttp.h @@ -70,6 +70,8 @@ #define SIGKILL (0) +typedef HANDLE pthread_t; + #else /* _WIN32 */ /* @@ -80,6 +82,7 @@ #include #include #include +#include #include #endif /* _WIN32 */ @@ -980,6 +983,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 pthread_t httplib_pthread_self( void ); 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 72e93217..5a060c67 100644 --- a/src/httplib_main.h +++ b/src/httplib_main.h @@ -272,7 +272,6 @@ typedef long off_t; typedef HANDLE pthread_mutex_t; typedef DWORD pthread_key_t; -typedef HANDLE pthread_t; typedef struct { CRITICAL_SECTION threadIdSec; struct httplib_workerTLS * waiting_thread; /* The chain of threads */ diff --git a/src/httplib_master_thread.c b/src/httplib_master_thread.c index 16259a45..ba4c7120 100644 --- a/src/httplib_master_thread.c +++ b/src/httplib_master_thread.c @@ -82,7 +82,7 @@ static void master_thread_run(void *thread_func_param) { && ((USE_MASTER_THREAD_PRIORITY) >= min_prio)) { struct sched_param sched_param = {0}; sched_param.sched_priority = (USE_MASTER_THREAD_PRIORITY); - pthread_setschedparam(pthread_self(), SCHED_RR, &sched_param); + pthread_setschedparam( httplib_pthread_self(), SCHED_RR, & sched_param ); } #endif diff --git a/src/httplib_pthread.h b/src/httplib_pthread.h index cd268de8..7ca408b7 100644 --- a/src/httplib_pthread.h +++ b/src/httplib_pthread.h @@ -48,6 +48,5 @@ int pthread_mutex_unlock( pthread_mutex_t *mutex ); void * pthread_getspecific( pthread_key_t key ); int pthread_setspecific( pthread_key_t key, void *value ); -DWORD pthread_self( void ); #endif /* _WIN32 */ diff --git a/src/win32_pthread_self.c b/src/httplib_pthread_self.c similarity index 73% rename from src/win32_pthread_self.c rename to src/httplib_pthread_self.c index db8241ec..76b964bd 100644 --- a/src/win32_pthread_self.c +++ b/src/httplib_pthread_self.c @@ -26,23 +26,26 @@ */ #include "httplib_main.h" -#include "httplib_pthread.h" - -#ifdef _WIN32 /* - * DWORD pthread_self( void ); + * pthread_t httplib_pthread_self( void ); * - * The function pthread_self() returns in ID describing the current thread. On - * windows where there is no support for the pthread() library, this - * implementation provides a comparable function which can be used as an - * alternative. + * The function httplib_pthread_self() provides a platform independent way to + * retrieve an ID associated with the current thread. On systems which support + * it a call to pthread_self() is used. Otherwise a system specific value is + * returned which best resembles the functionality of the pthread_ equivalent. */ -DWORD pthread_self( void ) { +pthread_t httplib_pthread_self( void ) { + +#if defined(_WIN32) return GetCurrentThreadId(); -} /* pthread_self */ +#else /* _WIN32 */ + + return pthread_self(); #endif /* _WIN32 */ + +} /* httplib_pthread_self */ diff --git a/src/httplib_set_thread_name.c b/src/httplib_set_thread_name.c index 11aaa964..7a014d13 100644 --- a/src/httplib_set_thread_name.c +++ b/src/httplib_set_thread_name.c @@ -79,7 +79,7 @@ void XX_httplib_set_thread_name(const char *name) { #elif defined(__GLIBC__) \ && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 12))) /* pthread_setname_np first appeared in glibc in version 2.12*/ - pthread_setname_np(pthread_self(), threadName); + pthread_setname_np( httplib_pthread_self(), threadName ); #elif defined(__linux__) /* on linux we can use the old prctl function */ prctl(PR_SET_NAME, threadName, 0, 0, 0); diff --git a/src/httplib_ssl_id_callback.c b/src/httplib_ssl_id_callback.c index 34297504..0de9c5a7 100644 --- a/src/httplib_ssl_id_callback.c +++ b/src/httplib_ssl_id_callback.c @@ -75,7 +75,7 @@ unsigned long XX_httplib_ssl_id_callback( void ) { * can rise a warning/error, depending on the platform. * Here memcpy is used as an anything-to-anything cast. */ unsigned long ret = 0; - pthread_t t = pthread_self(); + pthread_t t = httplib_pthread_self(); memcpy(&ret, &t, sizeof(pthread_t)); return ret; }