From 1efbfa530425c9ec304ddf1b46b1dc2762fd5b40 Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Mon, 20 May 2019 11:26:06 +0200
Subject: [PATCH] bugfix: fix UE realtime bug

When pushing traffic with iperf the UE was crashing.

The argument passed to pthread_mutex_timedlock is absolute time based
on the CLOCK_REALTIME timer, not relative time. That seems to be the
root cause of the bug. (That there is a timeout is another story.)

This commit uses absolute value.

Also we separate cases between IS_SOFTMODEM_BASICSIM / IS_SOFTMODEM_RFSIM
and standard realtime UE. In the 'sim' modes we don't care about
timeout and just call pthread_mutex_lock. It seems to be the idea
of the original work. If not, then to be fixed.
---
 targets/RT/USER/lte-ue.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/targets/RT/USER/lte-ue.c b/targets/RT/USER/lte-ue.c
index 0a6df786829..d2e8c6ad465 100644
--- a/targets/RT/USER/lte-ue.c
+++ b/targets/RT/USER/lte-ue.c
@@ -1503,6 +1503,7 @@ void *UE_thread(void *arg) {
   int i;
   int th_id;
   static uint8_t thread_idx = 0;
+  int ret;
   cpu_set_t cpuset;
   CPU_ZERO(&cpuset);
 
@@ -1724,15 +1725,27 @@ void *UE_thread(void *arg) {
           }
 
           pickTime(gotIQs);
-          struct timespec tv= {0};
-          tv.tv_nsec=10*1000;
 
-          if( IS_SOFTMODEM_BASICSIM || IS_SOFTMODEM_RFSIM)
-            tv.tv_sec=INT_MAX;
+          /* no timeout in IS_SOFTMODEM_BASICSIM or IS_SOFTMODEM_RFSIM mode */
+          if (IS_SOFTMODEM_BASICSIM || IS_SOFTMODEM_RFSIM) {
+            ret = pthread_mutex_lock(&proc->mutex_rxtx);
+          } else {
+            struct timespec tv;
+            if (clock_gettime(CLOCK_REALTIME, &tv) != 0) {
+              perror("clock_gettime");
+              exit(1);
+            }
+            tv.tv_nsec += 10*1000;
+            if (tv.tv_nsec >= 1000 * 1000 * 1000) {
+              tv.tv_sec++;
+              tv.tv_nsec -= 1000 * 1000 * 1000;
+            }
+            ret = pthread_mutex_timedlock(&proc->mutex_rxtx, &tv);
+          }
 
           // operate on thread sf mod 2
-          if (pthread_mutex_timedlock(&proc->mutex_rxtx, &tv) !=0) {
-            if ( errno == ETIMEDOUT) {
+          if (ret != 0) {
+            if (ret == ETIMEDOUT) {
               LOG_E(PHY,"Missed real time\n");
               continue;
             } else {
-- 
GitLab