From d6087662f5c19b693a000126d02ced43aad5d2f7 Mon Sep 17 00:00:00 2001 From: blacksirius Date: Sun, 3 Jun 2012 20:09:39 +0000 Subject: thread api: added support for platforms without TLS (Thread Local Storage) support (usually older OSX llvm versions ..) git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@16226 54d463be-8e91-2dee-dedb-b68131a5f0ec --- configure | 44 +++++++++++++++++++++++++++++++++++++++++++- configure.in | 24 ++++++++++++++++++++++++ src/common/thread.c | 42 +++++++++++++++++++++++++++++++++++++++++- src/common/thread.h | 3 ++- 4 files changed, 110 insertions(+), 3 deletions(-) diff --git a/configure b/configure index 7cb751351..67d416776 100755 --- a/configure +++ b/configure @@ -1,5 +1,5 @@ #! /bin/sh -# From configure.in Revision: 16203 . +# From configure.in Revision: 16221 . # Guess values for system-dependent variables and create Makefiles. # Generated by GNU Autoconf 2.67. # @@ -4708,6 +4708,48 @@ fi +# +# Check if CC supports __thread attribute (Thread Local Storage) +# (Usually our OSX friends 're lacking support of it in older llvm versions ..) +# +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CC supports __thread specifier (TLS)" >&5 +$as_echo_n "checking whether $CC supports __thread specifier (TLS)... " >&6; } +if test "$cross_compiling" = yes; then : + { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5 +$as_echo "$as_me: error: in \`$ac_pwd':" >&2;} +as_fn_error $? "cannot run test program while cross compiling +See \`config.log' for more details" "$LINENO" 5 ; } +else + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + + __thread int g_Test = -1; + + int main(int argc, char **argv){ + g_Test = 0; + return g_Test; + } + +_ACEOF +if ac_fn_c_try_run "$LINENO"; then : + + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 +$as_echo "yes" >&6; } + CFLAGS="$CFLAGS -DHAS_TLS" + +else + + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } + + +fi +rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \ + conftest.$ac_objext conftest.beam conftest.$ac_ext +fi + + + { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CC supports -Wno-unused-parameter" >&5 $as_echo_n "checking whether $CC supports -Wno-unused-parameter... " >&6; } OLD_CFLAGS="$CFLAGS" diff --git a/configure.in b/configure.in index 2d3df1ce2..e602db19f 100644 --- a/configure.in +++ b/configure.in @@ -363,6 +363,30 @@ AC_RUN_IFELSE( ) +# +# Check if CC supports __thread attribute (Thread Local Storage) +# (Usually our OSX friends 're lacking support of it in older llvm versions ..) +# +AC_MSG_CHECKING([whether $CC supports __thread specifier (TLS)]) +AC_RUN_IFELSE( + [ + __thread int g_Test = -1; + + int main(int argc, char **argv){ + g_Test = 0; + return g_Test; + } + ], + [ + AC_MSG_RESULT([yes]) + CFLAGS="$CFLAGS -DHAS_TLS" + ], + [ + AC_MSG_RESULT([no]) + ] +) + + AC_MSG_CHECKING([whether $CC supports -Wno-unused-parameter]) OLD_CFLAGS="$CFLAGS" CFLAGS="$CFLAGS -Wno-unused-parameter" diff --git a/src/common/thread.c b/src/common/thread.c index 49accff4c..728c6c66a 100644 --- a/src/common/thread.c +++ b/src/common/thread.c @@ -17,6 +17,11 @@ #include "showmsg.h" #include "thread.h" +// When Compiling using MSC (on win32..) we know we have support in any case! +#ifdef _MSC_VER +#define HAS_TLS +#endif + #define RA_THREADS_MAX 64 @@ -35,7 +40,9 @@ struct rAthread { }; +#ifdef HAS_TLS __thread int g_rathread_ID = -1; +#endif /// @@ -52,7 +59,9 @@ void rathread_init(){ } // now lets init thread id 0, which represnts the main thread +#ifdef HAS_TLS g_rathread_ID = 0; +#endif l_threads[0].prio = RAT_PRIO_NORMAL; l_threads[0].proc = (rAthreadProc)0xDEADCAFE; @@ -100,7 +109,9 @@ static void *_raThreadMainRedirector( void *p ){ void *ret; // Update myID @ TLS to right id. +#ifdef HAS_TLS g_rathread_ID = ((rAthread)p)->myID; +#endif #ifndef WIN32 // When using posix threads @@ -216,18 +227,47 @@ void rathread_destroy ( rAthread handle ){ }//end: rathread_destroy() rAthread rathread_self( ){ +#ifdef HAS_TLS rAthread handle = &l_threads[g_rathread_ID]; if(handle->proc != NULL) // entry point set, so its used! return handle; +#else + // .. so no tls means we have to search the thread by its api-handle .. + int i; + + #ifdef WIN32 + HANDLE hSelf; + hSelf = GetCurrent = GetCurrentThread(); + #else + pthread_t hSelf; + hSelf = pthread_self(); + #endif + + for(i = 0; i < RA_THREADS_MAX; i++){ + if(l_threads[i].hThread == hSelf && l_threads[i].proc != NULL) + return &l_threads[i]; + } + +#endif return NULL; }//end: rathread_self() int rathread_get_tid(){ - + +#ifdef HAS_TLS return g_rathread_ID; +#else + // todo + #ifdef WIN32 + return (int)GetCurrentThreadId(); + #else + return (int)pthread_self(); + #endif + +#endif }//end: rathread_get_tid() diff --git a/src/common/thread.h b/src/common/thread.h index d4027811f..8d3441868 100644 --- a/src/common/thread.h +++ b/src/common/thread.h @@ -62,7 +62,8 @@ rAthread rathread_self( ); /** * Returns own thrad id (TID) * - * @note this is not the operating system THREAD ID! + * @note this is an unique identifier for the calling thread, and + * depends on platfrom / compiler, and may not be the systems Thread ID! * * @return -1 when fails, otherwise >= 0 */ -- cgit v1.2.3-70-g09d2