summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/library.c22
-rw-r--r--configure.ac12
2 files changed, 34 insertions, 0 deletions
diff --git a/common/library.c b/common/library.c
index cde8384..c1275f3 100644
--- a/common/library.c
+++ b/common/library.c
@@ -104,6 +104,21 @@ uninit_common (void)
#ifdef OS_UNIX
+#ifdef P11_TLS_KEYWORD
+static p11_local *
+_p11_library_get_thread_local (void)
+{
+ static P11_TLS_KEYWORD p11_local local;
+ static P11_TLS_KEYWORD bool local_initialized = false;
+
+ if (!local_initialized) {
+ memset (&local, 0, sizeof (p11_local));
+ local_initialized = true;
+ }
+
+ return &local;
+}
+#else
static pthread_key_t thread_local = 0;
static p11_local *
@@ -121,6 +136,7 @@ _p11_library_get_thread_local (void)
return local;
}
+#endif
static void
count_forks (void)
@@ -136,7 +152,9 @@ p11_library_init_impl (void)
p11_debug ("initializing library");
P11_RECURSIVE_MUTEX_INIT (p11_library_mutex);
P11_RECURSIVE_MUTEX_INIT (p11_virtual_mutex);
+#ifndef P11_TLS_KEYWORD
pthread_key_create (&thread_local, free);
+#endif
p11_message_storage = thread_local_message;
#ifdef HAVE_STRERROR_L
p11_message_locale = newlocale (LC_ALL_MASK, "POSIX", (locale_t) 0);
@@ -156,15 +174,19 @@ p11_library_uninit (void)
{
uninit_common ();
+#ifndef P11_TLS_KEYWORD
/* Some cleanup to pacify valgrind */
free (pthread_getspecific (thread_local));
pthread_setspecific (thread_local, NULL);
+#endif
#ifdef HAVE_STRERROR_L
freelocale (p11_message_locale);
#endif
p11_message_storage = dont_store_message;
+#ifndef P11_TLS_KEYWORD
pthread_key_delete (thread_local);
+#endif
p11_mutex_uninit (&p11_virtual_mutex);
p11_mutex_uninit (&p11_library_mutex);
}
diff --git a/configure.ac b/configure.ac
index b35da75..6ee6404 100644
--- a/configure.ac
+++ b/configure.ac
@@ -139,6 +139,18 @@ if test "$os_unix" = "yes"; then
fi
])
+ AC_CACHE_CHECK([for thread-local storage class],
+ [ac_cv_tls_keyword],
+ [ac_cv_tls_keyword=
+ for keyword in _Thread_local __thread; do
+ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <stdlib.h>]],
+ [[static ]$keyword[ foo;]])],
+ [ac_cv_tls_keyword=$keyword])
+ done])
+ if test -n "$ac_cv_tls_keyword"; then
+ AC_DEFINE_UNQUOTED([P11_TLS_KEYWORD], [$ac_cv_tls_keyword], [the compiler keyword to define thread-local storage])
+ fi
+
# Required functions
AC_CHECK_FUNCS([gmtime_r],
[AC_DEFINE([HAVE_GMTIME_R], 1, [Whether gmtime_r() is available])],