2 #include "../port/lib.h"
6 #include "../port/error.h"
30 return i != 1 && i != (1 - sizeof(rb.buf));
36 return rb.wp != rb.rp;
42 up->basepri = PriNormal;
43 up->priority = up->basepri;
47 if(++rb.randomcount > 100000)
52 sleep(&rb.producer, rbnotfull, 0);
57 * produce random bits in a circular buffer
62 if(rb.randomcount == 0 || !rbnotfull(0))
65 rb.bits = (rb.bits<<2) ^ rb.randomcount;
86 /* Frequency close but not equal to HZ */
87 addclock0link(randomclock, MS2HZ+3);
88 rb.ep = rb.buf + sizeof(rb.buf);
89 rb.rp = rb.wp = rb.buf;
90 kproc("genrandom", genrandom, 0);
94 * consume random bytes from a circular buffer
97 randomread(void *xp, ulong n)
110 for(e = p + n; p < e; ){
113 wakeup(&rb.producer);
114 sleep(&rb.consumer, rbnotempty, 0);
120 * beating clocks will be predictable if
121 * they are synchronized. Use a cheap pseudo-
122 * random number generator to obscure any cycles.
124 x = rb.randn*1103515245 ^ *rb.rp;
135 wakeup(&rb.producer);