mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Thread-secure rand(3).
This commit is contained in:
parent
3361620d83
commit
a1ccba00f7
1 changed files with 8 additions and 1 deletions
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue