test cleaning
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Wed, 1 Oct 2025 17:03:43 +0000 (19:03 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Wed, 1 Oct 2025 17:03:43 +0000 (19:03 +0200)
mutex.c
semaphore.c

diff --git a/mutex.c b/mutex.c
index 7382a75886e25c2bf84f97e8f1329f7dc2db3310..39f7861eed88b701e81e6ead193de3bf02035694 100644 (file)
--- a/mutex.c
+++ b/mutex.c
@@ -18,7 +18,7 @@ void (*usage_ext) (FILE *) = NULL;
 int (*parse_arg_ext) (char *) = NULL;
 
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
-int shared_flag = 0;
+volatile int shared_flag = 0;
 ts_t timestamp1, timestamp2;
 int try = 0;
 
@@ -26,8 +26,8 @@ void *mutex_giver_task (__attribute__((unused)) void *arg)
 {
     while (try < nb_measurements) {
         pthread_mutex_lock (&mutex);
-        shared_flag = 1;
         sys_timestamp (&timestamp1);
+        shared_flag = 1;
         pthread_mutex_unlock (&mutex);
     }
     pthread_exit (NULL);
index 04eacf77dcf21b94c64667f39125e9d03e4d0760..4bcd736ce59f47aa6153b3eea4ebb6e41b3cfe61 100644 (file)
@@ -5,6 +5,7 @@
 #include <pthread.h>
 #include <semaphore.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "mtime.h"
 #include "test.h"
@@ -22,25 +23,27 @@ int (*parse_arg_ext) (char *) = NULL;
 int counter = 0;
 int try = 0;
 sem_t sem;
-ts_t ts1, ts2;
 
 void* trythis (__attribute__((unused)) void *arg)
 {
     sem_wait (&sem);
 
-    counter += 1;
+    static ts_t tsi;
+    ts_t ts;
+    sys_timestamp (&ts);
 
     /* thread 1 took the lock -> timestamp 1 */
-    if (counter % 2 != 0) {
-        sys_timestamp (&ts1);
+    if (counter % 2 == 0) {
+        set_timestamp (&tsi, &ts);
     }
 
     /* thread 2 took the lock -> timestamp 2 */
     else {
-        sys_timestamp (&ts2);
-        measure_tab[try++] = diff_timestamp (&ts2, &ts1);
+        measure_tab[try++] = diff_timestamp (&ts, &tsi);
     }
 
+    counter += 1;
+
     sem_post (&sem);
 
     return NULL;
@@ -59,6 +62,8 @@ int test (dts_t *buffer, int nb)
 
     for (int i = 0; i < nb; i++) {
 
+        //bzero (&tsi, sizeof (ts1));
+
         pthread_t tid1;
         if (pthread_create (&tid1, NULL, trythis, NULL) != 0) {
             fprintf (stderr, "error on pthread_create\n");