/* 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>
/* global variables */
-int64_t *deltas = NULL;
+nstime_t *deltas = NULL;
int nb_measurements = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
timestamp1 = sys_timestamp ();
pthread_mutex_unlock (&mutex);
}
- pthread_exit(NULL);
+ pthread_exit (NULL);
}
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)
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, ¶m);
- 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, ¶m);
+ pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED);
shared_flag = 1;
pthread_join (giver_thread, NULL);
pthread_join (taker_thread, NULL);
- pthread_mutex_destroy(&mutex);
+ pthread_mutex_destroy (&mutex);
return nsec_e;
}
--- /dev/null
+/* 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;
+}
+++ /dev/null
-//
-// 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;
-}