@@ -331,10 +331,13 @@ static void* CollectAndReport(void* data)
331331 do
332332 {
333333 T2Info ("%s while Loop -- START \n" , __FUNCTION__ );
334+ // Release reuseThreadMutex before acquiring reportInProgressMutex to maintain lock order
335+ pthread_mutex_unlock (& profile -> reuseThreadMutex );
334336 pthread_mutex_lock (& profile -> reportInProgressMutex );
335337 profile -> reportInProgress = true;
336338 pthread_cond_signal (& profile -> reportInProgressCond );
337339 pthread_mutex_unlock (& profile -> reportInProgressMutex );
340+ pthread_mutex_lock (& profile -> reuseThreadMutex );
338341
339342 int count = profile -> grepSeekProfile -> execCounter ;
340343
@@ -549,8 +552,18 @@ static void* CollectAndReport(void* data)
549552 pthread_cond_init (& profile -> reportcond , NULL );
550553 clock_gettime (CLOCK_REALTIME , & profile -> currentTime );
551554 profile -> maxlatencyTime .tv_sec = profile -> currentTime .tv_sec ;
552- srand (time (0 )); // Initialise the random number generator
553- maxuploadinmilliSec = rand () % (profile -> maxUploadLatency - 1 );
555+ unsigned int random_value = 0 ;
556+ FILE * urandom = fopen ("/dev/urandom" , "r" );
557+ if (urandom != NULL && fread (& random_value , sizeof (random_value ), 1 , urandom ) == 1 )
558+ {
559+ maxuploadinmilliSec = random_value % (profile -> maxUploadLatency - 1 );
560+ fclose (urandom );
561+ }
562+ else
563+ {
564+ if (urandom != NULL ) fclose (urandom );
565+ maxuploadinmilliSec = (unsigned int )(time (0 ) % (profile -> maxUploadLatency - 1 ));
566+ }
554567 maxuploadinSec = (maxuploadinmilliSec + 1 ) / 1000 ;
555568 }
556569 if ( strcmp (profile -> protocol , "HTTP" ) == 0 || strcmp (profile -> protocol , "RBUS_METHOD" ) == 0 )
@@ -564,7 +577,9 @@ static void* CollectAndReport(void* data)
564577 pthread_mutex_lock (& profile -> reportMutex );
565578 T2Info ("waiting for %ld sec of macUploadLatency\n" , (long ) maxuploadinSec );
566579 profile -> maxlatencyTime .tv_sec += maxuploadinSec ;
567- n = pthread_cond_timedwait (& profile -> reportcond , & profile -> reportMutex , & profile -> maxlatencyTime );
580+ do {
581+ n = pthread_cond_timedwait (& profile -> reportcond , & profile -> reportMutex , & profile -> maxlatencyTime );
582+ } while (n != ETIMEDOUT && n != 0 );
568583 if (n == ETIMEDOUT )
569584 {
570585 T2Info ("TIMEOUT for maxUploadLatency of profile %s\n" , profile -> name );
@@ -615,7 +630,9 @@ static void* CollectAndReport(void* data)
615630 pthread_mutex_lock (& profile -> reportMutex );
616631 T2Info ("waiting for %ld sec of macUploadLatency\n" , (long ) maxuploadinSec );
617632 profile -> maxlatencyTime .tv_sec += maxuploadinSec ;
618- n = pthread_cond_timedwait (& profile -> reportcond , & profile -> reportMutex , & profile -> maxlatencyTime );
633+ do {
634+ n = pthread_cond_timedwait (& profile -> reportcond , & profile -> reportMutex , & profile -> maxlatencyTime );
635+ } while (n != ETIMEDOUT && n != 0 );
619636 if (n == ETIMEDOUT )
620637 {
621638 T2Info ("TIMEOUT for maxUploadLatency of profile %s\n" , profile -> name );
@@ -782,9 +799,11 @@ reportThreadEnd :
782799 }
783800 while (profile -> enable );
784801 T2Info ("%s --out Exiting collect and report Thread\n" , __FUNCTION__ );
802+ pthread_mutex_unlock (& profile -> reuseThreadMutex );
785803 pthread_mutex_lock (& profile -> reportInProgressMutex );
786804 profile -> reportInProgress = false;
787805 pthread_mutex_unlock (& profile -> reportInProgressMutex );
806+ pthread_mutex_lock (& profile -> reuseThreadMutex );
788807 profile -> threadExists = false;
789808 pthread_mutex_unlock (& profile -> reuseThreadMutex );
790809 pthread_mutex_destroy (& profile -> reuseThreadMutex );
0 commit comments