From 2df632afbb27264473e0f4e4028b5d2d2aa2f09c Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Wed, 15 Oct 2025 19:20:49 +0200 Subject: [PATCH] change initial mutex by a barrier --- mq_lat.c | 21 +++++++++++---------- pipe_lat.c | 21 +++++++++++---------- test.c | 24 +++++++++++------------- test.h | 2 +- udp_lat.c | 21 +++++++++++---------- 5 files changed, 45 insertions(+), 44 deletions(-) diff --git a/mq_lat.c b/mq_lat.c index 7a35124..a89fbf4 100644 --- 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 */ diff --git a/pipe_lat.c b/pipe_lat.c index a93630f..37030bd 100644 --- a/pipe_lat.c +++ b/pipe_lat.c @@ -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 9964ab8..66d492f 100644 --- 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 dab521d..f34e2eb 100644 --- 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); diff --git a/udp_lat.c b/udp_lat.c index 8b7e561..5762a5d 100644 --- 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; } -- 2.30.2