Ensure thread safe through use of spinlock; add further comments.
This commit is contained in:
@ -18,6 +18,9 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @file random_jkiss.c - Random number generator for the MariaDB Corporation MaxScale
|
* @file random_jkiss.c - Random number generator for the MariaDB Corporation MaxScale
|
||||||
|
*
|
||||||
|
* See http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf for discussion of random
|
||||||
|
* number generators (RNGs).
|
||||||
*
|
*
|
||||||
* @verbatim
|
* @verbatim
|
||||||
* Revision History
|
* Revision History
|
||||||
@ -35,15 +38,22 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <random_jkiss.h>
|
#include <random_jkiss.h>
|
||||||
|
|
||||||
/* Public domain code for JKISS RNG - Comments added */
|
/* Public domain code for JKISS RNG - Comment header added */
|
||||||
|
|
||||||
|
/* If possible, the seed variables will be set from /dev/urandom but
|
||||||
|
* should that fail, these arbitrary numbers will be used as a last resort.
|
||||||
|
*/
|
||||||
static unsigned int x = 123456789,y = 987654321,z = 43219876,c = 6543217; /* Seed variables */
|
static unsigned int x = 123456789,y = 987654321,z = 43219876,c = 6543217; /* Seed variables */
|
||||||
static bool init = false;
|
static bool init = false;
|
||||||
|
|
||||||
static void random_init_jkiss();
|
static SPINLOCK random_jkiss_spinlock = SPINLOCK_INIT;
|
||||||
|
|
||||||
|
static unsigned int random_jkiss_devrand(void);
|
||||||
|
static void random_init_jkiss(void);
|
||||||
|
|
||||||
/***
|
/***
|
||||||
*
|
*
|
||||||
* Return a random number
|
* Return a pseudo-random number that satisfies major tests for random sequences
|
||||||
*
|
*
|
||||||
* @return uint Random number
|
* @return uint Random number
|
||||||
*
|
*
|
||||||
@ -52,9 +62,13 @@ unsigned int
|
|||||||
random_jkiss(void)
|
random_jkiss(void)
|
||||||
{
|
{
|
||||||
unsigned long long t;
|
unsigned long long t;
|
||||||
|
unsigned int result;
|
||||||
|
spinlock_acquire(&random_jkiss_spinlock);
|
||||||
if (!init)
|
if (!init)
|
||||||
{
|
{
|
||||||
|
/* Must set init first because initialisation calls this function */
|
||||||
init = true;
|
init = true;
|
||||||
|
spinlock_release(&random_jkiss_spinlock);
|
||||||
random_init_jkiss();
|
random_init_jkiss();
|
||||||
}
|
}
|
||||||
x = 314527869 * x + 1234567;
|
x = 314527869 * x + 1234567;
|
||||||
@ -64,7 +78,9 @@ random_jkiss(void)
|
|||||||
t = 4294584393ULL * z + c;
|
t = 4294584393ULL * z + c;
|
||||||
c = t >> 32;
|
c = t >> 32;
|
||||||
z = t;
|
z = t;
|
||||||
return x + y + z;
|
result = x + y + z;
|
||||||
|
spinlock_release(&random_jkiss_spinlock);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Own code adapted from http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf */
|
/* Own code adapted from http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf */
|
||||||
@ -72,19 +88,21 @@ random_jkiss(void)
|
|||||||
/***
|
/***
|
||||||
*
|
*
|
||||||
* Obtain a seed random number from /dev/urandom if available.
|
* Obtain a seed random number from /dev/urandom if available.
|
||||||
* Otherwise use constant values
|
|
||||||
*
|
*
|
||||||
* @return uint Random number
|
* @return uint Random number
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static unsigned int
|
static unsigned int
|
||||||
random_devrand()
|
random_jkiss_devrand(void)
|
||||||
{
|
{
|
||||||
int fn;
|
int fn;
|
||||||
unsigned int r;
|
unsigned int r;
|
||||||
fn = open("/dev/urandom", O_RDONLY);
|
if ((fn = open("/dev/urandom", O_RDONLY)) == -1) return 0;
|
||||||
if (fn == -1) return 0;
|
if (read(fn, &r, 4) != 4)
|
||||||
if (read(fn, &r, 4) != 4) return 0;
|
{
|
||||||
|
close(fn);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
close(fn);
|
close(fn);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@ -96,13 +114,15 @@ random_devrand()
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
random_init_jkiss()
|
random_init_jkiss(void)
|
||||||
{
|
{
|
||||||
int newrand, i;
|
int newrand, i;
|
||||||
if ((newrand = random_devrand()) != 0) x = newrand;
|
if ((newrand = random_jkiss_devrand()) != 0) x = newrand;
|
||||||
if ((newrand = random_devrand()) != 0) y = newrand;
|
if ((newrand = random_jkiss_devrand()) != 0) y = newrand;
|
||||||
if ((newrand = random_devrand()) != 0) z = newrand;
|
if ((newrand = random_jkiss_devrand()) != 0) z = newrand;
|
||||||
if ((newrand = random_devrand()) != 0)
|
if ((newrand = random_jkiss_devrand()) != 0)
|
||||||
c = newrand % 698769068 + 1; /* Should be less than 698769069 */
|
c = newrand % 698769068 + 1; /* Should be less than 698769069 */
|
||||||
|
|
||||||
|
/* "Warm up" our random number generator */
|
||||||
for (i = 0; i < 100; i++) random_jkiss();
|
for (i = 0; i < 100; i++) random_jkiss();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user