Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -550,10 +550,45 @@ AC_ARG_ENABLE(compiler-tls, AS_HELP_STRING([--disable-compiler-tls], [disable compiler thread local storage])) AS_IF([test x"$enable_compiler_tls" != x"no"], [ + AC_CHECK_HEADERS(threads.h, [ + AC_DEFINE(OF_HAVE_THREADS_H, 1, + [Whether we have threads.h]) + ]) + + AC_MSG_CHECKING(whether _Thread_local works) + AC_TRY_LINK([ + static _Thread_local int x = 0; + ], [ + x++; + ], [ + AS_IF([test x"$enable_shared" != x"no"], [ + old_OBJCFLAGS="$OBJCFLAGS" + OBJCFLAGS="$OBJCFLAGS -fPIC" + AC_TRY_COMPILE([ + static _Thread_local int x = 0; + ], [ + x++; + ], [ + AC_MSG_RESULT(yes) + AC_DEFINE(OF_HAVE__THREAD_LOCAL, 1, + [Whether _Thread_local works]) + ], [ + AC_MSG_RESULT(no) + ]) + OBJCFLAGS="$old_OBJCFLAGS" + ], [ + AC_MSG_RESULT(yes) + AC_DEFINE(OF_HAVE__THREAD_LOCAL, 1, + [Whether _Thread_local works]) + ]) + ], [ + AC_MSG_RESULT(no) + ]) + AC_MSG_CHECKING(whether __thread works) AC_TRY_LINK([ /* It seems __thread is buggy with GCC 4.1 */ #if __GNUC__ == 4 && __GNUC_MINOR__ < 2 # error buggy @@ -570,19 +605,19 @@ __thread int x = 0; ], [ x++; ], [ AC_MSG_RESULT(yes) - AC_DEFINE(OF_HAVE_COMPILER_TLS, 1, + AC_DEFINE(OF_HAVE___THREAD, 1, [Whether __thread works]) ], [ AC_MSG_RESULT(no) ]) OBJCFLAGS="$old_OBJCFLAGS" ], [ AC_MSG_RESULT(yes) - AC_DEFINE(OF_HAVE_COMPILER_TLS, 1, + AC_DEFINE(OF_HAVE___THREAD, 1, [Whether __thread works]) ]) ], [ AC_MSG_RESULT(no) ]) Index: src/OFAutoreleasePool.m ================================================================== --- src/OFAutoreleasePool.m +++ src/OFAutoreleasePool.m @@ -29,11 +29,11 @@ #import "autorelease.h" #define MAX_CACHE_SIZE 0x20 #if defined(OF_HAVE_COMPILER_TLS) -static __thread OFAutoreleasePool **cache = NULL; +static thread_local OFAutoreleasePool **cache = NULL; #elif defined(OF_HAVE_THREADS) static of_tlskey_t cacheKey; #else static OFAutoreleasePool **cache = NULL; #endif Index: src/OFObject.h ================================================================== --- src/OFObject.h +++ src/OFObject.h @@ -44,10 +44,22 @@ #if defined(__GNUC__) # define restrict __restrict__ #elif __STDC_VERSION__ < 199901L # define restrict #endif + +#if defined(OF_HAVE__THREAD_LOCAL) +# define OF_HAVE_COMPILER_TLS +# ifdef OF_HAVE_THREADS_H +# include +# else +# define thread_local _Thread_local +# endif +#elif defined(OF_HAVE___THREAD) +# define OF_HAVE_COMPILER_TLS +# define thread_local __thread +#endif #ifndef __has_feature # define __has_feature(x) 0 #endif Index: src/autorelease.m ================================================================== --- src/autorelease.m +++ src/autorelease.m @@ -28,13 +28,13 @@ #import "macros.h" #import "autorelease.h" #if defined(OF_HAVE_COMPILER_TLS) -static __thread id *objects = NULL; -static __thread id *top = NULL; -static __thread size_t size = 0; +static thread_local id *objects = NULL; +static thread_local id *top = NULL; +static thread_local size_t size = 0; #elif defined(OF_HAVE_THREADS) static of_tlskey_t objectsKey, topKey, sizeKey; #else static id *objects = NULL; static id *top = NULL; Index: src/objfw-defs.h.in ================================================================== --- src/objfw-defs.h.in +++ src/objfw-defs.h.in @@ -6,11 +6,10 @@ #undef OF_HAVE_BUILTIN_BSWAP16 #undef OF_HAVE_BUILTIN_BSWAP32 #undef OF_HAVE_BUILTIN_BSWAP64 #undef OF_HAVE_CHMOD #undef OF_HAVE_CHOWN -#undef OF_HAVE_COMPILER_TLS #undef OF_HAVE_FILES #undef OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR #undef OF_HAVE_GCC_ATOMIC_OPS #undef OF_HAVE_LINK #undef OF_HAVE_MAX_ALIGN_T @@ -26,9 +25,11 @@ #undef OF_HAVE_SCHED_YIELD #undef OF_HAVE_SOCKETS #undef OF_HAVE_SYMLINK #undef OF_HAVE_SYS_SOCKET_H #undef OF_HAVE_THREADS +#undef OF_HAVE___THREAD +#undef OF_HAVE__THREAD_LOCAL #undef OF_NINTENDO_DS #undef OF_OBJFW_RUNTIME #undef OF_UNIVERSAL #undef SIZE_MAX