]> git.rkrishnan.org Git - pihpsdr.git/commitdiff
making random number thread-safe.
authorc vw <dl1ycf@darc.de>
Thu, 25 Jul 2019 10:02:02 +0000 (12:02 +0200)
committerc vw <dl1ycf@darc.de>
Thu, 25 Jul 2019 10:02:02 +0000 (12:02 +0200)
hpsdrsim.c
newhpsdrsim.c

index 0f5e31a456796930b9a970e8da03e5aae4a9994a..97e3820857d13ebc75d2ac2286568cacacb7c9b4 100644 (file)
@@ -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<LENNOISE; i++) {
-          noiseItab[i]= ((double) rand() / j - 1.0) * 0.00003;
-          noiseQtab[i]= ((double) rand() / j - 1.0) * 0.00003;
+          noiseItab[i]= ((double) rand_r(&seed) / j - 1.0) * 0.00003;
+          noiseQtab[i]= ((double) rand_r(&seed) / j - 1.0) * 0.00003;
         }
 
        fprintf(stderr,".... producing an 800 Hz signal\n");
@@ -1074,6 +1077,9 @@ void *handler_ep6(void *arg)
         int noiseIQpt,toneIQpt,divpt,rxptr;
         double i1,q1,fac1,fac2,fac3,fac4;
         int decimation;  // for converting 1536 kHz samples to 48, 192, 384, ....
+        unsigned int seed;
+
+       seed=((uintptr_t) &seed) & 0xffffff;
 
        memcpy(buffer, id, 4);
 
@@ -1127,7 +1133,8 @@ void *handler_ep6(void *arg)
                    fac1=rxatt_dbl[0]*0.00001;          // Amplitude of 800-Hz-signal to ADC1
                    if (diversity) {
                        fac2=0.0001*rxatt_dbl[0];       // Amplitude of broad "man-made" noise to ADC1
-                       fac4=0.0003*rxatt_dbl[1];       // Amplitude of broad "man-made" noise to ADC2 (phase shifted 90 deg. and three times stronger)
+                       fac4=0.0002*rxatt_dbl[1];       // Amplitude of broad "man-made" noise to ADC2
+                                                       // (phase shifted 90 deg., 6 dB stronger)
                    }
                    for (j=0; j<n; j++) {
                        // ADC1: noise + weak tone on RX, feedback sig. on TX (except STEMlab)
@@ -1208,7 +1215,7 @@ void *handler_ep6(void *arg)
                        // Microphone samples: silence
                        pointer += 2;
                        rxptr++;              if (rxptr >= 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;
                    }
index 3cb137010ae8c225097106cc15c3db8328dad323..ddf11972c7ac285dfaf46641870e907ba1c589f6 100644 (file)
@@ -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)