/* depend: */
/* cflags: */
-/* linker: mtime.o test.o stat.o -lm -lrt */
+/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */
#include <stdio.h>
#include <stdlib.h>
int (*parse_arg_ext) (char *) = NULL;
#define MAXBUF 1024
+#define TIMER 1000
-ts_t ts1;
-ts_t ts2;
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+ts_t ts1, ts2;
+int try = 0;
int rc = 0;
void *ping (__attribute__((unused)) void *arg)
if (mq == -1) {
fprintf (stderr, "ping: mq_open\n");
rc = 1;
- return NULL;
+ pthread_exit (NULL);
}
- usleep (10 * 1000);
-
/* main loop */
printf ("Sending ping...\n");
- for (int i = 0; i < nb_measurements / 2; i++) {
- char buffer[MAXBUF] = { 0 };
- sprintf (buffer, "ping %d", i);
+ pthread_mutex_unlock (&mutex);
+ usleep (TIMER);
- sys_timestamp (&ts1);
+ while (try < nb_measurements) {
- if (mq_send (mq, buffer, strlen (buffer) + 1, 0) == -1) {
- fprintf (stderr, "ping error: mq_send (%d)\n", i);
- rc = 1;
- return NULL;
- }
- //printf ("send '%s'\n", buffer);
+ pthread_mutex_lock (&mutex);
+ pthread_mutex_unlock (&mutex);
- if (mq_receive (mq, buffer, sizeof(buffer), NULL) == -1) {
- fprintf (stderr, "ping error: mq_receive (%d)\n", i);
+ sys_timestamp (&ts1);
+ char *buffer = "ping";
+ if (mq_send (mq, buffer, strlen (buffer) + 1, 0) == -1) {
+ fprintf (stderr, "ping error: mq_send (%d)\n", try);
rc = 1;
- return NULL;
+ pthread_exit (NULL);
}
- //printf ("receive '%s'\n", buffer);
-
- sys_timestamp (&ts2);
- deltas[2 * i + 1] = diff_timestamp (&ts2, &ts1);
+ usleep (TIMER);
}
mq_close (mq);
- return 0;
+ pthread_exit (NULL);
}
void *pong (__attribute__((unused)) void *arg)
if (mq == -1) {
fprintf (stderr, "pong: mq_open\n");
rc = 1;
- return NULL;
+ pthread_exit (NULL);
}
/* main loop */
- for (int i = 0; i < nb_measurements / 2; i++) {
+ while (try < nb_measurements) {
- char buffer[MAXBUF] = { 0 };
+ pthread_mutex_unlock (&mutex);
+ char buffer[MAXBUF] = { 0 };
if (mq_receive (mq, buffer, sizeof(buffer), NULL) == -1) {
- fprintf (stderr, "pong error: mq_receive (%d)\n", i);
+ fprintf (stderr, "pong error: mq_receive (%d)\n", try);
rc = 1;
- return NULL;
+ pthread_exit (NULL);
}
- //printf ("receive '%s'\n", buffer);
-
sys_timestamp (&ts2);
- deltas[2 * i] = diff_timestamp (&ts2, &ts1);
-
- sys_timestamp (&ts1);
-
- if (mq_send (mq, buffer, strlen (buffer) + 1, 0) == -1) {
- fprintf (stderr, "pong error: mq_send (%d)\n", i);
- rc = 1;
- return NULL;
- }
- //printf ("send '%s'\n", buffer);
+ deltas[try++] = diff_timestamp (&ts2, &ts1);
+
+ pthread_mutex_lock (&mutex);
+ usleep (TIMER);
}
mq_close (mq);
- return NULL;
+ pthread_exit (NULL);
}
int test (dts_t *buffer, int nb)
/* mq latency 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");
pthread_join (tid1, NULL);
pthread_join (tid2, NULL);
-
+
+ pthread_mutex_destroy (&mutex);
+
return rc;
}