2019-01-30 09:30:31 +00:00
|
|
|
/*
|
|
|
|
* machinarium.
|
|
|
|
*
|
|
|
|
* cooperative multitasking engine.
|
2020-04-02 11:00:56 +00:00
|
|
|
*/
|
2019-01-30 09:30:31 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
2020-11-25 10:17:15 +00:00
|
|
|
#include <sys/types.h>
|
2019-01-30 09:30:31 +00:00
|
|
|
#include <time.h>
|
2020-11-25 10:17:15 +00:00
|
|
|
#include <unistd.h>
|
2019-01-30 09:30:31 +00:00
|
|
|
|
|
|
|
__thread unsigned short prng_seed[3];
|
|
|
|
__thread unsigned short *prng_state = NULL;
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
long int pg_lrand48(unsigned short *_rand48_seed);
|
|
|
|
void pg_srand48(long seed, unsigned short *_rand48_seed);
|
2019-01-30 09:30:31 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
void mm_lrand48_seed(void)
|
2019-01-30 09:30:31 +00:00
|
|
|
{
|
2020-04-02 11:00:56 +00:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
|
|
|
|
long int rand_seed_2 = 0;
|
|
|
|
long int rand_seed;
|
|
|
|
rand_seed = getpid() ^ getuid() ^ tv.tv_sec ^ tv.tv_usec;
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
fd = open("/dev/random", O_RDONLY);
|
|
|
|
if (fd != -1) {
|
|
|
|
int rc = read(fd, &rand_seed_2, sizeof(rand_seed_2));
|
|
|
|
(void)rc;
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
rand_seed ^= rand_seed_2;
|
|
|
|
pg_srand48(rand_seed, prng_seed);
|
|
|
|
prng_state = prng_seed;
|
2019-01-30 09:30:31 +00:00
|
|
|
}
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
long int machine_lrand48(void)
|
2019-01-30 09:30:31 +00:00
|
|
|
{
|
2020-04-02 11:00:56 +00:00
|
|
|
assert(prng_state);
|
|
|
|
return pg_lrand48(prng_state);
|
2019-03-12 16:39:46 +00:00
|
|
|
}
|