From ce71d94434657c03094e87267de7b1b405bd7dbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Tue, 29 May 2018 14:10:32 +0200 Subject: [PATCH] Make the xoshiro128plusplus thread-safe --- config.h.in | 12 ++ configure | 201 +++++++++++++++++------- configure.in | 39 +++++ lib/isc/include/isc/platform.h.in | 8 + lib/isc/random.c | 1 + lib/isc/win32/include/isc/platform.h.in | 7 + lib/isc/xoshiro128starstar.c | 32 ++++ 7 files changed, 246 insertions(+), 54 deletions(-) diff --git a/config.h.in b/config.h.in index 69e3c1fd31..1d149f7afd 100644 --- a/config.h.in +++ b/config.h.in @@ -521,6 +521,15 @@ int sigwait(const unsigned int *set, int *sig); /* Define to 1 if you have the header file. */ #undef HAVE_SYS_UN_H +/* Define to 1 if you have the header file. */ +#undef HAVE_THREADS_H + +/* Define if thread_local keyword is available */ +#undef HAVE_THREAD_LOCAL + +/* Define if Thread-Local Storage is available */ +#undef HAVE_TLS + /* Define if running under Compaq TruCluster */ #undef HAVE_TRUCLUSTER @@ -533,6 +542,9 @@ int sigwait(const unsigned int *set, int *sig); /* Define if zlib was found */ #undef HAVE_ZLIB +/* Define if __thread keyword is available */ +#undef HAVE___THREAD + /* Use HMAC-SHA1 for Client Cookie generation */ #undef HMAC_SHA1_CC diff --git a/configure b/configure index 28840a8a9c..539abdf5c7 100755 --- a/configure +++ b/configure @@ -2146,60 +2146,6 @@ $as_echo "$ac_res" >&6; } } # ac_fn_c_check_func -# ac_fn_c_check_type LINENO TYPE VAR INCLUDES -# ------------------------------------------- -# Tests whether TYPE exists after having included INCLUDES, setting cache -# variable VAR accordingly. -ac_fn_c_check_type () -{ - as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5 -$as_echo_n "checking for $2... " >&6; } -if eval \${$3+:} false; then : - $as_echo_n "(cached) " >&6 -else - eval "$3=no" - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -$4 -int -main () -{ -if (sizeof ($2)) - return 0; - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -$4 -int -main () -{ -if (sizeof (($2))) - return 0; - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - -else - eval "$3=yes" -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext -fi -eval ac_res=\$$3 - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 -$as_echo "$ac_res" >&6; } - eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno - -} # ac_fn_c_check_type - # ac_fn_c_check_header_mongrel LINENO HEADER VAR INCLUDES # ------------------------------------------------------- # Tests whether HEADER exists, giving a warning if it cannot be compiled using @@ -2291,6 +2237,60 @@ fi } # ac_fn_c_check_header_mongrel +# ac_fn_c_check_type LINENO TYPE VAR INCLUDES +# ------------------------------------------- +# Tests whether TYPE exists after having included INCLUDES, setting cache +# variable VAR accordingly. +ac_fn_c_check_type () +{ + as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5 +$as_echo_n "checking for $2... " >&6; } +if eval \${$3+:} false; then : + $as_echo_n "(cached) " >&6 +else + eval "$3=no" + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +$4 +int +main () +{ +if (sizeof ($2)) + return 0; + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +$4 +int +main () +{ +if (sizeof (($2))) + return 0; + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + +else + eval "$3=yes" +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext +fi +eval ac_res=\$$3 + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 +$as_echo "$ac_res" >&6; } + eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno + +} # ac_fn_c_check_type + # ac_fn_c_compute_int LINENO EXPR VAR INCLUDES # -------------------------------------------- # Tries to find the compile-time value of EXPR in a program that includes @@ -13279,6 +13279,99 @@ fi done +# +# Check for thread local storage +# +for ac_header in threads.h +do : + ac_fn_c_check_header_mongrel "$LINENO" "threads.h" "ac_cv_header_threads_h" "$ac_includes_default" +if test "x$ac_cv_header_threads_h" = xyes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_THREADS_H 1 +_ACEOF + + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for C11 Thread-Local Storage using thread_local" >&5 +$as_echo_n "checking for C11 Thread-Local Storage using thread_local... " >&6; } + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + + #include + +int +main () +{ + + static thread_local int tls = 0; + return (tls); + + ; + return 0; +} + +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 +$as_echo "yes" >&6; } + +$as_echo "#define HAVE_THREAD_LOCAL 1" >>confdefs.h + + +$as_echo "#define HAVE_TLS 1" >>confdefs.h + + +else + + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } + +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext + +else + + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for Thread-Local Storage using __thread" >&5 +$as_echo_n "checking for Thread-Local Storage using __thread... " >&6; } + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + + +int +main () +{ + + static __thread int tls = 0; + return (tls); + + ; + return 0; +} + +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 +$as_echo "yes" >&6; } + +$as_echo "#define HAVE___THREAD 1" >>confdefs.h + + +$as_echo "#define HAVE_TLS 1" >>confdefs.h + + +else + + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } + +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext + +fi + +done + + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for an ANSI C-conforming const" >&5 $as_echo_n "checking for an ANSI C-conforming const... " >&6; } if ${ac_cv_c_const+:} false; then : diff --git a/configure.in b/configure.in index 9594c01630..627ddec310 100644 --- a/configure.in +++ b/configure.in @@ -486,6 +486,45 @@ AC_CHECK_HEADERS(fcntl.h regex.h sys/time.h unistd.h sys/mman.h sys/sockio.h sys #endif ]) +# +# Check for thread local storage +# +AC_CHECK_HEADERS([threads.h], + [ + AC_MSG_CHECKING([for C11 Thread-Local Storage using thread_local]) + AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [ + #include + ],[ + static thread_local int tls = 0; + return (tls); + ]) + ],[ + AC_MSG_RESULT([yes]) + 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_CHECKING([for Thread-Local Storage using __thread]) + AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [ + ],[ + static __thread int tls = 0; + return (tls); + ]) + ],[ + AC_MSG_RESULT([yes]) + 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_C_CONST AC_C_INLINE AC_C_VOLATILE diff --git a/lib/isc/include/isc/platform.h.in b/lib/isc/include/isc/platform.h.in index 9a74ee64d5..b8c67624c8 100644 --- a/lib/isc/include/isc/platform.h.in +++ b/lib/isc/include/isc/platform.h.in @@ -18,6 +18,14 @@ ***** Platform-dependent defines. *****/ +/*** + *** Thread-Local Storage + ***/ + +#ifdef HAVE___THREAD +#define thread_local __thread +#endif + /*** *** Network. ***/ diff --git a/lib/isc/random.c b/lib/isc/random.c index d88431d6d0..490f0d95ad 100644 --- a/lib/isc/random.c +++ b/lib/isc/random.c @@ -35,6 +35,7 @@ #include #include +#include #include #include #include diff --git a/lib/isc/win32/include/isc/platform.h.in b/lib/isc/win32/include/isc/platform.h.in index d7f94a6ff0..3b60b01e11 100644 --- a/lib/isc/win32/include/isc/platform.h.in +++ b/lib/isc/win32/include/isc/platform.h.in @@ -18,6 +18,13 @@ #define ISC_PLATFORM_USETHREADS 1 +/* + * Thread-Local Storage + */ +#ifndef thread_local +#define thread_local +#endif + /* * Some compatibility cludges */ diff --git a/lib/isc/xoshiro128starstar.c b/lib/isc/xoshiro128starstar.c index f12f5cf52f..137ffa1ae2 100644 --- a/lib/isc/xoshiro128starstar.c +++ b/lib/isc/xoshiro128starstar.c @@ -33,6 +33,34 @@ * The state must be seeded so that it is not everywhere zero. */ +#if defined(_WIN32) || defined(_WIN64) +#include +static volatile HANDLE _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() { \ + if (!_mutex) { \ + HANDLE p = CreateMutex(NULL, FALSE, NULL); \ + if (InterlockedCompareExchangePointer((void **)&_mutex, (void *)p, NULL)) \ + CloseHandle(p); \ + } \ + WaitForSingleObject(_mutex, INFINITE); \ +} + +#define _UNLOCK() ReleaseMutex(_mutex) + +#else /* defined(_WIN32) || defined(_WIN64) */ + +#include +static pthread_mutex_t _mutex = PTHREAD_MUTEX_INITIALIZER; +#define _LOCK() pthread_mutex_lock(&_mutex) +#define _UNLOCK() pthread_mutex_unlock(&_mutex) +#endif /* defined(_WIN32) || defined(_WIN64) */ + static inline uint32_t rotl(const uint32_t x, int k) { return (x << k) | (x >> (32 - k)); } @@ -41,6 +69,8 @@ static uint32_t seed[4]; static inline uint32_t next(void) { + _LOCK(); + const uint32_t result_starstar = rotl(seed[0] * 5, 7) * 9; const uint32_t t = seed[1] << 9; @@ -54,5 +84,7 @@ next(void) { seed[3] = rotl(seed[3], 11); + _UNLOCK(); + return (result_starstar); }