From 3720aa05834c119d3ac87917bfced03acebaf5ce Mon Sep 17 00:00:00 2001
From: Wang Tsu-Han <wangts@eurecom.fr>
Date: Tue, 4 Jun 2019 15:23:50 +0200
Subject: [PATCH] assersion condition and timedlock overflow fix

---
 openair1/PHY/defs_common.h |  9 +++++++--
 targets/RT/USER/lte-enb.c  |  1 +
 targets/RT/USER/lte-ru.c   | 39 ++++++++++++++++++++++++--------------
 3 files changed, 33 insertions(+), 16 deletions(-)

diff --git a/openair1/PHY/defs_common.h b/openair1/PHY/defs_common.h
index 78c12683c1..697067b577 100644
--- a/openair1/PHY/defs_common.h
+++ b/openair1/PHY/defs_common.h
@@ -1099,10 +1099,15 @@ static inline int timedwait_on_condition(pthread_mutex_t *mutex,pthread_cond_t *
     // proc->instance_cnt_rxtx is -1
     abstime.tv_sec=now.tv_sec;
     abstime.tv_nsec = now.tv_nsec + time_ns;
-    if ((waitret = pthread_cond_timedwait(cond,mutex,&abstime))==ETIMEDOUT) break; // this unlocks mutex_rxtx while waiting and then locks it again
+    if (abstime.tv_nsec >= 1000*1000*1000)
+    {
+      abstime.tv_nsec -= 1000*1000*1000;
+      abstime.tv_sec  += 1;
+    }
+    if ((waitret = pthread_cond_timedwait(cond,mutex,&abstime)) == 0) break; // this unlocks mutex_rxtx while waiting and then locks it again
   }
 
-  AssertFatal((rc = pthread_mutex_unlock(mutex))==0,"[SCHED][eNB] timedwait_on_condition(): error unlocking mutex return %d for %s\n", rc, name);
+  AssertFatal((rc = pthread_mutex_unlock(mutex)) == 0,"[SCHED][eNB] timedwait_on_condition(): error unlocking mutex return %d for %s\n", rc, name);
 
   return(0);
 }
diff --git a/targets/RT/USER/lte-enb.c b/targets/RT/USER/lte-enb.c
index 3dfa7e741c..2091b03cc6 100644
--- a/targets/RT/USER/lte-enb.c
+++ b/targets/RT/USER/lte-enb.c
@@ -420,6 +420,7 @@ static void *L1_thread( void *param ) {
     VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_RX0_ENB,proc->subframe_rx);
     VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX0_ENB,proc->frame_tx);
     VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_RX0_ENB,proc->frame_rx);
+
   
     if (oai_exit) break;
 
diff --git a/targets/RT/USER/lte-ru.c b/targets/RT/USER/lte-ru.c
index 8ab3f3979b..eac8382cec 100644
--- a/targets/RT/USER/lte-ru.c
+++ b/targets/RT/USER/lte-ru.c
@@ -862,7 +862,12 @@ void wakeup_slaves(RU_proc_t *proc) {
     // lock the FH mutex and make sure the thread is ready
     clock_gettime(CLOCK_REALTIME,&wait);
     wait.tv_nsec += time_ns;
-    AssertFatal((ret=pthread_mutex_timedlock(&slave_proc->mutex_FH,&wait))==ETIMEDOUT,"ERROR pthread_mutex_lock for RU %d slave %d (IC %d)\n",proc->ru->idx,slave_proc->ru->idx,slave_proc->instance_cnt_FH);
+    if(wait.tv_nsec >= 1000*1000*1000)
+    {
+      wait.tv_nsec -= 1000*1000*1000;
+      wait.tv_sec  += 1;
+    }
+    AssertFatal((ret=pthread_mutex_timedlock(&slave_proc->mutex_FH,&wait))==0,"ERROR pthread_mutex_lock for RU %d slave %d (IC %d)\n",proc->ru->idx,slave_proc->ru->idx,slave_proc->instance_cnt_FH);
 
     int cnt_slave            = ++slave_proc->instance_cnt_FH;
     slave_proc->frame_rx     = proc->frame_rx;
@@ -989,7 +994,12 @@ int wakeup_synch(RU_t *ru) {
   // lock the synch mutex and make sure the thread is readif (pthread_mutex_timedlock(&ru->proc.mutex_synch,&wait) != 0) {
   clock_gettime(CLOCK_REALTIME,&wait);
   wait.tv_nsec += time_ns;
-  AssertFatal((ret=pthread_mutex_timedlock(&ru->proc.mutex_synch,&wait))==ETIMEDOUT,"[RU] ERROR pthread_mutex_lock for RU synch thread (IC %d)\n", ru->proc.instance_cnt_synch );
+  if(wait.tv_nsec >= 1000*1000*1000)
+  {
+    wait.tv_nsec -= 1000*1000*1000;
+    wait.tv_sec  += 1;
+  }
+  AssertFatal((ret=pthread_mutex_timedlock(&ru->proc.mutex_synch,&wait)) == 0,"[RU] ERROR pthread_mutex_lock for RU synch thread (IC %d)\n", ru->proc.instance_cnt_synch );
 
   ++ru->proc.instance_cnt_synch;
 
@@ -1189,22 +1199,18 @@ void wakeup_L1s(RU_t *ru) {
 }
 inline int wakeup_prach_ru(RU_t *ru) {
   int ret;
-  /*struct timespec wait;
+  struct timespec wait;
   int time_ns = 5000000L;
 
   clock_gettime(CLOCK_REALTIME,&wait);
   wait.tv_nsec += time_ns;
-  AssertFatal((ret=pthread_mutex_timedlock(&ru->proc.mutex_prach,&wait))==ETIMEDOUT,"[RU] ERROR pthread_mutex_lock for RU prach thread (IC %d)\n", ru->proc.instance_cnt_prach);
-*/
-  struct timespec wait;
-  wait.tv_sec=0;
-  wait.tv_nsec=5000000L;
-
-  if (pthread_mutex_timedlock(&ru->proc.mutex_prach,&wait) !=0) {
-    LOG_E( PHY, "[RU] ERROR pthread_mutex_lock for RU prach thread (IC %d)\n", ru->proc.instance_cnt_prach);
-    exit_fun( "error locking mutex_rxtx" );
-    return(-1);
+  if(wait.tv_nsec >= 1000*1000*1000)
+  {
+    wait.tv_nsec -= 1000*1000*1000;
+    wait.tv_sec  += 1;
   }
+  AssertFatal((ret=pthread_mutex_timedlock(&ru->proc.mutex_prach,&wait)) == 0,"[RU] ERROR pthread_mutex_lock for RU prach thread (IC %d)\n", ru->proc.instance_cnt_prach);
+
 
   if (ru->proc.instance_cnt_prach==-1) {
     ++ru->proc.instance_cnt_prach;
@@ -1235,7 +1241,12 @@ inline int wakeup_prach_ru_br(RU_t *ru) {
 
   clock_gettime(CLOCK_REALTIME,&wait);
   wait.tv_nsec += time_ns;
-  AssertFatal((ret=pthread_mutex_timedlock(&ru->proc.mutex_prach_br,&wait))==ETIMEDOUT,"[RU] ERROR pthread_mutex_lock for RU prach thread BR (IC %d)\n", ru->proc.instance_cnt_prach_br);
+  if(wait.tv_nsec >= 1000*1000*1000)
+  {
+    wait.tv_nsec -= 1000*1000*1000;
+    wait.tv_sec  += 1;
+  }
+  AssertFatal((ret=pthread_mutex_timedlock(&ru->proc.mutex_prach_br,&wait))==0,"[RU] ERROR pthread_mutex_lock for RU prach thread BR (IC %d)\n", ru->proc.instance_cnt_prach_br);
 
 
   if (ru->proc.instance_cnt_prach_br==-1) {
-- 
GitLab