From 2ea1b5c3dd7494965738b4515f2e80b08b8abffa Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Tue, 23 Apr 2013 09:46:26 +0100 Subject: [PATCH] os: Fallback to OS TLS when compiler does not support it (issue #120). --- CMakeLists.txt | 15 --------------- common/os_thread.hpp | 20 +++++++++++++------- common/trace_writer_local.cpp | 17 +++++++++++------ retrace/glretrace_ws.cpp | 2 +- wrappers/gltrace_state.cpp | 2 +- 5 files changed, 26 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9394282..aa96903 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,21 +34,6 @@ set (ENABLE_EGL true CACHE BOOL "Enable EGL support.") ############################################################################## # Find dependencies -# Ensure __thread is support -if (NOT MSVC) - include (CheckCXXSourceCompiles) - check_cxx_source_compiles("__thread int i; int main() { return 0; }" HAVE_COMPILER_TLS) - if (NOT HAVE_COMPILER_TLS) - if (APPLE) - message (FATAL_ERROR "C++ compiler does not support __thread keyword. Please install XCode 4.5 or higher.") - else (MINGW32) - message (FATAL_ERROR "C++ compiler does not support __thread keyword. Please use MinGW g++ version 4.4 or higher") - else () - message (FATAL_ERROR "C++ compiler does not support __thread keyword.") - endif () - endif () -endif () - set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) set (CMAKE_USE_PYTHON_VERSION 2.7 2.6) diff --git a/common/os_thread.hpp b/common/os_thread.hpp index da6e106..fb340e1 100644 --- a/common/os_thread.hpp +++ b/common/os_thread.hpp @@ -40,6 +40,10 @@ #endif +/* + * These features are not supported on Windows XP + */ +#define USE_WIN32_DECLSPEC_THREAD 0 #define USE_WIN32_CONDITION_VARIABLES 0 @@ -50,13 +54,15 @@ * - http://gcc.gnu.org/onlinedocs/gcc-4.6.3/gcc/Thread_002dLocal.html * - http://msdn.microsoft.com/en-us/library/9w1sdazb.aspx */ -#if defined(_MSC_VER) -# define thread_specific __declspec(thread) -#elif defined(__GNUC__) -# define thread_specific __thread -#else -# define thread_specific -# error "Unsupported compiler" +#if !defined(_WIN32) || USE_WIN32_DECLSPEC_THREAD +# if defined(_MSC_VER) +# define OS_THREAD_SPECIFIC_PTR(_type) __declspec(thread) _type * +# elif defined(__GNUC__) +# define OS_THREAD_SPECIFIC_PTR(_type) __thread _type * +# endif +#endif +#if !defined(OS_THREAD_SPECIFIC_PTR) +# define OS_THREAD_SPECIFIC_PTR(_type) os::thread_specific_ptr< _type > #endif diff --git a/common/trace_writer_local.cpp b/common/trace_writer_local.cpp index feb5c91..757e9c0 100644 --- a/common/trace_writer_local.cpp +++ b/common/trace_writer_local.cpp @@ -128,8 +128,10 @@ LocalWriter::open(void) { #endif } -static unsigned next_thread_num = 1; -static thread_specific unsigned thread_num = 0; +static uintptr_t next_thread_num = 1; + +static OS_THREAD_SPECIFIC_PTR(void) +thread_num; unsigned LocalWriter::beginEnter(const FunctionSig *sig) { mutex.lock(); @@ -139,13 +141,16 @@ unsigned LocalWriter::beginEnter(const FunctionSig *sig) { open(); } - unsigned this_thread_num = thread_num; + // Although thread_num is a void *, we actually use it as a uintptr_t + uintptr_t this_thread_num = + reinterpret_cast(static_cast(thread_num)); if (!this_thread_num) { - this_thread_num = thread_num = next_thread_num++; + this_thread_num = next_thread_num++; + thread_num = reinterpret_cast(this_thread_num); } - assert(thread_num > 0); - unsigned thread_id = thread_num - 1; + assert(this_thread_num); + unsigned thread_id = this_thread_num - 1; return Writer::beginEnter(sig, thread_id); } diff --git a/retrace/glretrace_ws.cpp b/retrace/glretrace_ws.cpp index ac03fcc..94a0d93 100644 --- a/retrace/glretrace_ws.cpp +++ b/retrace/glretrace_ws.cpp @@ -131,7 +131,7 @@ Context::~Context() } -static thread_specific Context * +static OS_THREAD_SPECIFIC_PTR(Context) currentContextPtr; diff --git a/wrappers/gltrace_state.cpp b/wrappers/gltrace_state.cpp index 031b5cc..60eada6 100644 --- a/wrappers/gltrace_state.cpp +++ b/wrappers/gltrace_state.cpp @@ -57,7 +57,7 @@ public: } }; -static thread_specific ThreadState *thread_state; +static OS_THREAD_SPECIFIC_PTR(ThreadState) thread_state; static ThreadState *get_ts(void) { -- 2.43.0