From aaf523fb6566150c8c3203b72790cf580e30094e Mon Sep 17 00:00:00 2001 From: stevenj Date: Thu, 3 Jun 2010 18:55:50 -0400 Subject: [PATCH] use thread_id, if available, in nlopt_srand_time to ensure that different threads get different seed even if they are called simultaneously darcs-hash:20100603225550-c8de0-963cf98deef902085d264eb09e4067f0e8e32742.gz --- api/general.c | 22 +++++++++++++++++++++- configure.ac | 15 ++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/api/general.c b/api/general.c index c7f6a54..5d300ea 100644 --- a/api/general.c +++ b/api/general.c @@ -100,6 +100,26 @@ const char *nlopt_algorithm_name(nlopt_algorithm a) return nlopt_algorithm_names[a]; } +/*************************************************************************/ +/* get thread id, if possible, for use in nlopt_srand_time to ensure that + different threads have a different default seed even if they are called + simultaneously */ + +#if defined(_WIN32) || defined(__WIN32__) +# include +# define my_gettid GetCurrentThreadId +#elif defined(HAVE_GETTID_SYSCALL) +# include +# include +# define my_gettid() syscall(SYS_gettid) +#elif defined(HAVE_GETPID) +# include +# include +# define my_gettid getpid +#else +# define my_gettid() (0) +#endif + /*************************************************************************/ static THREADLOCAL int nlopt_srand_called = 0; @@ -109,7 +129,7 @@ void nlopt_srand(unsigned long seed) { } void nlopt_srand_time() { - nlopt_srand(nlopt_time_seed()); + nlopt_srand(nlopt_time_seed() + my_gettid() * 314159); } void nlopt_srand_time_default() { diff --git a/configure.ac b/configure.ac index b53f5ed..760e941 100644 --- a/configure.ac +++ b/configure.ac @@ -51,11 +51,20 @@ AC_CHECK_TYPES(uint32_t, [], [], [$ac_includes_default dnl Checks for libraries and functions AC_CHECK_LIB(m, sin) -AC_CHECK_FUNCS([BSDgettimeofday gettimeofday time qsort_r]) +AC_CHECK_FUNCS([BSDgettimeofday gettimeofday time qsort_r getpid]) + +AC_MSG_CHECKING([for gettid syscall]) +AC_TRY_LINK([#include +#include +], [syscall(SYS_gettid);], [ok=yes], [ok=no]) +if test "$ok" = "yes"; then + AC_DEFINE(HAVE_GETTID_SYSCALL,1,[Define if syscall(SYS_gettid) available.]) +fi +AC_MSG_RESULT(${ok}) AC_MSG_CHECKING([for isnan]) AC_TRY_LINK([#include -], if (!isnan(3.14159)) isnan(2.7183);, ok=yes, ok=no) +], [if (!isnan(3.14159)) isnan(2.7183);], ok=yes, ok=no) if test "$ok" = "yes"; then AC_DEFINE(HAVE_ISNAN,1,[Define if the isnan() function/macro is available.]) fi @@ -63,7 +72,7 @@ AC_MSG_RESULT(${ok}) AC_MSG_CHECKING([for isinf]) AC_TRY_LINK([#include -], if (!isinf(3.14159)) isinf(2.7183);, ok=yes, ok=no) +], [if (!isinf(3.14159)) isinf(2.7183);], ok=yes, ok=no) if test "$ok" = "yes"; then AC_DEFINE(HAVE_ISINF,1,[Define if the isinf() function/macro is available.]) fi -- 2.30.2