]> git.sesse.net Git - vlc/commitdiff
Work-around non-thread-safe use of the C random number generator
authorRémi Denis-Courmont <remi@remlab.net>
Sun, 11 Apr 2010 15:37:49 +0000 (18:37 +0300)
committerRémi Denis-Courmont <remi@remlab.net>
Sun, 11 Apr 2010 15:39:16 +0000 (18:39 +0300)
bin/override.c

index 5098a7d59823e329a3c9e6469a72e1fdece1b84d..2f7606ba56367fc9964afced6a24a1f50c894700 100644 (file)
@@ -41,6 +41,7 @@ void vlc_enable_override (void)
 #include <stdio.h>
 #include <stdlib.h>
 #include <dlfcn.h>
+#include <pthread.h>
 
 static void vlogbug (const char *level, const char *func, const char *fmt,
                      va_list ap)
@@ -116,4 +117,34 @@ int unsetenv (const char *name)
 }
 
 
+/*** Pseudo random numbers ***
+ *
+ * The C PRNG is not thread-safe (and generally sucks, the POSIX 48-bits PRNG
+ * is much better as a reproducible non-secure PRNG). To work around this, we
+ * force evil callers to serialize. This makes the call safe, but fails to
+ * preserve reproducibility of the number sequence (which usually does not
+ * matter).
+ **/
+static pthread_mutex_t prng_lock = PTHREAD_MUTEX_INITIALIZER;
+
+void srand (unsigned int seed)
+{
+    pthread_mutex_lock (&prng_lock);
+    LOG("Warning", "%d", seed);
+    CALL(srand, seed);
+    pthread_mutex_unlock (&prng_lock);
+}
+
+int rand (void)
+{
+    int ret;
+
+    pthread_mutex_lock (&prng_lock);
+    LOG("Warning", "");
+    ret = CALL(rand);
+    pthread_mutex_unlock (&prng_lock);
+    return ret;
+}
+
+
 #endif /* __ELF__ */