rework on mutex test
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Thu, 2 Oct 2025 16:18:46 +0000 (18:18 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Thu, 2 Oct 2025 16:18:46 +0000 (18:18 +0200)
mutex.c

diff --git a/mutex.c b/mutex.c
index 39f7861eed88b701e81e6ead193de3bf02035694..666fcb589f0a86c547d32edd828f0fe61fb87322 100644 (file)
--- a/mutex.c
+++ b/mutex.c
@@ -4,6 +4,7 @@
 
 #include <pthread.h>
 #include <stdio.h>
+#include <unistd.h>
 
 #include "mtime.h"
 #include "test.h"
 dts_t *deltas = NULL;
 int nb_measurements = 0;
 
-char *message = "mutex";
+char *message = "Mutex latency";
 void (*usage_ext) (FILE *) = NULL;
 int (*parse_arg_ext) (char *) = NULL;
 
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t event = PTHREAD_MUTEX_INITIALIZER;
 volatile int shared_flag = 0;
-ts_t timestamp1, timestamp2;
+ts_t ts1, ts2;
 int try = 0;
+int nbtoofast = 0;
 
+#define TIMER 1000
 void *mutex_giver_task (__attribute__((unused)) void *arg)
 {
+
     while (try < nb_measurements) {
         pthread_mutex_lock (&mutex);
-        sys_timestamp (&timestamp1);
+        usleep (TIMER);
         shared_flag = 1;
+        sys_timestamp (&ts1);
         pthread_mutex_unlock (&mutex);
+        //shared_flag = 0;
+        usleep (TIMER);
     }
     pthread_exit (NULL);
 }
@@ -36,11 +44,32 @@ void *mutex_giver_task (__attribute__((unused)) void *arg)
 void *mutex_taker_task (__attribute__((unused)) void *arg)
 {
 
+    /* real-time process */
+/*
+    struct sched_param param = {0};
+    if (sched_getparam (0, &param) != 0) {
+        fprintf (stderr, "error: sched_getparam\n");
+        return 1;
+    }
+    param.sched_priority += 10;
+    int rc = sched_setscheduler (0, SCHED_FIFO, &param);
+    if (rc != 0) {
+        fprintf (stderr, "error: sched_setscheduler\n");
+        return 1;
+    }
+*/
+
     while (try < nb_measurements) {
+        ts_t ts;
+        sys_timestamp (&ts);
         pthread_mutex_lock (&mutex);
         if (shared_flag) {
-            sys_timestamp (&timestamp2);
-            deltas[try++] = diff_timestamp (&timestamp2, &timestamp1);
+            sys_timestamp (&ts2);
+            if (diff_timestamp (&ts2, &ts) < (9 * TIMER / 10)) {
+                nbtoofast++;
+            } else {
+                deltas[try++] = diff_timestamp (&ts2, &ts1);
+            }
             shared_flag = 0;
         }
         pthread_mutex_unlock (&mutex);
@@ -77,6 +106,9 @@ int test (dts_t *buffer, int nb)
     pthread_join (taker_thread, NULL);
 
     pthread_mutex_destroy (&mutex);
+    pthread_mutex_destroy (&event);
+
+    printf ("Too fast: %d\n", nbtoofast);
 
     return 0;
 }