From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id A6CFB384DB68; Mon, 13 Nov 2023 19:03:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A6CFB384DB68 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1699902208; bh=S/0Yttsc/+COgUokO71T2+3OsC57KQWDVmqhlMewcXA=; h=From:To:Subject:Date:From; b=SyHlJ8wKjaf2tjNZfcsqi/4khcBRUHsl6/hODUtEVmJNjY5AQj26Culs+OLwe3iW3 UAG+w/mq+07Xb1W7tgHBe0FFL8+xvxfUz1LZuzi8gNxl/Wa2o6AtgQsR7zpaQMOrGR tcbLK43t/SImcAyREJXOaAhFF9ttq0C9qqvHB/ZI= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/cygwin-3_4-branch] Cygwin: random: make random(3) functions thread-safe X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/cygwin-3_4-branch X-Git-Oldrev: 97a975dac2dee9640964f54fa9693f0295c2363c X-Git-Newrev: 6461906beb86c4583eb4e3f8bb1ed03221277f3e Message-Id: <20231113190328.A6CFB384DB68@sourceware.org> Date: Mon, 13 Nov 2023 19:03:28 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D6461906beb8= 6c4583eb4e3f8bb1ed03221277f3e commit 6461906beb86c4583eb4e3f8bb1ed03221277f3e Author: Corinna Vinschen AuthorDate: Mon Nov 13 19:41:08 2023 +0100 Commit: Corinna Vinschen CommitDate: Mon Nov 13 19:52:37 2023 +0100 Cygwin: random: make random(3) functions thread-safe =20 Add locking to the random(3) family of functions to gain thread-safety per POSIX. Use NetBSD version of the file as role-model. =20 Reported-by: Bruno Haible Signed-off-by: Corinna Vinschen Diff: --- winsup/cygwin/random.cc | 47 +++++++++++++++++++++++++++++++++++++++++----= -- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/winsup/cygwin/random.cc b/winsup/cygwin/random.cc index 163fc040cef5..b38fdc078189 100644 --- a/winsup/cygwin/random.cc +++ b/winsup/cygwin/random.cc @@ -29,6 +29,14 @@ =20 #define __INSIDE_CYGWIN__ =20 +#include "winsup.h" + +SRWLOCK NO_COPY rndlock =3D SRWLOCK_INIT; +# define __random_lock() { if (__isthreaded) \ + AcquireSRWLockExclusive (&rndlock); } +# define __random_unlock() { if (__isthreaded) \ + ReleaseSRWLockExclusive (&rndlock); } + extern "C" { #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] =3D "@(#)random.c 8.2 (Berkeley) 5/19/95"; @@ -248,6 +256,8 @@ static inline uint32_t good_rand (int32_t x) #endif /* !USE_WEAK_SEEDING */ } =20 +static long __random_unlocked(); + /* * srandom: * @@ -260,8 +270,8 @@ static inline uint32_t good_rand (int32_t x) * introduced by the L.C.R.N.G. Note that the initialization of randtbl[] * for default usage relies on values produced by this routine. */ -void -srandom(unsigned x) +static void +__srandom_unlocked(unsigned x) { int i, lim; =20 @@ -276,7 +286,15 @@ srandom(unsigned x) lim =3D 10 * rand_deg; } for (i =3D 0; i < lim; i++) - (void)random(); + (void)__random_unlocked(); +} + +void +srandom(unsigned x) +{ + __random_lock(); + __srandom_unlocked(x); + __random_unlock(); } =20 /* @@ -350,6 +368,7 @@ initstate(unsigned seed, /* seed for R.N.G. */ char *ostate =3D (char *)(&state[-1]); uint32_t *int_arg_state =3D (uint32_t *)arg_state; =20 + __random_lock(); if (rand_type =3D=3D TYPE_0) state[-1] =3D rand_type; else @@ -358,6 +377,7 @@ initstate(unsigned seed, /* seed for R.N.G. */ (void)fprintf(stderr, "random: not enough state (%lu bytes); ignored.\n", (unsigned long) n); + __random_unlock(); return(0); } if (n < BREAK_1) { @@ -383,11 +403,12 @@ initstate(unsigned seed, /* seed for R.N.G. */ } state =3D int_arg_state + 1; /* first location */ end_ptr =3D &state[rand_deg]; /* must set end_ptr before srandom */ - srandom(seed); + __srandom_unlocked(seed); if (rand_type =3D=3D TYPE_0) int_arg_state[0] =3D rand_type; else int_arg_state[0] =3D MAX_TYPES * (rptr - state) + rand_type; + __random_unlock(); return(ostate); } =20 @@ -418,6 +439,7 @@ setstate(char *arg_state /* pointer to state array */) uint32_t rear =3D new_state[0] / MAX_TYPES; char *ostate =3D (char *)(&state[-1]); =20 + __random_lock(); if (rand_type =3D=3D TYPE_0) state[-1] =3D rand_type; else @@ -442,6 +464,7 @@ setstate(char *arg_state /* pointer to state array */) fptr =3D &state[(rear + rand_sep) % rand_deg]; } end_ptr =3D &state[rand_deg]; /* set end_ptr too */ + __random_unlock(); return(ostate); } =20 @@ -462,8 +485,8 @@ setstate(char *arg_state /* pointer to state array */) * * Returns a 31-bit random number. */ -long -random() +static long +__random_unlocked() { uint32_t i; uint32_t *f, *r; @@ -490,4 +513,16 @@ random() } return((long)i); } + +long +random() +{ + long r; + + __random_lock(); + r =3D __random_unlocked(); + __random_unlock(); + return (r); +} + }