X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Futils%2Fmono-rand.c;h=71be40b92bf7fc5abc22553512baf33f3c7c835a;hb=4f9c74a2c4dbfed27d09dcb99d4d590a036d6f69;hp=2b4effbcc13909fc3afa1d5db4f6fcb68074eb6a;hpb=a4e986024fc235f82a0a07aaa5cf0273ca9a427d;p=mono.git diff --git a/mono/utils/mono-rand.c b/mono/utils/mono-rand.c index 2b4effbcc13..71be40b92bf 100644 --- a/mono/utils/mono-rand.c +++ b/mono/utils/mono-rand.c @@ -16,7 +16,9 @@ #include #include +#include "atomic.h" #include "mono-rand.h" +#include "mono-threads.h" #include "metadata/exception.h" #include "metadata/object.h" @@ -80,7 +82,7 @@ mono_rand_try_get_bytes (gpointer *handle, guchar *buffer, gint buffer_size) { HCRYPTPROV provider; - g_assert (handle) + g_assert (handle); provider = (HCRYPTPROV) *handle; if (!CryptGenRandom (provider, buffer_size, buffer)) { @@ -188,8 +190,12 @@ get_entropy_from_egd (const char *path, guchar *buffer, int buffer_size) gboolean mono_rand_open (void) { - if (use_egd || (file >= 0)) + static gint32 status = 0; + if (status != 0 || InterlockedCompareExchange (&status, 1, 0) != 0) { + while (status != 2) + mono_thread_info_yield (); return TRUE; + } #ifdef NAME_DEV_URANDOM file = open (NAME_DEV_URANDOM, O_RDONLY); @@ -201,6 +207,8 @@ mono_rand_open (void) if (file < 0) use_egd = g_getenv("MONO_EGD_SOCKET") != NULL; + status = 2; + return TRUE; } @@ -257,7 +265,17 @@ mono_rand_close (gpointer provider) gboolean mono_rand_open (void) { + static gint32 status = 0; + if (status != 0 || InterlockedCompareExchange (&status, 1, 0) != 0) { + while (status != 2) + mono_thread_info_yield (); + return TRUE; + } + srand (time (NULL)); + + status = 2; + return TRUE; }