prepare tests for affinity
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Tue, 7 Oct 2025 17:32:22 +0000 (19:32 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Tue, 7 Oct 2025 17:32:22 +0000 (19:32 +0200)
clock.c
cycle.c
mq_lat.c
mutex.c
pipe_lat.c
semaphore.c
test.c
test.h
thread_c+j.c
thread_c.c
udp_lat.c

diff --git a/clock.c b/clock.c
index 3a5a4b3b344d0c04c23875f97f2313312fd6eda2..b563f4268b79ab24e491fdbed992d0bd13ae3481 100644 (file)
--- a/clock.c
+++ b/clock.c
@@ -1,7 +1,8 @@
 /* depend: */
 /* cflags: */
-/* linker: mtime.o test.o stat.o -lm -lrt */
+/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */
 
+#include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -13,6 +14,7 @@
 
 dts_t *deltas = NULL;
 int nb_measurements = 0;
+pthread_mutex_t *mutex = NULL;
 
 char *message = "Clock accurency";
 void (*usage_ext) (FILE *) = NULL;
@@ -23,15 +25,22 @@ int try = 0;
 #define MAXDUR 1000
 #define MINDUR 100
 
-int test (dts_t *buffer, int nb)
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
 {
 
     /* set global variables */
 
     deltas = buffer;
     nb_measurements = nb;
+    mutex = synchro;
 
-    /* thread test */
+    return 0;
+}
+
+void *ping (__attribute__((unused)) void *arg)
+{
+
+    pthread_mutex_unlock (mutex);
 
     for (int i = 0; i < nb_measurements; i++) {
 
@@ -44,5 +53,16 @@ int test (dts_t *buffer, int nb)
         deltas[i] = diff_timestamp (&ts2, &ts1);
     }
 
+    pthread_exit (NULL);
+}
+
+void *pong (__attribute__((unused)) void *arg)
+{
+    pthread_mutex_unlock (mutex);
+    pthread_exit (NULL);
+}
+
+int finish ()
+{
     return 0;
 }
diff --git a/cycle.c b/cycle.c
index 5ebc8e7362879a07af7a5dd0b067e076a6a9073f..4f517d622e00e15aa034692b55fed4847f4bfc7a 100644 (file)
--- a/cycle.c
+++ b/cycle.c
@@ -1,7 +1,8 @@
 /* depend: */
 /* cflags: */
-/* linker: mtime.o test.o stat.o -lm -lrt */
+/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */
 
+#include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -13,6 +14,7 @@
 
 dts_t *deltas = NULL;
 int nb_measurements = 0;
+pthread_mutex_t *mutex = NULL;
 
 char *message = "Cycle latency";
 void (*usage_ext) (FILE *) = NULL;
@@ -23,15 +25,22 @@ int try = 0;
 #define MAXDUR 1000
 #define MINDUR 100
 
-int test (dts_t *buffer, int nb)
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
 {
 
     /* set global variables */
 
     deltas = buffer;
     nb_measurements = nb;
+    mutex = synchro;
 
-    /* thread test */
+    return 0;
+}
+
+void *ping (__attribute__((unused)) void *arg)
+{
+
+    pthread_mutex_unlock (mutex);
 
     for (int i = 0; i < nb_measurements; i++) {
 
@@ -44,5 +53,16 @@ int test (dts_t *buffer, int nb)
         deltas[i] = diff_timestamp (&ts2, &ts1) - duration;
     }
 
+    pthread_exit (NULL);
+}
+
+void *pong (__attribute__((unused)) void *arg)
+{
+    pthread_mutex_unlock (mutex);
+    pthread_exit (NULL);
+}
+
+int finish ()
+{
     return 0;
 }
index 4d62cbfc89239c252bd6a2d8a756a4536eeee329..7a35124ea11d15c71303cb1fd06da514f6dae403 100644 (file)
--- a/mq_lat.c
+++ b/mq_lat.c
@@ -2,6 +2,7 @@
 /* cflags: */
 /* linker: msg.o mtime.o test.o stat.o -lm -lpthread -lrt */
 
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -26,28 +27,21 @@ char *message = "Message queue latency";
 void (*usage_ext) (FILE *) = NULL;
 int (*parse_arg_ext) (char *) = NULL;
 
+#define QNAME "/test_queue"
 #define MSGLEN 128
 #define MAXBUF 1024
-#define TIMER 10000
+#define TIMER 1000
 
-pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t *mutex = NULL;
 ts_t ts1, ts2;
 int rc = 0;
 
 void *ping (__attribute__((unused)) void *arg)
 {
 
-    /* open mq */
-
-    struct mq_attr attr;
-    attr.mq_flags = 0;
-    attr.mq_maxmsg = 5;
-    attr.mq_msgsize = MAXBUF;
-    attr.mq_curmsgs = 0;
-
-    mqd_t mq = mq_open ("/test_queue", O_CREAT | O_RDWR, 0644, &attr);
-    if (mq == -1) {
-        fprintf (stderr, "ping: mq_open\n");
+    mqd_t mq_out = mq_open (QNAME, O_WRONLY);
+    if (mq_out == -1) {
+        fprintf (stderr, "ping error: mq_open\n");
         rc = 1;
         pthread_exit (NULL);
     }
@@ -56,19 +50,18 @@ void *ping (__attribute__((unused)) void *arg)
 
     printf ("Sending ping...\n");
 
-    pthread_mutex_unlock (&mutex);
+    pthread_mutex_unlock (mutex);
     usleep (TIMER);
 
     for (int i = 0; i < nb_measurements; i++) {
 
-        //char *msg = "ping";
         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, msg, strlen (msg) + 1, 0) == -1) {
+        if (mq_send (mq_out, msg, MSGLEN, 0) == -1) {
             fprintf (stderr, "ping error: mq_send (%d)\n", i);
             rc = 1;
             pthread_exit (NULL);
@@ -76,7 +69,7 @@ void *ping (__attribute__((unused)) void *arg)
         usleep (TIMER);
     }
 
-    mq_close (mq);
+    mq_close (mq_out);
 
     pthread_exit (NULL);
 }
@@ -84,79 +77,76 @@ void *ping (__attribute__((unused)) void *arg)
 void *pong (__attribute__((unused)) void *arg)
 {
 
-    /* open mq */
-
-    struct mq_attr attr;
-    attr.mq_flags = 0;
-    attr.mq_maxmsg = 5;
-    attr.mq_msgsize = MAXBUF;
-    attr.mq_curmsgs = 0;
-
-    mqd_t mq = mq_open ("/test_queue", O_CREAT | O_RDWR, 0644, &attr);
-    if (mq == -1) {
-        fprintf (stderr, "ping: mq_open\n");
+    mqd_t mq_in = mq_open (QNAME, O_RDONLY);
+    if (mq_in == -1) {
+        fprintf (stderr, "error pong: mq_open\n");
         rc = 1;
         pthread_exit (NULL);
     }
 
-    /* main loop */
-
     printf ("Responding pong...\n");
 
     for (int i = 0; i < nb_measurements; i++) {
 
-        pthread_mutex_unlock (&mutex);
+        pthread_mutex_unlock (mutex);
 
         char buffer[MAXBUF] = { 0 };
-        if (mq_receive (mq, buffer, sizeof (buffer) - 1, NULL) == -1) {
+        if (mq_receive (mq_in, buffer, sizeof (buffer), NULL) == -1) {
             fprintf (stderr, "pong error: mq_receive (%d)\n", i);
             rc = 1;
             pthread_exit (NULL);
         }
+
         sys_timestamp (&ts2);
         deltas[i] = diff_timestamp (&ts2, &ts1);
 
-        pthread_mutex_lock (&mutex);
+        pthread_mutex_lock (mutex);
         usleep (TIMER);
     }
 
-    mq_close (mq);
+    mq_close (mq_in);
 
     pthread_exit (NULL);
 }
 
-int test (dts_t *buffer, int nb)
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
 {
 
     /* set global variables */
 
     deltas = buffer;
     nb_measurements = nb;
+    mutex = synchro;
 
-    /* mq latency test */
+    /* open mq */
 
-    pthread_mutex_lock (&mutex);
+    struct mq_attr mq_attr = { 0 };
+    mq_attr.mq_flags = 0;
+    mq_attr.mq_maxmsg = 5;
+    mq_attr.mq_msgsize = MAXBUF;
+    mq_attr.mq_curmsgs = 0;
 
-    pthread_t tid1;
-    if (pthread_create (&tid1, NULL, ping, NULL) != 0) {
-        fprintf (stderr, "error on pthread_create\n");
+    if ((mq_unlink (QNAME) != 0) && errno != ENOENT) {
+        fprintf (stderr, "error: mq_unlink\n");
         return 1;
     }
-
-    pthread_mutex_lock (&mutex);
-
-    pthread_t tid2;
-    if (pthread_create (&tid2, NULL, pong, NULL) != 0) {
-        fprintf (stderr, "error on pthread_create\n");
+    mqd_t mq = mq_open (QNAME, O_CREAT | O_RDWR, S_IWUSR | S_IRUSR, &mq_attr);
+    if (mq == -1) {
+        fprintf (stderr, "error: mq_open\n");
         return 1;
     }
+    mq_close (mq);
 
-    pthread_join (tid1, NULL);
-    pthread_join (tid2, NULL);
+    return 0;
+}
 
-    mq_unlink ("/test_queue");
+int finish ()
+{
 
-    pthread_mutex_destroy (&mutex);
+    if ((mq_unlink (QNAME) != 0) && errno != ENOENT) {
+        fprintf (stderr, "error: mq_unlink\n");
+        return 1;
+    }
 
     return rc;
 }
diff --git a/mutex.c b/mutex.c
index 6dba569e3dc255a2a9d301b4a5003019b3010591..a9a5581e8c4f61c95d484a4a635427cb37b16142 100644 (file)
--- a/mutex.c
+++ b/mutex.c
@@ -18,7 +18,8 @@ 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 *mutex = NULL;
+pthread_mutex_t test = PTHREAD_MUTEX_INITIALIZER;
 volatile int shared_flag = 0;
 ts_t ts1, ts2;
 int try = 0;
@@ -26,43 +27,33 @@ int nbtoofast = 0;
 
 #define TIMER 1000
 
-void *mutex_giver_task (__attribute__((unused)) void *arg)
+void *ping (__attribute__((unused)) void *arg)
 {
 
+    pthread_mutex_unlock (mutex);
+
     while (try < nb_measurements) {
-        pthread_mutex_lock (&mutex);
+        pthread_mutex_lock (&test);
         usleep (TIMER);
         shared_flag = 1;
         sys_timestamp (&ts1);
-        pthread_mutex_unlock (&mutex);
+        pthread_mutex_unlock (&test);
         //shared_flag = 0;
         usleep (TIMER);
     }
+
     pthread_exit (NULL);
 }
 
-void *mutex_taker_task (__attribute__((unused)) void *arg)
+void *pong (__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;
-    }
-*/
+    pthread_mutex_unlock (mutex);
 
     while (try < nb_measurements) {
         ts_t ts;
         sys_timestamp (&ts);
-        pthread_mutex_lock (&mutex);
+        pthread_mutex_lock (&test);
         if (shared_flag) {
             sys_timestamp (&ts2);
             if (diff_timestamp (&ts2, &ts) < (9 * TIMER / 10)) {
@@ -72,42 +63,32 @@ void *mutex_taker_task (__attribute__((unused)) void *arg)
             }
             shared_flag = 0;
         }
-        pthread_mutex_unlock (&mutex);
+        pthread_mutex_unlock (&test);
     }
 
     pthread_exit (NULL);
 }
 
-int test (dts_t *buffer, int nb)
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
 {
 
     /* set global variables */
 
     deltas = buffer;
     nb_measurements = nb;
-
-    /* mutex test */
+    mutex = synchro;
 
     shared_flag = 1;
 
-    pthread_t giver_thread;
-    if (pthread_create (&giver_thread, NULL, mutex_giver_task, NULL) != 0) {
-        fprintf (stderr, "error on pthread_create for giver_task\n");
-        return 1;
-    }
-
-    pthread_t taker_thread;
-    if (pthread_create (&taker_thread, NULL, mutex_taker_task, NULL) != 0) {
-        fprintf (stderr, "error on pthread_create for taker_task\n");
-        return 1;
-    }
+    return 0;
+}
 
-    pthread_join (giver_thread, NULL);
-    pthread_join (taker_thread, NULL);
+int finish ()
+{
 
-    pthread_mutex_destroy (&mutex);
+    pthread_mutex_destroy (&test);
 
     printf ("Too fast: %d\n", nbtoofast);
 
-    return 0;
+    return nbtoofast;
 }
index 3d7aae0df55c2d380dc71aa48d3285049bfecd52..a93630f1e0744ff32fa0f43da9cd5041ed6cadce 100644 (file)
@@ -26,7 +26,7 @@ int (*parse_arg_ext) (char *) = NULL;
 #define MAXBUF 1024
 #define TIMER 1000
 
-pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t *mutex = NULL;
 ts_t ts1, ts2;
 int pipefd[2] = { 0 };
 int rc = 0;
@@ -41,15 +41,15 @@ void *ping (__attribute__((unused)) void *arg)
 
     printf ("Sending ping...\n");
 
-    pthread_mutex_unlock (&mutex);
+    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) {
@@ -76,11 +76,11 @@ void *pong (__attribute__((unused)) void *arg)
 
     for (int i = 0; i < nb_measurements; i++) {
 
-        pthread_mutex_unlock (&mutex);
+        pthread_mutex_unlock (mutex);
 
         char buffer[MAXBUF] = { 0 };
         if (read (fdin, buffer, sizeof(buffer)) == -1) {
-            fprintf (stderr, "ping error: read (%d)\n", i);
+            fprintf (stderr, "pong error: read (%d)\n", i);
             rc = 1;
             pthread_exit (NULL);
         }
@@ -88,7 +88,7 @@ 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);
     }
 
@@ -98,13 +98,14 @@ void *pong (__attribute__((unused)) void *arg)
     pthread_exit (NULL);
 }
 
-int test (dts_t *buffer, int nb)
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
 {
 
     /* set global variables */
 
     deltas = buffer;
     nb_measurements = nb;
+    mutex = synchro;
 
     /* open pipe */
 
@@ -113,31 +114,14 @@ int test (dts_t *buffer, int nb)
         return 1;
     }
 
-    /* pipe test */
-
-    pthread_mutex_lock (&mutex);
-
-    pthread_t tid1;
-    if (pthread_create (&tid1, NULL, ping, NULL) != 0) {
-        fprintf (stderr, "error on pthread_create\n");
-        return 1;
-    }
-
-    pthread_mutex_lock (&mutex);
-
-    pthread_t tid2;
-    if (pthread_create (&tid2, NULL, pong, NULL) != 0) {
-        fprintf (stderr, "error on pthread_create\n");
-        return 1;
-    }
+    return 0;
+}
 
-    pthread_join (tid1, NULL);
-    pthread_join (tid2, NULL);
+int finish ()
+{
 
     close (pipefd[0]);
     close (pipefd[1]);
 
-    pthread_mutex_destroy (&mutex);
-
     return rc;
 }
index ac0202c34f452ca1ffe32da185fa20440381df6c..21d28278940ba8b8d7189050a101305303190e20 100644 (file)
@@ -22,7 +22,7 @@ char *message = "Semaphore latency";
 void (*usage_ext) (FILE *) = NULL;
 int (*parse_arg_ext) (char *) = NULL;
 
-pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t *mutex = NULL;
 volatile int shared_flag = 0;
 ts_t ts1, ts2;
 sem_t sem;
@@ -31,9 +31,11 @@ int nbtoofast = 0;
 
 #define TIMER 1000
 
-void *semaphore_giver_task (__attribute__((unused)) void *arg)
+void *ping (__attribute__((unused)) void *arg)
 {
 
+    pthread_mutex_unlock (mutex);
+
     while (try < nb_measurements) {
         sem_wait (&sem);
         usleep (TIMER);
@@ -47,9 +49,11 @@ void *semaphore_giver_task (__attribute__((unused)) void *arg)
     pthread_exit (NULL);
 }
 
-void *semaphore_taker_task (__attribute__((unused)) void *arg)
+void *pong (__attribute__((unused)) void *arg)
 {
 
+    pthread_mutex_unlock (mutex);
+
     while (try < nb_measurements) {
         ts_t ts;
         sys_timestamp (&ts);
@@ -69,13 +73,14 @@ void *semaphore_taker_task (__attribute__((unused)) void *arg)
     pthread_exit (NULL);
 }
 
-int test (dts_t *buffer, int nb)
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
 {
 
     /* set global variables */
 
     deltas = buffer;
     nb_measurements = nb;
+    mutex = synchro;
 
     /* semaphore test */
 
@@ -83,24 +88,15 @@ int test (dts_t *buffer, int nb)
 
     shared_flag = 1;
 
-    pthread_t giver_thread;
-    if (pthread_create (&giver_thread, NULL, semaphore_giver_task, NULL) != 0) {
-        fprintf (stderr, "error on pthread_create for giver_task\n");
-        return 1;
-    }
-
-    pthread_t taker_thread;
-    if (pthread_create (&taker_thread, NULL, semaphore_taker_task, NULL) != 0) {
-        fprintf (stderr, "error on pthread_create for taker_task\n");
-        return 1;
-    }
+    return 0;
+}
 
-    pthread_join (giver_thread, NULL);
-    pthread_join (taker_thread, NULL);
+int finish ()
+{
 
     sem_destroy (&sem);
 
     printf ("Too fast: %d\n", nbtoofast);
 
-    return 0;
+    return nbtoofast;
 }
diff --git a/test.c b/test.c
index 838698fe0758dcb929fe2e6f162f2879cf89604a..c74d681812c42c632d5b3acdbcb6fd71da6690db 100644 (file)
--- a/test.c
+++ b/test.c
@@ -1,4 +1,5 @@
 #include <assert.h>
+#include <pthread.h>
 #include <sched.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -168,7 +169,35 @@ int main (int argc, char *argv[])
 
     printf ("Test: %s\n", (message) ? message : "unknown");
 
-    if (test (buffer, nb)) {
+    pthread_mutex_t synchro = PTHREAD_MUTEX_INITIALIZER;
+
+    if (init (buffer, nb, &synchro)) {
+        fprintf (stderr, "error on init\n");
+        return 1;
+    }
+
+    pthread_mutex_lock (&synchro);
+
+    pthread_t tid1;
+    if (pthread_create (&tid1, NULL, ping, NULL) != 0) {
+        fprintf (stderr, "error on pthread_create\n");
+        return 1;
+    }
+
+    pthread_mutex_lock (&synchro);
+
+    pthread_t tid2;
+    if (pthread_create (&tid2, NULL, pong, NULL) != 0) {
+        fprintf (stderr, "error on pthread_create\n");
+        return 1;
+    }
+
+    pthread_join (tid1, NULL);
+    pthread_join (tid2, NULL);
+
+    pthread_mutex_destroy (&synchro);
+
+    if (finish ()) {
         printf ("\033[1;31mKO\033[0;0m\n");
         return 1;
     } else {
diff --git a/test.h b/test.h
index 8faa05d08953a7c20bfd6e7cbcbc59815dcb5ad1..dab521d37bcf91c9c1bf26b0fa72eb425eb68cc2 100644 (file)
--- a/test.h
+++ b/test.h
@@ -3,8 +3,16 @@
 #ifndef __TEST_H__
 #define __TEST_H__
 
+#include <pthread.h>
+
 #include "mtime.h"
 
-int test (dts_t *buffer, int nb);
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro);
+
+void *ping (void *arg);
+
+void *pong (void *arg);
+
+int finish ();
 
 #endif /* __TEST_H__ */
index 6e1027231e64176fca6435531aeda5fbda61f0fe..5b752b0580b1e1071469b3d348a223d42a6c4629 100644 (file)
 
 dts_t *deltas = NULL;
 int nb_measurements = 0;
+pthread_mutex_t *mutex = NULL;
 
 char *message = "Thread (create and join) latency";
 void (*usage_ext) (FILE *) = NULL;
 int (*parse_arg_ext) (char *) = NULL;
 
+int rc = 0;
 
-void* dummy_thread (__attribute__((unused)) void *arg)
-{
-    pthread_exit (NULL);
-}
-
-int test (dts_t *buffer, int nb)
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
 {
 
     /* set global variables */
 
     deltas = buffer;
     nb_measurements = nb;
+    mutex = synchro;
+
+    return 0;
+}
+
+void* dummy_thread (__attribute__((unused)) void *arg)
+{
+    pthread_exit (NULL);
+}
+
+void *ping (__attribute__((unused)) void *arg)
+{
 
-    /* thread test */
+    pthread_mutex_unlock (mutex);
 
     for (int i = 0; i < nb_measurements; i++) {
 
@@ -41,7 +50,8 @@ int test (dts_t *buffer, int nb)
         pthread_t posix_t;
         if (pthread_create(&posix_t, NULL, dummy_thread, NULL) != 0) {
             fprintf (stderr, "error on pthread_create\n");
-            return 1;
+            rc = 1;
+            pthread_exit (NULL);
         }
 
         pthread_join(posix_t, NULL);
@@ -52,5 +62,16 @@ int test (dts_t *buffer, int nb)
         deltas[i] = diff_timestamp (&ts2, &ts1);
     }
 
-    return 0;
+    pthread_exit (NULL);
+}
+
+void *pong (__attribute__((unused)) void *arg)
+{
+    pthread_mutex_unlock (mutex);
+    pthread_exit (NULL);
+}
+
+int finish ()
+{
+    return rc;
 }
index d2371572e21d9290de3aecc38d1acac17d20d07b..637c526271f80a62cb5a7aef0bce6bf6ba9e59c2 100644 (file)
 
 dts_t *deltas = NULL;
 int nb_measurements = 0;
+pthread_mutex_t *mutex = NULL;
 
 char *message = "Thread (create) latency";
 void (*usage_ext) (FILE *) = NULL;
 int (*parse_arg_ext) (char *) = NULL;
 
 ts_t ts1, ts2;
-int try = 0;
+int rc = 0;
+
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
+{
+
+    /* set global variables */
+
+    deltas = buffer;
+    nb_measurements = nb;
+    mutex = synchro;
+
+    return 0;
+}
 
 void* dummy_thread (__attribute__((unused)) void *arg)
 {
@@ -26,15 +39,10 @@ void* dummy_thread (__attribute__((unused)) void *arg)
     pthread_exit (NULL);
 }
 
-int test (dts_t *buffer, int nb)
+void *ping (__attribute__((unused)) void *arg)
 {
 
-    /* set global variables */
-
-    deltas = buffer;
-    nb_measurements = nb;
-
-    /* thread test */
+    pthread_mutex_unlock (mutex);
 
     for (int i = 0; i < nb_measurements; i++) {
 
@@ -43,7 +51,8 @@ int test (dts_t *buffer, int nb)
         pthread_t posix_t;
         if (pthread_create(&posix_t, NULL, dummy_thread, NULL) != 0) {
             fprintf (stderr, "error on pthread_create\n");
-            return 1;
+            rc = 1;
+            pthread_exit (NULL);
         }
 
         pthread_join(posix_t, NULL);
@@ -51,5 +60,16 @@ int test (dts_t *buffer, int nb)
         deltas[i] = diff_timestamp (&ts2, &ts1);
     }
 
-    return 0;
+    pthread_exit (NULL);
+}
+
+void *pong (__attribute__((unused)) void *arg)
+{
+    pthread_mutex_unlock (mutex);
+    pthread_exit (NULL);
+}
+
+int finish ()
+{
+    return rc;
 }
index ab7499e639456a7f94ba7ab1b590b03557d56d33..3b47f8d0a0c3197d55b2503055b8f952b6ec479c 100644 (file)
--- a/udp_lat.c
+++ b/udp_lat.c
@@ -52,7 +52,7 @@ int (*parse_arg_ext) (char *) = _parse_arg_ext;
 #define MAXBUF 1024
 #define TIMER 1000
 
-pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t *mutex = NULL;
 ts_t ts1, ts2;
 int rc = 0;
 int localhost_ip = 0x7f000001;
@@ -85,15 +85,15 @@ void *ping (__attribute__((unused)) void *arg)
 
     printf ("Sending ping...\n");
 
-    pthread_mutex_unlock (&mutex);
+    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) {
@@ -135,7 +135,7 @@ void *pong (__attribute__((unused)) void *arg)
 
     for (int i = 0; i < nb_measurements; i++) {
 
-        pthread_mutex_unlock (&mutex);
+        pthread_mutex_unlock (mutex);
 
         char buffer[MAXBUF] = { 0 };
         if (recv (sock, buffer, MAXBUF, 0) == -1) {
@@ -146,7 +146,7 @@ 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);
     }
 
@@ -155,36 +155,19 @@ void *pong (__attribute__((unused)) void *arg)
     pthread_exit (NULL);
 }
 
-int test (dts_t *buffer, int nb)
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
 {
 
     /* set global variables */
 
     deltas = buffer;
     nb_measurements = nb;
+    mutex = synchro;
 
-    /* udp socket test */
-
-    pthread_mutex_lock (&mutex);
-
-    pthread_t tid1;
-    if (pthread_create (&tid1, NULL, ping, NULL) != 0) {
-        fprintf (stderr, "error on pthread_create\n");
-        return 1;
-    }
-
-    pthread_mutex_lock (&mutex);
-
-    pthread_t tid2;
-    if (pthread_create (&tid2, NULL, pong, NULL) != 0) {
-        fprintf (stderr, "error on pthread_create\n");
-        return 1;
-    }
-
-    pthread_join (tid1, NULL);
-    pthread_join (tid2, NULL);
-
-    pthread_mutex_destroy (&mutex);
+    return 0;
+}
 
+int finish ()
+{
     return rc;
 }