From 51992d4b7cba22f4694991b03e3249e337ef1bb7 Mon Sep 17 00:00:00 2001 From: c vw Date: Thu, 25 Jul 2019 12:02:02 +0200 Subject: [PATCH] making random number thread-safe. --- hpsdrsim.c | 15 +++++++++++---- newhpsdrsim.c | 5 ++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/hpsdrsim.c b/hpsdrsim.c index 0f5e31a..97e3820 100644 --- a/hpsdrsim.c +++ b/hpsdrsim.c @@ -211,6 +211,7 @@ int main(int argc, char *argv[]) unsigned long checksum; socklen_t lenaddr; struct sockaddr_in addr_from; + unsigned int seed; uint32_t last_seqnum = 0xffffffff, seqnum; // sequence number of received packet @@ -231,6 +232,8 @@ int main(int argc, char *argv[]) * Examples for C25: RedPitaya based boards with fixed ADC connections */ + // seed value for random number generator + seed = ((uintptr_t) &seed) & 0xffffff; diversity=0; OLDDEVICE=DEVICE_ORION2; NEWDEVICE=NEW_DEVICE_ORION2; @@ -264,8 +267,8 @@ int main(int argc, char *argv[]) // Produce some noise j=RAND_MAX / 2; for (i=0; i= OLDRTXLEN) rxptr=0; - noiseIQpt++; if (noiseIQpt >= LENNOISE) noiseIQpt=rand() / NOISEDIV; + noiseIQpt++; if (noiseIQpt >= LENNOISE) noiseIQpt=rand_r(&seed) / NOISEDIV; toneIQpt+=decimation; if (toneIQpt >= LENTONE) toneIQpt=0; divpt+=decimation; if (divpt >= LENDIV) divpt=0; } diff --git a/newhpsdrsim.c b/newhpsdrsim.c index 3cb1370..ddf1197 100644 --- a/newhpsdrsim.c +++ b/newhpsdrsim.c @@ -735,6 +735,7 @@ void *rx_thread(void *data) { int rxptr; int divptr; int decimation; + unsigned int seed; struct timespec delay; #ifdef __APPLE__ @@ -744,6 +745,8 @@ void *rx_thread(void *data) { myddc=(int) (uintptr_t) data; if (myddc < 0 || myddc >= NUMRECEIVERS) return NULL; seqnum=0; + // unique seed value for random number generator + seed = ((uintptr_t) &seed) & 0xffffff; sock=socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { @@ -834,7 +837,7 @@ void *rx_thread(void *data) { // i1sample=i0sample=noiseItab[noisept]; q1sample=q0sample=noiseQtab[noisept++]; - if (noisept == LENNOISE) noisept=rand() / NOISEDIV; + if (noisept == LENNOISE) noisept=rand_r(&seed) / NOISEDIV; // // PS: produce sample PAIRS, // a) distorted TX data (with Drive and Attenuation) -- 2.45.2