]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add and use ISC_THREAD_LOCAL macro
authorOndřej Surý <ondrej@isc.org>
Mon, 2 Dec 2019 10:42:50 +0000 (11:42 +0100)
committerOndřej Surý <ondrej@sury.org>
Tue, 3 Dec 2019 15:27:24 +0000 (16:27 +0100)
The new ISC_THREAD_LOCAL macro unifies usage of platform dependent
Thread Local Storage definition thread_local vs __thread vs
__declspec(thread) to a single macro.

The commit also unifies the required level of support for TLS as for
some parts of the code it was mandatory and for some parts of the code
it wasn't.

configure
configure.ac
lib/isc/hp.c
lib/isc/include/isc/platform.h.in
lib/isc/netmgr/netmgr-int.h
lib/isc/netmgr/netmgr.c
lib/isc/pthreads/include/isc/thread.h
lib/isc/random.c
lib/isc/win32/include/isc/thread.h
lib/isc/xoshiro128starstar.c

index 0d6df8eea01110713c52861b3fe898f1fa19860e..7d1629af0e9f1999f9a1290a6d86147a936a265f 100755 (executable)
--- a/configure
+++ b/configure
@@ -13616,8 +13616,7 @@ $as_echo "#define HAVE_TLS 1" >>confdefs.h
 
 else
 
-                            { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
+                            as_fn_error $? "Thread Local Storage support required, update your toolchain to build BIND 9" "$LINENO" 5
 
 fi
 rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
@@ -13655,8 +13654,7 @@ $as_echo "#define HAVE_TLS 1" >>confdefs.h
 
 else
 
-                            { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
+                            as_fn_error $? "Thread Local Storage support required, update your toolchain to build BIND 9" "$LINENO" 5
 
 fi
 rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
index 623cb277d1477e78e1b14c3439fd045b6d39eb4c..25b0ce89591a7a49203a8ca803c52f808dcc46e3 100644 (file)
@@ -389,7 +389,7 @@ AC_CHECK_HEADERS([threads.h],
                             AC_DEFINE([HAVE_THREAD_LOCAL],[1],[Define if thread_local keyword is available])
                             AC_DEFINE([HAVE_TLS],[1],[Define if Thread-Local Storage is available])
                         ],[
-                            AC_MSG_RESULT([no])
+                            AC_MSG_ERROR([Thread Local Storage support required, update your toolchain to build BIND 9])
                         ])
                 ],[
                     AC_MSG_CHECKING([for Thread-Local Storage using __thread])
@@ -405,7 +405,7 @@ AC_CHECK_HEADERS([threads.h],
                             AC_DEFINE([HAVE___THREAD],[1],[Define if __thread keyword is available])
                             AC_DEFINE([HAVE_TLS],[1],[Define if Thread-Local Storage is available])
                         ],[
-                            AC_MSG_RESULT([no])
+                            AC_MSG_ERROR([Thread Local Storage support required, update your toolchain to build BIND 9])
                         ])
                 ])
 
index 746d13fc0a43cc92e40e5f829c6797be947c1a45..4bdd23c284742fe16bec4e628303d50256e30f8c 100644 (file)
@@ -51,6 +51,7 @@
 #include <isc/string.h>
 #include <isc/mem.h>
 #include <isc/util.h>
+#include <isc/thread.h>
 
 #define HP_MAX_THREADS 128
 #define HP_MAX_HPS 4                   /* This is named 'K' in the HP paper */
 
 static atomic_int_fast32_t tid_v_base = ATOMIC_VAR_INIT(0);
 
-#if defined(HAVE_TLS)
-#if defined(HAVE_THREAD_LOCAL)
-#include <threads.h>
-static thread_local int tid_v = TID_UNKNOWN;
-#elif defined(HAVE___THREAD)
-static __thread int tid_v = TID_UNKNOWN;
-#elif defined(HAVE___DECLSPEC_THREAD)
-static __declspec( thread ) int tid_v = TID_UNKNOWN;
-#else  /* if defined(HAVE_THREAD_LOCAL) */
-#error "Unknown method for defining a TLS variable!"
-#endif /* if defined(HAVE_THREAD_LOCAL) */
-#else  /* if defined(HAVE_TLS) */
-#error "Thread-local storage support is required!"
-#endif /* if defined(HAVE_TLS) */
+ISC_THREAD_LOCAL int tid_v = TID_UNKNOWN;
 
 typedef struct retirelist {
        int                     size;
index 0a16b887391e94d2a5e0bc2aa9ddd7b34e9a8645..68c0103834bcffc0671ac3736f539765927011c4 100644 (file)
  ***** Platform-dependent defines.
  *****/
 
-/***
- *** Thread-Local Storage
- ***/
-
-#ifdef HAVE___THREAD
-#define thread_local __thread
-#endif
-
 /***
  *** Default strerror_r buffer size
  ***/
index b4191ff6efc3557c2f41c4be911601c5a5669a38..3acacc756bca9a8c4db32a3b4e5e14adfc72b9be 100644 (file)
@@ -31,7 +31,6 @@
 #include <isc/util.h>
 
 #define ISC_NETMGR_TID_UNKNOWN -1
-#define ISC_NETMGR_TID_NOTLS -2
 
 /*
  * Single network event loop worker.
index e8d330f008b75974031f4c0ac46cbcbb70ec0a10..902fe2d1ccf60ec3f79891761ee1f8774f7f42c6 100644 (file)
  * request using async_cb.
  */
 
-#if defined(HAVE_TLS)
-#if defined(HAVE_THREAD_LOCAL)
-#include <threads.h>
-static thread_local int isc__nm_tid_v = ISC_NETMGR_TID_UNKNOWN;
-#elif defined(HAVE___THREAD)
-static __thread int isc__nm_tid_v = ISC_NETMGR_TID_UNKNOWN;
-#elif defined(HAVE___DECLSPEC_THREAD)
-static __declspec( thread ) int isc__nm_tid_v = ISC_NETMGR_TID_UNKNOWN;
-#else  /* if defined(HAVE_THREAD_LOCAL) */
-#error "Unknown method for defining a TLS variable!"
-#endif /* if defined(HAVE_THREAD_LOCAL) */
-#else  /* if defined(HAVE_TLS) */
-static int isc__nm_tid_v = ISC_NETMGR_TID_NOTLS;
-#endif /* if defined(HAVE_TLS) */
+ISC_THREAD_LOCAL int isc__nm_tid_v = ISC_NETMGR_TID_UNKNOWN;
 
 static void
 nmsocket_maybe_destroy(isc_nmsocket_t *sock);
index 63194d3b043b3d2f0b70f590c169d5be96f2fbd6..ac50cce0fd4193a0f00615d416d6b83e1d00833c 100644 (file)
@@ -58,6 +58,23 @@ isc_thread_setaffinity(int cpu);
 #define isc_thread_key_setspecific pthread_setspecific
 #define isc_thread_key_delete pthread_key_delete
 
+/***
+ *** Thread-Local Storage
+ ***/
+
+#if defined(HAVE_TLS)
+#if defined(HAVE_THREAD_LOCAL)
+#include <threads.h>
+#define ISC_THREAD_LOCAL static thread_local
+#elif defined(HAVE___THREAD)
+#define ISC_THREAD_LOCAL static __thread
+#else  /* if defined(HAVE_THREAD_LOCAL) */
+#error "Unknown method for defining a TLS variable!"
+#endif /* if defined(HAVE_THREAD_LOCAL) */
+#else  /* if defined(HAVE_TLS) */
+#error "Thread-local storage support is required!"
+#endif /* if defined(HAVE_TLS) */
+
 ISC_LANG_ENDDECLS
 
 #endif /* ISC_THREAD_H */
index 79fbc7c7aa16606fc728f0f24336ef5124b0dcfe..681478f6b07fa2af3bfce6dc391bf4a5fb42d883 100644 (file)
@@ -36,6 +36,7 @@
 #include <isc/platform.h>
 #include <isc/random.h>
 #include <isc/result.h>
+#include <isc/thread.h>
 #include <isc/types.h>
 #include <isc/util.h>
 
  */
 #include "xoshiro128starstar.c"
 
-#if defined(HAVE_TLS)
-#if defined(HAVE_THREAD_LOCAL)
-#include <threads.h>
-static thread_local isc_once_t isc_random_once = ISC_ONCE_INIT;
-#elif defined(HAVE___THREAD)
-static __thread isc_once_t isc_random_once = ISC_ONCE_INIT;
-#elif defined(HAVE___DECLSPEC_THREAD)
-static __declspec( thread ) isc_once_t isc_random_once = ISC_ONCE_INIT;
-#else
-#error "Unknown method for defining a TLS variable!"
-#endif
-#else
-static isc_once_t isc_random_once = ISC_ONCE_INIT;
-#endif
+ISC_THREAD_LOCAL isc_once_t isc_random_once = ISC_ONCE_INIT;
 
 static void
 isc_random_initialize(void) {
index f091d0623c47d5e8d5e8a1715d7a0ac405fe02a2..05225f901676d722ca400149d7739af65e371e3a 100644 (file)
@@ -96,6 +96,12 @@ isc_thread_key_setspecific(isc_thread_key_t key, void *value);
 
 #define isc_thread_yield() Sleep(0)
 
+#if HAVE___DECLSPEC_THREAD
+#define ISC_THREAD_LOCAL static __declspec( thread )
+#else
+#error "Thread-local storage support is required!"
+#endif
+
 ISC_LANG_ENDDECLS
 
 #endif /* ISC_THREAD_H */
index abf1ea8455957301a39dfefc209ee39012b0a902..589ed54e60b420d836e81cb1bb2bdb671247a75d 100644 (file)
@@ -22,6 +22,8 @@
 
 #include <inttypes.h>
 
+#include <isc/thread.h>
+
 /*
  * This is xoshiro128** 1.0, our 32-bit all-purpose, rock-solid generator.
  * It has excellent (sub-ns) speed, a state size (128 bits) that is large
  *
  * The state must be seeded so that it is not everywhere zero.
  */
-#if defined(HAVE_TLS)
-#define _LOCK() {};
-#define _UNLOCK() {};
-
-#if defined(HAVE_THREAD_LOCAL)
-#include <threads.h>
-static thread_local uint32_t seed[4];
-#elif defined(HAVE___THREAD)
-static __thread uint32_t seed[4];
-#elif defined(HAVE___DECLSPEC_THREAD)
-static __declspec( thread ) uint32_t seed[4];
-#else
-#error "Unknown method for defining a TLS variable!"
-#endif
-
-#else
-#if defined(_WIN32) || defined(_WIN64)
-#include <windows.h>
-static volatile void *_mutex = NULL;
-
-/*
- * Initialize the mutex on the first lock attempt. On collision, each thread
- * will attempt to allocate a mutex and compare-and-swap it into place as the
- * global mutex. On failure to swap in the global mutex, the mutex is closed.
- */
-#define _LOCK() \
-       do {                                                            \
-               if (!_mutex) {                                          \
-                       void *p = CreateMutex(NULL, FALSE, NULL);       \
-                       if (InterlockedCompareExchangePointer           \
-                           ((void **)&_mutex, (void *)p, NULL)) {      \
-                               CloseHandle(p);                         \
-                       }                                               \
-               }                                                       \
-               WaitForSingleObject(_mutex, INFINITE);                  \
-       } while (0)
-
-#define _UNLOCK() ReleaseMutex(_mutex)
-
-#else /* defined(_WIN32) || defined(_WIN64) */
-
-#include <pthread.h>
-static pthread_mutex_t _mutex = PTHREAD_MUTEX_INITIALIZER;
-#define _LOCK()   RUNTIME_CHECK(pthread_mutex_lock(&_mutex)==0)
-#define _UNLOCK() RUNTIME_CHECK(pthread_mutex_unlock(&_mutex)==0)
-#endif /* defined(_WIN32) || defined(_WIN64) */
-
-static uint32_t seed[4];
-
-#endif /* defined(HAVE_TLS) */
+ISC_THREAD_LOCAL uint32_t seed[4] = { 0 };
 
 static inline uint32_t rotl(const uint32_t x, int k) {
        return (x << k) | (x >> (32 - k));
@@ -91,8 +44,6 @@ static inline uint32_t
 next(void) {
        uint32_t result_starstar, t;
 
-       _LOCK();
-
        result_starstar = rotl(seed[0] * 5, 7) * 9;
        t = seed[1] << 9;
 
@@ -105,7 +56,5 @@ next(void) {
 
        seed[3] = rotl(seed[3], 11);
 
-       _UNLOCK();
-
        return (result_starstar);
 }