#include <pthread.h>
#include <stdio.h>
+#include <unistd.h>
#include "mtime.h"
#include "test.h"
dts_t *deltas = NULL;
int nb_measurements = 0;
-char *message = "mutex";
+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 event = PTHREAD_MUTEX_INITIALIZER;
volatile int shared_flag = 0;
-ts_t timestamp1, timestamp2;
+ts_t ts1, ts2;
int try = 0;
+int nbtoofast = 0;
+#define TIMER 1000
void *mutex_giver_task (__attribute__((unused)) void *arg)
{
+
while (try < nb_measurements) {
pthread_mutex_lock (&mutex);
- sys_timestamp (×tamp1);
+ usleep (TIMER);
shared_flag = 1;
+ sys_timestamp (&ts1);
pthread_mutex_unlock (&mutex);
+ //shared_flag = 0;
+ usleep (TIMER);
}
pthread_exit (NULL);
}
void *mutex_taker_task (__attribute__((unused)) void *arg)
{
+ /* real-time process */
+/*
+ struct sched_param param = {0};
+ if (sched_getparam (0, ¶m) != 0) {
+ fprintf (stderr, "error: sched_getparam\n");
+ return 1;
+ }
+ param.sched_priority += 10;
+ int rc = sched_setscheduler (0, SCHED_FIFO, ¶m);
+ if (rc != 0) {
+ fprintf (stderr, "error: sched_setscheduler\n");
+ return 1;
+ }
+*/
+
while (try < nb_measurements) {
+ ts_t ts;
+ sys_timestamp (&ts);
pthread_mutex_lock (&mutex);
if (shared_flag) {
- sys_timestamp (×tamp2);
- deltas[try++] = diff_timestamp (×tamp2, ×tamp1);
+ sys_timestamp (&ts2);
+ if (diff_timestamp (&ts2, &ts) < (9 * TIMER / 10)) {
+ nbtoofast++;
+ } else {
+ deltas[try++] = diff_timestamp (&ts2, &ts1);
+ }
shared_flag = 0;
}
pthread_mutex_unlock (&mutex);
pthread_join (taker_thread, NULL);
pthread_mutex_destroy (&mutex);
+ pthread_mutex_destroy (&event);
+
+ printf ("Too fast: %d\n", nbtoofast);
return 0;
}