From e7b6ae0111262b0144a8e4bb206e481a24f8c0e9 Mon Sep 17 00:00:00 2001
From: Jeremy Huddleston <jeremyhu@apple.com>
Date: Sat, 11 Feb 2012 00:44:38 -0800
Subject: [PATCH 2/2] Expand TLS support beyond __thread to __declspec(thread)

Also prefer initial-exec if available.  This code was pretty much
coppied from a similar commit that I made to xorg-server in April.

cf: xorg/xserver: bb4d145bd25e2aee988b100ecf1105ea3b6a40b8

Signed-off-by: Jeremy Huddleston <jeremyhu@apple.com>
---
 configure.ac             |   46 ++++++++++++++++++++++++++++------------------
 pixman/pixman-compiler.h |    4 ++--
 2 files changed, 30 insertions(+), 20 deletions(-)

diff --git a/configure.ac b/configure.ac
index 54c8fb9..52afb0d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -733,26 +733,36 @@ fi
 dnl =====================================
 dnl Thread local storage
 
-support_for__thread=no
-
-AC_MSG_CHECKING(for __thread)
-AC_LINK_IFELSE([AC_LANG_SOURCE([[
-#if defined(__MINGW32__) && !(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
-#error This MinGW version has broken __thread support
-#endif
-#ifdef __OpenBSD__
-#error OpenBSD has broken __thread support
-#endif
-static __thread int x ;
-int main () { x = 123; return x; }
-]])], support_for__thread=yes)
+AC_MSG_CHECKING(for thread local storage (TLS) support)
+AC_CACHE_VAL(ac_cv_tls, [
+    ac_cv_tls=none
+    keywords="__thread __declspec(thread)"
+    for kw in $keywords ; do
+        AC_TRY_COMPILE([int $kw test;], [], ac_cv_tls=$kw)
+    done
+])
+AC_MSG_RESULT($ac_cv_tls)
+
+if test "$ac_cv_tls" != "none"; then
+    AC_MSG_CHECKING(for tls_model attribute support)
+    AC_CACHE_VAL(ac_cv_tls_model, [
+        save_CFLAGS="$CFLAGS"
+        CFLAGS="$CFLAGS $WERROR"
+        AC_TRY_COMPILE([int $ac_cv_tls __attribute__((tls_model("initial-exec"))) test;], [],
+                       ac_cv_tls_model=yes, ac_cv_tls_model=no)
+        CFLAGS="$save_CFLAGS"
+    ])
+    AC_MSG_RESULT($ac_cv_tls_model)
+
+    if test "x$ac_cv_tls_model" = "xyes" ; then
+        pixman_tls=$ac_cv_tls' __attribute__((tls_model("initial-exec")))'
+    else
+        pixman_tls=$ac_cv_tls
+    fi
 
-if test $support_for__thread = yes; then 
-   AC_DEFINE([TOOLCHAIN_SUPPORTS__THREAD],[],[Whether the tool chain supports __thread])
+    AC_DEFINE_UNQUOTED([TLS], $pixman_tls, [The compiler supported TLS storage class, prefering initial-exec if tls_model is supported])
 fi
 
-AC_MSG_RESULT($support_for__thread)
-
 dnl
 dnl posix tls
 dnl
@@ -803,7 +813,7 @@ AC_DEFUN([PIXMAN_CHECK_PTHREAD],[dnl
     fi
 ])
 
-if test $support_for__thread = no; then
+if test $ac_cv_tls != none ; then
     support_for_pthread_setspecific=no
 
     AC_MSG_CHECKING(for pthread_setspecific)
diff --git a/pixman/pixman-compiler.h b/pixman/pixman-compiler.h
index 5b568e1..ffd5172 100644
--- a/pixman/pixman-compiler.h
+++ b/pixman/pixman-compiler.h
@@ -97,10 +97,10 @@
 #   define PIXMAN_GET_THREAD_LOCAL(name)				\
     (&name)
 
-#elif defined(TOOLCHAIN_SUPPORTS__THREAD)
+#elif defined(TLS)
 
 #   define PIXMAN_DEFINE_THREAD_LOCAL(type, name)			\
-    static __thread type name
+    static TLS type name
 #   define PIXMAN_GET_THREAD_LOCAL(name)				\
     (&name)
 
-- 
1.7.9

