chiark / gitweb /
use thread_id, if available, in nlopt_srand_time to ensure that different threads...
authorstevenj <stevenj@alum.mit.edu>
Thu, 3 Jun 2010 22:55:50 +0000 (18:55 -0400)
committerstevenj <stevenj@alum.mit.edu>
Thu, 3 Jun 2010 22:55:50 +0000 (18:55 -0400)
darcs-hash:20100603225550-c8de0-963cf98deef902085d264eb09e4067f0e8e32742.gz

api/general.c
configure.ac

index c7f6a54c4eb665da4bac551cc2073b27d892200c..5d300ea19ee44e313540502f7d9dcd99563c1bb0 100644 (file)
@@ -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 <windows.h>
+#  define my_gettid GetCurrentThreadId
+#elif defined(HAVE_GETTID_SYSCALL)
+#  include <unistd.h>
+#  include <sys/syscall.h>
+#  define my_gettid() syscall(SYS_gettid)
+#elif defined(HAVE_GETPID)
+#  include <sys/types.h>
+#  include <unistd.h>
+#  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() {
index b53f5ed5a651dfb32332a1daa7b267ed36019de2..760e941e6cd8ab59e3f952967280106c3c4a25b0 100644 (file)
@@ -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 <unistd.h>
+#include <sys/syscall.h>
+], [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 <math.h>
-], 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 <math.h>
-], 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