clean shedulater and timestamp management
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 29 Sep 2025 09:54:16 +0000 (11:54 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 29 Sep 2025 09:54:16 +0000 (11:54 +0200)
mq.c
mtime.c
mtime.h
mutex.c
pipe.c
semaphore.c
test.c
thread.c

diff --git a/mq.c b/mq.c
index 7e07f891b6478949996b9ac3a9f372b3a1c11236..b29fd90f43d877e62cc2625c2efcfa8d864f76aa 100644 (file)
--- a/mq.c
+++ b/mq.c
@@ -6,8 +6,6 @@
 #include <stdlib.h>
 #include <string.h>
 
-//#include <pthread.h>
-
 #include <fcntl.h>           /* For O_* constants */
 #include <sys/stat.h>        /* For mode constants */
 #include <mqueue.h>
@@ -34,11 +32,10 @@ sec_t receiver (void)
 {
 
     printf ("Waiting for data...\n");
-    nstime_t ts = 0, ts_old;
     for (int i = 0; i < nb_measurements; i++) {
-        ts_old = ts;
-        ts = sys_timestamp_ns();
-    
+        mtime_t ts1;
+        sys_timestamp (&ts1);
+
         char buffer[MAXBUF] = { 0 };
         int m = mq_receive (mq, buffer, sizeof(buffer), NULL);
         if (m == -1) {
@@ -47,10 +44,12 @@ sec_t receiver (void)
         }
         //printf ("receive '%s'\n", buffer);
 
-        deltas[i] = ts - ts_old;
+        mtime_t ts2;
+        sys_timestamp (&ts2);
+        deltas[i] = diff_timestamp (&ts2, &ts1);
     }
 
-    return usec_e;
+    return nsec_e;
 }
 
 sec_t sender (void)
@@ -83,24 +82,13 @@ sec_t test (int64_t *buffer, int nb)
 
     /* mutex test */
 
-/*
-    pthread_attr_t th_attr;
-    struct sched_param param;
-
-    pthread_attr_init (&th_attr);
-    pthread_attr_setschedpolicy (&th_attr, SCHED_FIFO);
-    param.sched_priority = sched_get_priority_max (SCHED_FIFO);
-    pthread_attr_setschedparam (&th_attr, &param);
-    pthread_attr_setinheritsched (&th_attr, PTHREAD_EXPLICIT_SCHED);
-*/
-
-    struct mq_attr mq_attr;
-    mq_attr.mq_flags = 0;
-    mq_attr.mq_maxmsg = 5;
-    mq_attr.mq_msgsize = MAXBUF;
-    mq_attr.mq_curmsgs = 0;
+    struct mq_attr attr;
+    attr.mq_flags = 0;
+    attr.mq_maxmsg = 5;
+    attr.mq_msgsize = MAXBUF;
+    attr.mq_curmsgs = 0;
 
-    mq = mq_open ("/test_queue", O_CREAT | O_RDWR, 0644, &mq_attr);
+    mq = mq_open ("/test_queue", O_CREAT | O_RDWR, 0644, &attr);
     if (mq == -1) {
         fprintf (stderr, "error: mq_open\n");
         return none_e;
diff --git a/mtime.c b/mtime.c
index f895c4c20808af11364e2c98f2bcb1bc5814aa51..e4ceb8cbd664dd9647fbbe7f749b7d763d115d22 100644 (file)
--- a/mtime.c
+++ b/mtime.c
@@ -2,23 +2,19 @@
 
 #include "mtime.h"
 
-mstime_t sys_timestamp_ms (void)
+void sys_timestamp (mtime_t *ts)
 {
-    struct timespec ts;
-    clock_gettime (CLOCK_REALTIME, &ts);
-    return ts.tv_sec * 1000 + ts.tv_nsec / 10000000 ;
+    clock_gettime (CLOCK_REALTIME, ts);
 }
 
-ustime_t sys_timestamp_us (void)
+unsigned int diff_timestamp  (mtime_t *ts1, mtime_t *ts2)
 {
-    struct timespec ts;
-    clock_gettime (CLOCK_REALTIME, &ts);
-    return ts.tv_sec * 1000000 + ts.tv_nsec / 1000 ;
+    return  (ts1->tv_sec - ts2->tv_sec) * 1000000000 +
+             ts1->tv_nsec - ts2->tv_nsec;
 }
 
-nstime_t sys_timestamp_ns (void)
+void set_timestamp  (mtime_t *ts1, mtime_t *ts2)
 {
-    struct timespec ts;
-    clock_gettime (CLOCK_REALTIME, &ts);
-    return ts.tv_sec * 1000000000 + ts.tv_nsec;
+    ts1->tv_sec = ts2->tv_sec;
+    ts1->tv_nsec = ts2->tv_nsec;
 }
diff --git a/mtime.h b/mtime.h
index b90fda719feec19e35a641986e7ee31bd20ed27a..aedd6ffd6c74f9cf9cae38b105f5e91624e2323d 100644 (file)
--- a/mtime.h
+++ b/mtime.h
@@ -5,16 +5,18 @@
 
 #include "inttypes.h"
 
-typedef int64_t mstime_t;
+typedef unsigned int mstime_t;
 
-mstime_t sys_timestamp_ms (void);
+typedef unsigned int ustime_t;
 
-typedef int64_t ustime_t;
+typedef unsigned int nstime_t;
 
-ustime_t sys_timestamp_us (void);
+typedef struct timespec mtime_t;
 
-typedef int64_t nstime_t;
+void sys_timestamp (mtime_t *ts);
 
-nstime_t sys_timestamp_ns (void);
+unsigned int diff_timestamp  (mtime_t *ts1, mtime_t *ts2);
+
+void set_timestamp  (mtime_t *ts1, mtime_t *ts2);
 
 #endif /* __MTIME_H__ */
diff --git a/mutex.c b/mutex.c
index 937937082400d6be474eaf2a67cd352c8c3256be..3701b74a6401748c9819d3070b2e21707d4865e2 100644 (file)
--- a/mutex.c
+++ b/mutex.c
@@ -1,6 +1,6 @@
 /* depend: */
 /* cflags: */
-/* linker: mtime.o test.o stat.o -lm -lpthread */
+/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */
 
 #include <pthread.h>
 #include <stdio.h>
@@ -17,7 +17,7 @@ char *message = "mutex";
 
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 int shared_flag = 0;
-nstime_t timestamp1, timestamp2;
+mtime_t timestamp1, timestamp2;
 int try = 0;
 
 void *mutex_giver_task (__attribute__((unused)) void *arg)
@@ -25,7 +25,7 @@ void *mutex_giver_task (__attribute__((unused)) void *arg)
     while (try < nb_measurements) {
         pthread_mutex_lock (&mutex);
         shared_flag = 1;
-        timestamp1 = sys_timestamp_ns (); 
+        sys_timestamp (&timestamp1);
         pthread_mutex_unlock (&mutex);
     }
     pthread_exit (NULL);
@@ -37,8 +37,8 @@ void *mutex_taker_task (__attribute__((unused)) void *arg)
     while (try < nb_measurements) {
         pthread_mutex_lock (&mutex);
         if (shared_flag) {
-            timestamp2 = sys_timestamp_ns ();
-            deltas[try++] = timestamp2 - timestamp1;
+            sys_timestamp (&timestamp2);
+            deltas[try++] = diff_timestamp (&timestamp2, &timestamp1);
             shared_flag = 0;
         }
         pthread_mutex_unlock (&mutex);
@@ -57,24 +57,16 @@ sec_t test (int64_t *buffer, int nb)
 
     /* mutex test */
 
-    pthread_t giver_thread, taker_thread;
-    pthread_attr_t attr;
-    struct sched_param param;
-
-    pthread_attr_init (&attr);
-    pthread_attr_setschedpolicy (&attr, SCHED_FIFO);
-    param.sched_priority = sched_get_priority_max (SCHED_FIFO);
-    pthread_attr_setschedparam (&attr, &param);
-    pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED);
-
     shared_flag = 1;
-    
-    if (pthread_create (&giver_thread, &attr, mutex_giver_task, NULL) != 0) {
+
+    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 none_e;
     }
 
-    if (pthread_create (&taker_thread, &attr, mutex_taker_task, NULL) != 0) {
+    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 none_e;
     }
diff --git a/pipe.c b/pipe.c
index 4abbf26f4472e2a6206030f8feec850a61b684b5..f1c7e00544d5b264f1374bd2d1b48bd609dcafdc 100644 (file)
--- a/pipe.c
+++ b/pipe.c
@@ -1,13 +1,11 @@
 /* depend: */
 /* cflags: */
-/* linker: mtime.o test.o stat.o -lm */
+/* linker: mtime.o test.o stat.o -lm -lrt */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-//#include <pthread.h>
-
 #include <signal.h>
 #include <sys/wait.h>
 #include <unistd.h>
@@ -28,11 +26,10 @@ sec_t receiver (int fd)
 {
 
     printf ("Waiting for data...\n");
-    nstime_t ts = 0, ts_old;
     for (int i = 0; i < nb_measurements; i++) {
-        ts_old = ts;
-        ts = sys_timestamp_ns();
-    
+        mtime_t ts1;
+        sys_timestamp (&ts1);
+
         char buffer[MAXBUF] = { 0 };
         if (read (fd, buffer, sizeof(buffer)) == -1) {
             fprintf (stderr, "error: read (%d)\n", i);
@@ -40,7 +37,9 @@ sec_t receiver (int fd)
         }
         //printf ("receive '%s'\n", buffer);
 
-        deltas[i] = ts - ts_old;
+        mtime_t ts2;
+        sys_timestamp (&ts2);
+        deltas[i] = diff_timestamp (&ts2, &ts1);
     }
 
     return usec_e;
@@ -73,18 +72,7 @@ sec_t test (int64_t *buffer, int nb)
     deltas = buffer;
     nb_measurements = nb;
 
-    /* mutex test */
-
-/*
-    pthread_attr_t th_attr;
-    struct sched_param param;
-
-    pthread_attr_init (&th_attr);
-    pthread_attr_setschedpolicy (&th_attr, SCHED_FIFO);
-    param.sched_priority = sched_get_priority_max (SCHED_FIFO);
-    pthread_attr_setschedparam (&th_attr, &param);
-    pthread_attr_setinheritsched (&th_attr, PTHREAD_EXPLICIT_SCHED);
-*/
+    /* pipe test */
 
     int pipefd[2] = { 0 };
 
index 044fb8c91ba4790bcef1ad81d1a97b867d0111ab..6c2fecc5515993260ac3c363df11437cacf58afb 100644 (file)
@@ -1,6 +1,6 @@
 /* depend: */
 /* cflags: */
-/* linker: mtime.o test.o stat.o -lm -lpthread */
+/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */
 
 #include <pthread.h>
 #include <semaphore.h>
 
 /* global variables */
 
-ustime_t *measure_tab = NULL;
+mstime_t *measure_tab = NULL;
 
 char *message = "semaphore";
 
 int counter = 0;
 int try = 0;
 sem_t sem;
-ustime_t ts1, ts2;
+mtime_t ts1, ts2;
 
 void* trythis (__attribute__((unused)) void *arg)
 {
@@ -30,13 +30,13 @@ void* trythis (__attribute__((unused)) void *arg)
 
     /* thread 1 took the lock -> timestamp 1 */
     if (counter % 2 != 0) {
-        ts1 = sys_timestamp_us ();
+        sys_timestamp (&ts1);
     }
-  
+
     /* thread 2 took the lock -> timestamp 2 */
     else {
-        ts2 = sys_timestamp_us ();
-        measure_tab[try++] = ts2 - ts1;
+        sys_timestamp (&ts2);
+        measure_tab[try++] = diff_timestamp (&ts2, &ts1);
     }
 
     sem_post (&sem);
@@ -56,12 +56,14 @@ sec_t test (int64_t *buffer, int nb)
     sem_init (&sem, 0, 1);
 
     for (int i = 0; i < nb; i++) {
-        pthread_t tid1, tid2;
+
+        pthread_t tid1;
         if (pthread_create (&tid1, NULL, trythis, NULL) != 0) {
             fprintf (stderr, "error on pthread_create\n");
             return none_e;
         }
-        
+
+        pthread_t tid2;
         if (pthread_create (&tid2, NULL, trythis, NULL) != 0) {
             fprintf (stderr, "error on pthread_create\n");
             return none_e;
@@ -73,5 +75,5 @@ sec_t test (int64_t *buffer, int nb)
 
     sem_destroy (&sem);
 
-    return usec_e;
+    return msec_e;
 }
diff --git a/test.c b/test.c
index dce0ef46a14f5670355ea1baab54c1d281424a41..d7847f5dba0cb41b8f85304ffd4f1bf58a37e458 100644 (file)
--- a/test.c
+++ b/test.c
@@ -1,5 +1,6 @@
 #include <assert.h>
 #include <inttypes.h>
+#include <sched.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -7,6 +8,10 @@
 #include "stat.h"
 #include "test.h"
 
+/* constants */
+
+#define RT_PRIO 80
+
 /* static variables */
 
 char *progname = NULL;
@@ -116,6 +121,20 @@ int main (int argc, char *argv[])
         }
     }
 
+    /* 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 = RT_PRIO;
+    int rc = sched_setscheduler (0, SCHED_FIFO, &param); /* non-preemptive */
+    // int rc = sched_setscheduler (0, SCHED_RR, &param); /* preemptive */
+    if (rc != 0) {
+        fprintf (stderr, "error: sched_setscheduler\n");
+        return 1;
+    }
+
     /* main process */
 
     int64_t *buffer = (int64_t *) calloc (nb, sizeof (int64_t));
@@ -127,7 +146,7 @@ int main (int argc, char *argv[])
 
     if (sec_unit == none_e) {
         printf ("\033[1;31mKO\033[0;0m\n");
-        exit (1);
+        return 1;
     } else {
         printf ("\033[1;32mOK\033[0;0m\n");
     }
index 53142fed82d7e77c85747204bb58c42bede4757e..2f6c172bdbc49f5f8bcdb7fabfc633885dd148ea 100644 (file)
--- a/thread.c
+++ b/thread.c
@@ -1,6 +1,6 @@
 /* depend: */
 /* cflags: */
-/* linker: mtime.o test.o stat.o -lm -lpthread */
+/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */
 
 #include <pthread.h>
 #include <stdio.h>
@@ -10,7 +10,7 @@
 
 /* global variables */
 
-ustime_t *deltas = NULL;
+nstime_t *deltas = NULL;
 int nb_measurements = 0;
 
 char *message = "thread";
@@ -30,33 +30,24 @@ sec_t test (int64_t *buffer, int nb)
 
     /* thread test */
 
-    pthread_t posix_t;
-    pthread_attr_t attr;
-    struct sched_param param;
-
-    pthread_attr_init (&attr);
-    pthread_attr_setschedpolicy (&attr, SCHED_FIFO);
-    param.sched_priority = sched_get_priority_max (SCHED_FIFO);
-    pthread_attr_setschedparam (&attr, &param);
-    pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED);
-
     for (int i = 0; i < nb_measurements; i++) {
 
-        ustime_t timestamp1 = sys_timestamp_us (); 
+        mtime_t ts1;
+        sys_timestamp (&ts1);
 
-        if (pthread_create(&posix_t, &attr, dummy_thread, NULL) != 0) {
+        pthread_t posix_t;
+        if (pthread_create(&posix_t, NULL, dummy_thread, NULL) != 0) {
             fprintf (stderr, "error on pthread_create\n");
             return none_e;
         }
 
         pthread_join(posix_t, NULL);
 
-        ustime_t timestamp2 = sys_timestamp_us (); 
+        mtime_t ts2;
+        sys_timestamp (&ts2);
 
-        deltas[i++] = timestamp2 - timestamp1;
+        deltas[i++] = diff_timestamp (&ts2, &ts1);
     }
 
-    pthread_attr_destroy(&attr);
-
-    return usec_e;
+    return nsec_e;
 }