summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoridontgetoutmuch <dominic@steinitz.org>2019-07-01 11:28:09 +0100
committerGitHub <noreply@github.com>2019-07-01 11:28:09 +0100
commitd844a145f2e8808c9f75cd99c673d5f5c8960bf2 (patch)
treebd793f3556be06624df1043d64f5562be20fcc5f
parent03f114e2d849bbffac89e535c7736ebe7e4d1762 (diff)
parente6914acb75514fbb2bd2e736c4157e38abdb1ec0 (diff)
Merge pull request #301 from nh2/no-random_r-flagHEADmaster
Allow disabling random_r() usage manually.
-rw-r--r--packages/base/hmatrix.cabal8
-rw-r--r--packages/base/src/Internal/C/vector-aux.c33
2 files changed, 31 insertions, 10 deletions
diff --git a/packages/base/hmatrix.cabal b/packages/base/hmatrix.cabal
index 76e3825..4dc62e5 100644
--- a/packages/base/hmatrix.cabal
+++ b/packages/base/hmatrix.cabal
@@ -36,6 +36,11 @@ flag disable-default-paths
36 default: False 36 default: False
37 manual: True 37 manual: True
38 38
39flag no-random_r
40 description: When enabled, don't depend on the random_r() C function.
41 default: False
42 manual: True
43
39library 44library
40 45
41 Build-Depends: base >= 4.9 && < 5, 46 Build-Depends: base >= 4.9 && < 5,
@@ -99,6 +104,9 @@ library
99 cc-options: -msse2 104 cc-options: -msse2
100 105
101 106
107 if flag(no-random_r)
108 cc-options: -DNO_RANDOM_R
109
102 if os(OSX) 110 if os(OSX)
103 if flag(openblas) 111 if flag(openblas)
104 if !flag(disable-default-paths) 112 if !flag(disable-default-paths)
diff --git a/packages/base/src/Internal/C/vector-aux.c b/packages/base/src/Internal/C/vector-aux.c
index dcd6c0b..73455a5 100644
--- a/packages/base/src/Internal/C/vector-aux.c
+++ b/packages/base/src/Internal/C/vector-aux.c
@@ -932,20 +932,33 @@ int vectorScan(char * file, int* n, double**pp){
932 932
933//////////////////////////////////////////////////////////////////////////////// 933////////////////////////////////////////////////////////////////////////////////
934 934
935#if defined (__APPLE__) || (__FreeBSD__) 935#if defined (__APPLE__) || (__FreeBSD__) || defined(NO_RANDOM_R) || defined(_WIN32) || defined(WIN32)
936/* FreeBSD and Mac OS X do not provide random_r(), thread safety cannot be 936/* Windows use thread-safe random
937 guaranteed. 937 See: http://stackoverflow.com/questions/143108/is-windows-rand-s-thread-safe
938*/
939#if defined (__APPLE__) || (__FreeBSD__) || defined(NO_RANDOM_R)
940
941/* For FreeBSD, Mac OS X, and other libcs (like `musl`) that do not provide
942 random_r(), or if the use of random_r() is explicitly disabled, thread safety
943 cannot be guaranteed.
944 As per current understanding, this should at worst lead to less "random"
945 numbers being generated, in particular
946 * if another thread somebody calls lcong48() at the same time as nrand48()
947 is called
948 * in addition to that, for glibc with NO_RANDOM_R enabled when ndrand48()
949 is called for the first time by multiple threads in parallel due to the
950 initialisation function placed within it
951 See: http://www.evanjones.ca/random-thread-safe.html
952
938 For FreeBSD and Mac OS X, nrand48() is much better than random(). 953 For FreeBSD and Mac OS X, nrand48() is much better than random().
939 See: http://www.evanjones.ca/random-thread-safe.html 954 See: http://www.evanjones.ca/random-thread-safe.html
940*/
941#pragma message "randomVector is not thread-safe in OSX and FreeBSD"
942#endif
943 955
944#if defined (__APPLE__) || (__FreeBSD__) || defined(_WIN32) || defined(WIN32) 956 TODO: As mentioned in the linked article, this could be fixed:
945/* Windows use thread-safe random 957 "the best solution for truly portable applications is to include
946 See: http://stackoverflow.com/questions/143108/is-windows-rand-s-thread-safe 958 your own random number generator implementation,
959 and not rely on the system's C library".
947*/ 960*/
948#if defined (__APPLE__) || (__FreeBSD__) 961#pragma message "randomVector is not thread-safe in OSX and FreeBSD or with NO_RANDOM_R; this likely leads to less random numbers at worst; see http://www.evanjones.ca/random-thread-safe.html"
949 962
950inline double urandom() { 963inline double urandom() {
951 /* the probalility of matching will be theoretically p^3(in fact, it is not) 964 /* the probalility of matching will be theoretically p^3(in fact, it is not)