]> git.cworth.org Git - apitrace/commitdiff
os: Fallback to OS TLS when compiler does not support it (issue #120).
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Tue, 23 Apr 2013 08:46:26 +0000 (09:46 +0100)
committerJosé Fonseca <jfonseca@vmware.com>
Tue, 23 Apr 2013 13:05:59 +0000 (14:05 +0100)
CMakeLists.txt
common/os_thread.hpp
common/trace_writer_local.cpp
retrace/glretrace_ws.cpp
wrappers/gltrace_state.cpp

index 9394282c591062bdc931a054a3c01b6a4d3941d4..aa96903533e21fb8f3a142fc4d84401807ce271c 100644 (file)
@@ -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)
index da6e106487e5e7c4505a2ae3efb05cc88e36af71..fb340e162c2401e4bc019326b9f44d413acab544 100644 (file)
 #endif
 
 
+/*
+ * These features are not supported on Windows XP
+ */
+#define USE_WIN32_DECLSPEC_THREAD 0
 #define USE_WIN32_CONDITION_VARIABLES 0
 
 
  * - 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
 
 
index feb5c912dfd06b2a61f2a003f9f9759bc23255e0..757e9c0fc328ce99dc94d9c7e33beaba1e49d158 100644 (file)
@@ -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<uintptr_t>(static_cast<void *>(thread_num));
     if (!this_thread_num) {
-        this_thread_num = thread_num = next_thread_num++;
+        this_thread_num = next_thread_num++;
+        thread_num = reinterpret_cast<void *>(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);
 }
 
index ac03fcc97de6d56390f807a3b1661e6571d489c1..94a0d931ad008ad886ee08d55c4285f1b6312841 100644 (file)
@@ -131,7 +131,7 @@ Context::~Context()
 }
 
 
-static thread_specific Context *
+static OS_THREAD_SPECIFIC_PTR(Context)
 currentContextPtr;
 
 
index 031b5cceb431155a480eebc1d0dd2dd84fde7a8c..60eada60d49e4014def29e518526ba79a9cfe994 100644 (file)
@@ -57,7 +57,7 @@ public:
     }
 };
 
-static thread_specific ThreadState *thread_state;
+static OS_THREAD_SPECIFIC_PTR(ThreadState) thread_state;
 
 static ThreadState *get_ts(void)
 {