change initial mutex by a barrier
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Wed, 15 Oct 2025 17:20:49 +0000 (19:20 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Wed, 15 Oct 2025 17:20:49 +0000 (19:20 +0200)
mq_lat.c
pipe_lat.c
test.c
test.h
udp_lat.c

index 7a35124ea11d15c71303cb1fd06da514f6dae403..a89fbf4783d164e8ad38ffe82bc890effc3bd262 100644 (file)
--- a/mq_lat.c
+++ b/mq_lat.c
@@ -32,7 +32,8 @@ int (*parse_arg_ext) (char *) = NULL;
 #define MAXBUF 1024
 #define TIMER 1000
 
-pthread_mutex_t *mutex = NULL;
+pthread_barrier_t *barrier = NULL;
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;;
 ts_t ts1, ts2;
 int rc = 0;
 
@@ -48,17 +49,17 @@ void *ping (__attribute__((unused)) void *arg)
 
     /* main loop */
 
+    pthread_barrier_wait (barrier);
     printf ("Sending ping...\n");
 
-    pthread_mutex_unlock (mutex);
     usleep (TIMER);
 
     for (int i = 0; i < nb_measurements; i++) {
 
         char *msg = get_msg (MSGLEN);
 
-        pthread_mutex_lock (mutex);
-        pthread_mutex_unlock (mutex);
+        pthread_mutex_lock (&mutex);
+        pthread_mutex_unlock (&mutex);
 
         sys_timestamp (&ts1);
         if (mq_send (mq_out, msg, MSGLEN, 0) == -1) {
@@ -84,12 +85,11 @@ void *pong (__attribute__((unused)) void *arg)
         pthread_exit (NULL);
     }
 
-    printf ("Responding pong...\n");
+    pthread_barrier_wait (barrier);
+    printf ("Receiving pong...\n");
 
     for (int i = 0; i < nb_measurements; i++) {
 
-        pthread_mutex_unlock (mutex);
-
         char buffer[MAXBUF] = { 0 };
         if (mq_receive (mq_in, buffer, sizeof (buffer), NULL) == -1) {
             fprintf (stderr, "pong error: mq_receive (%d)\n", i);
@@ -100,8 +100,9 @@ void *pong (__attribute__((unused)) void *arg)
         sys_timestamp (&ts2);
         deltas[i] = diff_timestamp (&ts2, &ts1);
 
-        pthread_mutex_lock (mutex);
+        pthread_mutex_lock (&mutex);
         usleep (TIMER);
+        pthread_mutex_unlock (&mutex);
     }
 
     mq_close (mq_in);
@@ -109,14 +110,14 @@ void *pong (__attribute__((unused)) void *arg)
     pthread_exit (NULL);
 }
 
-int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
+int init (dts_t *buffer, int nb, pthread_barrier_t *synchro)
 {
 
     /* set global variables */
 
     deltas = buffer;
     nb_measurements = nb;
-    mutex = synchro;
+    barrier = synchro;
 
     /* open mq */
 
index a93630f1e0744ff32fa0f43da9cd5041ed6cadce..37030bdc7cfd9a072f0bb28b6f518f36ee8be196 100644 (file)
@@ -26,7 +26,8 @@ int (*parse_arg_ext) (char *) = NULL;
 #define MAXBUF 1024
 #define TIMER 1000
 
-pthread_mutex_t *mutex = NULL;
+pthread_barrier_t *barrier = NULL;
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;;
 ts_t ts1, ts2;
 int pipefd[2] = { 0 };
 int rc = 0;
@@ -39,17 +40,17 @@ void *ping (__attribute__((unused)) void *arg)
 
     /* main loop */
 
+    pthread_barrier_wait (barrier);
     printf ("Sending ping...\n");
 
-    pthread_mutex_unlock (mutex);
     usleep (TIMER);
 
     for (int i = 0; i < nb_measurements; i++) {
 
         char *msg = get_msg (MSGLEN);
 
-        pthread_mutex_lock (mutex);
-        pthread_mutex_unlock (mutex);
+        pthread_mutex_lock (&mutex);
+        pthread_mutex_unlock (&mutex);
 
         sys_timestamp (&ts1);
         if (write (fdout, msg, MSGLEN) == -1) {
@@ -72,12 +73,11 @@ void *pong (__attribute__((unused)) void *arg)
     int fdin = dup (pipefd[0]);
     int fdout = dup (pipefd[1]);
 
-    printf ("Responding pong...\n");
+    pthread_barrier_wait (barrier);
+    printf ("Receiving pong...\n");
 
     for (int i = 0; i < nb_measurements; i++) {
 
-        pthread_mutex_unlock (mutex);
-
         char buffer[MAXBUF] = { 0 };
         if (read (fdin, buffer, sizeof(buffer)) == -1) {
             fprintf (stderr, "pong error: read (%d)\n", i);
@@ -88,8 +88,9 @@ void *pong (__attribute__((unused)) void *arg)
         sys_timestamp (&ts2);
         deltas[i] = diff_timestamp (&ts2, &ts1);
 
-        pthread_mutex_lock (mutex);
+        pthread_mutex_lock (&mutex);
         usleep (TIMER);
+        pthread_mutex_unlock (&mutex);
     }
 
     close (fdin);
@@ -98,14 +99,14 @@ void *pong (__attribute__((unused)) void *arg)
     pthread_exit (NULL);
 }
 
-int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
+int init (dts_t *buffer, int nb, pthread_barrier_t *synchro)
 {
 
     /* set global variables */
 
     deltas = buffer;
     nb_measurements = nb;
-    mutex = synchro;
+    barrier = synchro;
 
     /* open pipe */
 
diff --git a/test.c b/test.c
index 9964ab8a676cf55cc69ceba7edaa739ad604ea2f..66d492f19736d4afa397a07d30ecd1db7e1fef23 100644 (file)
--- a/test.c
+++ b/test.c
@@ -206,30 +206,28 @@ int main (int argc, char *argv[])
     printf ("Test: %s\n", (message) ? message : "unknown");
     printf ("Dedicated core(s): %d\n", nb_cores);
 
-    pthread_mutex_t synchro = PTHREAD_MUTEX_INITIALIZER;
-    launch_param_t lparam = { 0 };
+    pthread_barrier_t synchro;
+    pthread_barrier_init (&synchro, NULL, 2);
 
     if (init (buffer, nb, &synchro)) {
         fprintf (stderr, "error on init\n");
         return 1;
     }
 
-    pthread_mutex_lock (&synchro);
-
     pthread_t tid1;
-    lparam.target = 0;
-    lparam.func = ping;
-    if (pthread_create (&tid1, NULL, launch, &lparam) != 0) {
+    launch_param_t lparam_t1 = { 0 };
+    lparam_t1.target = 0;
+    lparam_t1.func = ping;
+    if (pthread_create (&tid1, NULL, launch, &lparam_t1) != 0) {
         fprintf (stderr, "error on pthread_create\n");
         return 1;
     }
 
-    pthread_mutex_lock (&synchro);
-
     pthread_t tid2;
-    lparam.target = 1;
-    lparam.func = pong;
-    if (pthread_create (&tid2, NULL, launch, &lparam) != 0) {
+    launch_param_t lparam_t2 = { 0 };
+    lparam_t2.target = 1;
+    lparam_t2.func = pong;
+    if (pthread_create (&tid2, NULL, launch, &lparam_t2) != 0) {
         fprintf (stderr, "error on pthread_create\n");
         return 1;
     }
@@ -237,7 +235,7 @@ int main (int argc, char *argv[])
     pthread_join (tid1, NULL);
     pthread_join (tid2, NULL);
 
-    pthread_mutex_destroy (&synchro);
+    pthread_barrier_destroy (&synchro);
 
     if (finish ()) {
         printf ("\033[1;31mKO\033[0;0m\n");
diff --git a/test.h b/test.h
index dab521d37bcf91c9c1bf26b0fa72eb425eb68cc2..f34e2eb129ebda1aae34119294a3d9e59aa62cd4 100644 (file)
--- a/test.h
+++ b/test.h
@@ -7,7 +7,7 @@
 
 #include "mtime.h"
 
-int init (dts_t *buffer, int nb, pthread_mutex_t *synchro);
+int init (dts_t *buffer, int nb, pthread_barrier_t *synchro);
 
 void *ping (void *arg);
 
index 8b7e5619fc4552a15f70ab5d82209a21e4040a78..5762a5d0a848e5b0ae4f41a2a59ad35895a9f245 100644 (file)
--- a/udp_lat.c
+++ b/udp_lat.c
@@ -52,7 +52,8 @@ int (*parse_arg_ext) (char *) = _parse_arg_ext;
 #define MAXBUF 1024
 #define TIMER 1000
 
-pthread_mutex_t *mutex = NULL;
+pthread_barrier_t *barrier = NULL;
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;;
 ts_t ts1, ts2;
 int rc = 0;
 int localhost_ip = 0x7f000001;
@@ -83,17 +84,17 @@ void *ping (__attribute__((unused)) void *arg)
     remote.sin_port = htons (port);
     remote.sin_addr.s_addr = htonl (localhost_ip);
 
+    pthread_barrier_wait (barrier);
     printf ("Sending ping...\n");
 
-    pthread_mutex_unlock (mutex);
     usleep (TIMER);
 
     for (int i = 0; i < nb_measurements; i++) {
 
         char *msg = get_msg (MSGLEN);
 
-        pthread_mutex_lock (mutex);
-        pthread_mutex_unlock (mutex);
+        pthread_mutex_lock (&mutex);
+        pthread_mutex_unlock (&mutex);
 
         sys_timestamp (&ts1);
         if (sendto (sock, msg, MSGLEN, 0, (struct sockaddr *)&remote, sizeof (remote)) == -1) {
@@ -131,12 +132,11 @@ void *pong (__attribute__((unused)) void *arg)
         pthread_exit (NULL);
     }
 
-    printf ("Sending pong...\n");
+    pthread_barrier_wait (barrier);
+    printf ("Receiving pong...\n");
 
     for (int i = 0; i < nb_measurements; i++) {
 
-        pthread_mutex_unlock (mutex);
-
         char buffer[MAXBUF] = { 0 };
         struct sockaddr_in src = { 0 };
         socklen_t alen = sizeof (src);
@@ -148,8 +148,9 @@ void *pong (__attribute__((unused)) void *arg)
         sys_timestamp (&ts2);
         deltas[i] = diff_timestamp (&ts2, &ts1);
 
-        pthread_mutex_lock (mutex);
+        pthread_mutex_lock (&mutex);
         usleep (TIMER);
+        pthread_mutex_unlock (&mutex);
     }
 
     close (sock);
@@ -157,14 +158,14 @@ void *pong (__attribute__((unused)) void *arg)
     pthread_exit (NULL);
 }
 
-int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
+int init (dts_t *buffer, int nb, pthread_barrier_t *synchro)
 {
 
     /* set global variables */
 
     deltas = buffer;
     nb_measurements = nb;
-    mutex = synchro;
+    barrier = synchro;
 
     return 0;
 }