Thread-secure rand(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2014-01-08 23:39:13 +01:00
parent 3361620d83
commit a1ccba00f7
1 changed files with 8 additions and 1 deletions

View File

@ -22,9 +22,11 @@
*******************************************************************************/
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
static pthread_mutex_t rand_mutex = PTHREAD_MUTEX_INITIALIZER;
static uint32_t m_w = 1337;
static uint32_t m_z = 37;
@ -37,11 +39,16 @@ static uint32_t random_32_bits()
extern "C" int rand()
{
return (int) (random_32_bits() % ((uint32_t) RAND_MAX + 1));
pthread_mutex_lock(&rand_mutex);
int result = (int) (random_32_bits() % ((uint32_t) RAND_MAX + 1));
pthread_mutex_unlock(&rand_mutex);
return result;
}
extern "C" void srand(unsigned int seed)
{
pthread_mutex_lock(&rand_mutex);
m_w = seed >> 16 & 0xFFFF;
m_z = seed >> 0 & 0xFFFF;
pthread_mutex_unlock(&rand_mutex);
}