Commit ce71d944 authored by Ondřej Surý's avatar Ondřej Surý

Make the xoshiro128plusplus thread-safe

parent 99ba29bc
......@@ -521,6 +521,15 @@ int sigwait(const unsigned int *set, int *sig);
/* Define to 1 if you have the <sys/un.h> header file. */
#undef HAVE_SYS_UN_H
/* Define to 1 if you have the <threads.h> 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
......
......@@ -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 <threads.h>
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 :
......
......@@ -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 <threads.h>
],[
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
......
......@@ -18,6 +18,14 @@
***** Platform-dependent defines.
*****/
/***
*** Thread-Local Storage
***/
#ifdef HAVE___THREAD
#define thread_local __thread
#endif
/***
*** Network.
***/
......
......@@ -35,6 +35,7 @@
#include <string.h>
#include <unistd.h>
#include <isc/platform.h>
#include <isc/random.h>
#include <isc/result.h>
#include <isc/types.h>
......
......@@ -18,6 +18,13 @@
#define ISC_PLATFORM_USETHREADS 1
/*
* Thread-Local Storage
*/
#ifndef thread_local
#define thread_local
#endif
/*
* Some compatibility cludges
*/
......
......@@ -33,6 +33,34 @@
* The state must be seeded so that it is not everywhere zero.
*/
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
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 <pthread.h>
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);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment