From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/73 Path: news.gmane.org!not-for-mail From: Szabolcs Nagy Newsgroups: gmane.linux.lib.musl.general Subject: Re: Completeness status of musl Date: Fri, 24 Jun 2011 14:12:46 +0200 Message-ID: <20110624121246.GU27421@port70.net> References: <20110529180854.GC6142@port70.net> <20110530103009.GE6142@port70.net> <20110530154741.GB277@brightrain.aerifal.cx> <20110530172735.GF6142@port70.net> <20110608235810.GJ191@brightrain.aerifal.cx> <20110621014640.GL27421@port70.net> <20110623065452.GU12592@brightrain.aerifal.cx> <20110623132517.GO27421@port70.net> <20110623141608.GP27421@port70.net> <20110623185041.GQ27421@port70.net> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="it/zdz3K1bH9Y8/E" X-Trace: dough.gmane.org 1308917586 16915 80.91.229.12 (24 Jun 2011 12:13:06 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 24 Jun 2011 12:13:06 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-157-gllmg-musl=m.gmane.org@lists.openwall.com Fri Jun 24 14:13:00 2011 Return-path: Envelope-to: gllmg-musl@lo.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by lo.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1Qa5Fs-000451-6z for gllmg-musl@lo.gmane.org; Fri, 24 Jun 2011 14:13:00 +0200 Original-Received: (qmail 20138 invoked by uid 550); 24 Jun 2011 12:12:59 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 20130 invoked from network); 24 Jun 2011 12:12:58 -0000 Content-Disposition: inline In-Reply-To: <20110623185041.GQ27421@port70.net> User-Agent: Mutt/1.5.20 (2009-06-14) Xref: news.gmane.org gmane.linux.lib.musl.general:73 Archived-At: --it/zdz3K1bH9Y8/E Content-Type: text/plain; charset=us-ascii Content-Disposition: inline * Szabolcs Nagy [2011-06-23 20:50:41 +0200]: > * Szabolcs Nagy [2011-06-23 16:16:08 +0200]: > > modified a bit to use uint32_t and uint64_t > > i changed the seeding added locking (not tested) --it/zdz3K1bH9Y8/E Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="random-lock.diff" diff --git a/src/prng/random.c b/src/prng/random.c index 4b2daef..cc5702e 100644 --- a/src/prng/random.c +++ b/src/prng/random.c @@ -7,6 +7,7 @@ #include #include +#include "libc.h" /* this code uses the same lagged fibonacci generator as the @@ -32,6 +33,7 @@ static int n = 31; static int i = 3; static int j = 0; static uint32_t *x = init+1; +static int lock; static uint32_t lcg31(uint32_t x) { return (1103515245*x + 12345) & 0x7fffffff; @@ -53,7 +55,7 @@ static void loadstate(uint32_t *state) { j = x[-1]&0xff; } -void srandom(unsigned seed) { +static void __srandom(unsigned seed) { int k; uint64_t s = seed; @@ -71,11 +73,20 @@ void srandom(unsigned seed) { x[0] |= 1; } +void srandom(unsigned seed) { + LOCK(&lock); + __srandom(seed); + UNLOCK(&lock); +} + char *initstate(unsigned seed, char *state, size_t size) { - void *old = savestate(); + void *old; + if (size < 8) return 0; - else if (size < 32) + LOCK(&lock); + old = savestate(); + if (size < 32) n = 0; else if (size < 64) n = 7; @@ -86,26 +97,36 @@ char *initstate(unsigned seed, char *state, size_t size) { else n = 63; x = (uint32_t*)state + 1; - srandom(seed); + __srandom(seed); + UNLOCK(&lock); return old; } char *setstate(char *state) { - void *old = savestate(); + void *old; + + LOCK(&lock); + old = savestate(); loadstate((uint32_t*)state); + UNLOCK(&lock); return old; } long random(void) { long k; - if (n == 0) - return x[0] = lcg31(x[0]); + LOCK(&lock); + if (n == 0) { + k = x[0] = lcg31(x[0]); + goto end; + } x[i] += x[j]; k = x[i]>>1; if (++i == n) i = 0; if (++j == n) j = 0; +end: + UNLOCK(&lock); return k; } --it/zdz3K1bH9Y8/E--