From 79c7191ffec5d85f15544667192ad98827b94037 Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Thu, 2 Oct 2025 18:18:46 +0200 Subject: [PATCH] rework on mutex test --- mutex.c | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/mutex.c b/mutex.c index 39f7861..666fcb5 100644 --- a/mutex.c +++ b/mutex.c @@ -4,6 +4,7 @@ #include #include +#include #include "mtime.h" #include "test.h" @@ -13,22 +14,29 @@ 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 (×tamp1); + 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, ¶m) != 0) { + fprintf (stderr, "error: sched_getparam\n"); + return 1; + } + param.sched_priority += 10; + int rc = sched_setscheduler (0, SCHED_FIFO, ¶m); + 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 (×tamp2); - deltas[try++] = diff_timestamp (×tamp2, ×tamp1); + 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; } -- 2.30.2