--- a/sys/libkern/random.c +++ b/sys/libkern/random.c @@ -32,6 +32,7 @@ */ #include +#include static u_long randseed = 1; @@ -49,7 +50,8 @@ u_long krandom(void) { - long x, hi, lo, t; + u_long x; + long hi, lo, t; /* * Compute x[n + 1] = (7^5 * x[n]) mod (2^31 - 1). @@ -57,12 +59,13 @@ * Park and Miller, Communications of the ACM, vol. 31, no. 10, * October 1988, p. 1195. */ - x = randseed; - hi = x / 127773; - lo = x % 127773; - t = 16807 * lo - 2836 * hi; - if (t <= 0) - t += 0x7fffffff; - randseed = t; + do { + x = randseed; + hi = x / 127773; + lo = x % 127773; + t = 16807 * lo - 2836 * hi; + if (t <= 0) + t += 0x7fffffff; + } while (atomic_cmpset_long(&randseed, x, (u_long)t) == 0); return (t); }