Commit 4ef62f71 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Avoid problems with srand() and speed up a bit (avoid dlsym)

(cherry picked from commit 1ae21d47)
parent a6b7d025
...@@ -148,24 +148,28 @@ int unsetenv (const char *name) ...@@ -148,24 +148,28 @@ int unsetenv (const char *name)
* preserve reproducibility of the number sequence (which usually does not * preserve reproducibility of the number sequence (which usually does not
* matter). * matter).
**/ **/
static pthread_mutex_t prng_lock = PTHREAD_MUTEX_INITIALIZER; static struct
{
pthread_mutex_t lock;
unsigned int seed;
} prng = { PTHREAD_MUTEX_INITIALIZER, 0, };
void srand (unsigned int seed) void srand (unsigned int seed)
{ {
pthread_mutex_lock (&prng_lock); pthread_mutex_lock (&prng.lock);
LOG("Warning", "%d", seed); LOG("Warning", "%d", seed);
CALL(srand, seed); prng.seed = seed;
pthread_mutex_unlock (&prng_lock); pthread_mutex_unlock (&prng.lock);
} }
int rand (void) int rand (void)
{ {
int ret; int ret;
pthread_mutex_lock (&prng_lock); pthread_mutex_lock (&prng.lock);
LOG("Warning", ""); LOG("Warning", "");
ret = CALL(rand); ret = rand_r (&prng.seed);
pthread_mutex_unlock (&prng_lock); pthread_mutex_unlock (&prng.lock);
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment