add semaphore test
authorLaurent Mazet <mazet@softndesign.org>
Fri, 26 Sep 2025 21:52:26 +0000 (23:52 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Fri, 26 Sep 2025 21:52:26 +0000 (23:52 +0200)
mutex.c
semaphore.c [new file with mode: 0644]
todo/sem_start_finish.c [deleted file]

diff --git a/mutex.c b/mutex.c
index 8c3db8d406e15f49ba96e69105236b3752962376..2afddc3b89befdfc8a8d1cb8008a3e7b23c838ed 100644 (file)
--- a/mutex.c
+++ b/mutex.c
@@ -1,6 +1,6 @@
 /* depend: */
 /* cflags: */
-/* linker: nstime.o test.o stat.o -lpthread -lm */
+/* linker: nstime.o test.o stat.o -lm -lpthread */
 
 #include <pthread.h>
 #include <stdio.h>
@@ -11,7 +11,7 @@
 
 /* global variables */
 
-int64_t *deltas = NULL;
+nstime_t *deltas = NULL;
 int nb_measurements = 0;
 
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -27,7 +27,7 @@ void *mutex_giver_task (__attribute__((unused)) void *arg)
         timestamp1 = sys_timestamp (); 
         pthread_mutex_unlock (&mutex);
     }
-    pthread_exit(NULL);
+    pthread_exit (NULL);
 }
 
 void *mutex_taker_task (__attribute__((unused)) void *arg)
@@ -40,10 +40,10 @@ void *mutex_taker_task (__attribute__((unused)) void *arg)
             deltas[try++] = timestamp2 - timestamp1;
             shared_flag = 0;
         }
-        pthread_mutex_unlock(&mutex);
+        pthread_mutex_unlock (&mutex);
     }
 
-    pthread_exit(NULL);
+    pthread_exit (NULL);
 }
 
 sec_t test (int64_t *buffer, int nb)
@@ -60,11 +60,11 @@ sec_t test (int64_t *buffer, int nb)
     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);
+    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;
     
@@ -81,7 +81,7 @@ sec_t test (int64_t *buffer, int nb)
     pthread_join (giver_thread, NULL);
     pthread_join (taker_thread, NULL);
 
-    pthread_mutex_destroy(&mutex);
+    pthread_mutex_destroy (&mutex);
 
     return nsec_e;
 }
diff --git a/semaphore.c b/semaphore.c
new file mode 100644 (file)
index 0000000..4a7e331
--- /dev/null
@@ -0,0 +1,65 @@
+/* depend: */
+/* cflags: */
+/* linker: mustime.o test.o stat.o -lm -lpthread */
+
+#include <pthread.h>
+#include <semaphore.h>
+
+#include "mustime.h"
+#include "stat.h"
+#include "test.h"
+
+#define NT 2
+
+mustime_t *measure_tab;
+
+int counter = 0;
+int try = 0;
+sem_t sem;
+mustime_t ts1, ts2;
+
+void* trythis (__attribute__((unused)) void *arg)
+{
+    sem_wait (&sem);
+
+    counter += 1;
+
+    /* thread 1 took the lock -> timestamp 1 */
+    if (counter % 2 != 0) {
+        ts1 = sys_timestamp ();
+    }
+  
+    /* thread 2 took the lock -> timestamp 2 */
+    else {
+        ts2 = sys_timestamp ();
+        measure_tab[try++] = ts2 - ts1;
+    }
+
+    sem_post (&sem);
+
+    return NULL;
+}
+
+sec_t test (int64_t *buffer, int nb)
+{
+
+    /* set global variables */
+
+    measure_tab = buffer;
+
+    /* main process */
+    sem_init (&sem, 0, 1);
+
+    for (int i = 0; i < nb; i++) {
+        pthread_t tid1, tid2;
+        pthread_create (&tid1, NULL, trythis, NULL);
+        pthread_create (&tid2, NULL, trythis, NULL);
+
+        pthread_join (tid1, NULL);
+        pthread_join (tid2, NULL);
+    }
+
+    sem_destroy (&sem);
+
+    return musec_e;
+}
diff --git a/todo/sem_start_finish.c b/todo/sem_start_finish.c
deleted file mode 100644 (file)
index d422bc1..0000000
+++ /dev/null
@@ -1,123 +0,0 @@
-//
-// Pthread synchronization with POSIX semaphore
-//
-
-
-#include <stdio.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <pthread.h>
-#include <libgen.h>
-#include <semaphore.h>
-
-#define NT 2
-
-static long *measure_tab;
-
-pthread_t tid[NT];
-int counter, i_loop;
-sem_t sem;
-long ts1, ts2;
-
-// SysTimestamp() emulation
-int64_t timespec_as_microseconds(struct timespec ts)
-{
-    long rv = ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
-    return rv;
-}
-
-long timespec_as_nanoseconds(struct timespec ts)
-{
-    long rv = ts.tv_sec * 1000000000 + ts.tv_nsec ;
-    return rv;
-}
-
-int64_t sysTimestamp()
-{
-  struct timespec ts;
-
-  clock_gettime (CLOCK_REALTIME, &ts);
-
-  return timespec_as_microseconds(ts);
-}
-
-void* trythis(void* arg)
-{
-  sem_wait(&sem);
-
-  counter += 1;
-
-  // thread 1 took the lock -> timestamp 1
-  if (counter % 2 != 0) {
-    ts1 = sysTimestamp();
-  }
-  
-  // thread 2 took the lock -> timestamp 2
-  else {
-    ts2 = sysTimestamp();
-    //    printf ("%ld\n", ts2-ts1);
-    measure_tab[i_loop++] = ts2 - ts1;
-  }
-
-  sem_post(&sem);
-
-  return NULL;
-}
-
-void usage (char *s)
-{
-  fprintf (stderr, "Usage: %s [-n <loops>]\n", s);
-  exit (1);
-}
-
-int main(int ac, char **av)
-{
-  register int i, nloop = 10;
-  char *cp, *progname = (char*)basename(av[0]);
-
-  while (--ac) {
-    if ((cp = *++av) == NULL)
-      break;
-    if (*cp == '-' && *++cp) {
-      switch(*cp) {
-      case 'n' :
-       nloop = atoi(*++av);
-       break;
-
-      default: 
-       usage(progname);
-       break;
-      }
-    }
-    else
-      break;
-  }
-
-  // Allocate the tab
-  measure_tab = (long *)calloc (nloop, sizeof(long));
-  if (measure_tab == NULL) {
-      perror ("calloc");
-      exit (1);
-  }
-
-  sem_init (&sem, 0, 1);
-
-  for (i = 0 ; i < nloop ; i++) {
-    for (int i = 0; i < NT; i++)
-      pthread_create(&tid[i], NULL, trythis, NULL);
-
-    for (int i = 0; i < NT; i++)
-      pthread_join(tid[i], NULL);
-  }
-
-  sem_destroy(&sem);
-
-  // Display the result
-  for (i = 0 ; i < i_loop ; i++)
-    printf ("%ld\n", measure_tab[i]);
-
-  free (measure_tab);
-       
-  return 0;
-}