#define MAXBUF 1024
#define TIMER 1000
-pthread_mutex_t *mutex = NULL;
+pthread_barrier_t *barrier = NULL;
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;;
ts_t ts1, ts2;
int rc = 0;
/* main loop */
+ pthread_barrier_wait (barrier);
printf ("Sending ping...\n");
- pthread_mutex_unlock (mutex);
usleep (TIMER);
for (int i = 0; i < nb_measurements; i++) {
char *msg = get_msg (MSGLEN);
- pthread_mutex_lock (mutex);
- pthread_mutex_unlock (mutex);
+ pthread_mutex_lock (&mutex);
+ pthread_mutex_unlock (&mutex);
sys_timestamp (&ts1);
if (mq_send (mq_out, msg, MSGLEN, 0) == -1) {
pthread_exit (NULL);
}
- printf ("Responding pong...\n");
+ pthread_barrier_wait (barrier);
+ printf ("Receiving pong...\n");
for (int i = 0; i < nb_measurements; i++) {
- pthread_mutex_unlock (mutex);
-
char buffer[MAXBUF] = { 0 };
if (mq_receive (mq_in, buffer, sizeof (buffer), NULL) == -1) {
fprintf (stderr, "pong error: mq_receive (%d)\n", i);
sys_timestamp (&ts2);
deltas[i] = diff_timestamp (&ts2, &ts1);
- pthread_mutex_lock (mutex);
+ pthread_mutex_lock (&mutex);
usleep (TIMER);
+ pthread_mutex_unlock (&mutex);
}
mq_close (mq_in);
pthread_exit (NULL);
}
-int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
+int init (dts_t *buffer, int nb, pthread_barrier_t *synchro)
{
/* set global variables */
deltas = buffer;
nb_measurements = nb;
- mutex = synchro;
+ barrier = synchro;
/* open mq */
#define MAXBUF 1024
#define TIMER 1000
-pthread_mutex_t *mutex = NULL;
+pthread_barrier_t *barrier = NULL;
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;;
ts_t ts1, ts2;
int pipefd[2] = { 0 };
int rc = 0;
/* main loop */
+ pthread_barrier_wait (barrier);
printf ("Sending ping...\n");
- pthread_mutex_unlock (mutex);
usleep (TIMER);
for (int i = 0; i < nb_measurements; i++) {
char *msg = get_msg (MSGLEN);
- pthread_mutex_lock (mutex);
- pthread_mutex_unlock (mutex);
+ pthread_mutex_lock (&mutex);
+ pthread_mutex_unlock (&mutex);
sys_timestamp (&ts1);
if (write (fdout, msg, MSGLEN) == -1) {
int fdin = dup (pipefd[0]);
int fdout = dup (pipefd[1]);
- printf ("Responding pong...\n");
+ pthread_barrier_wait (barrier);
+ printf ("Receiving pong...\n");
for (int i = 0; i < nb_measurements; i++) {
- pthread_mutex_unlock (mutex);
-
char buffer[MAXBUF] = { 0 };
if (read (fdin, buffer, sizeof(buffer)) == -1) {
fprintf (stderr, "pong error: read (%d)\n", i);
sys_timestamp (&ts2);
deltas[i] = diff_timestamp (&ts2, &ts1);
- pthread_mutex_lock (mutex);
+ pthread_mutex_lock (&mutex);
usleep (TIMER);
+ pthread_mutex_unlock (&mutex);
}
close (fdin);
pthread_exit (NULL);
}
-int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
+int init (dts_t *buffer, int nb, pthread_barrier_t *synchro)
{
/* set global variables */
deltas = buffer;
nb_measurements = nb;
- mutex = synchro;
+ barrier = synchro;
/* open pipe */
printf ("Test: %s\n", (message) ? message : "unknown");
printf ("Dedicated core(s): %d\n", nb_cores);
- pthread_mutex_t synchro = PTHREAD_MUTEX_INITIALIZER;
- launch_param_t lparam = { 0 };
+ pthread_barrier_t synchro;
+ pthread_barrier_init (&synchro, NULL, 2);
if (init (buffer, nb, &synchro)) {
fprintf (stderr, "error on init\n");
return 1;
}
- pthread_mutex_lock (&synchro);
-
pthread_t tid1;
- lparam.target = 0;
- lparam.func = ping;
- if (pthread_create (&tid1, NULL, launch, &lparam) != 0) {
+ launch_param_t lparam_t1 = { 0 };
+ lparam_t1.target = 0;
+ lparam_t1.func = ping;
+ if (pthread_create (&tid1, NULL, launch, &lparam_t1) != 0) {
fprintf (stderr, "error on pthread_create\n");
return 1;
}
- pthread_mutex_lock (&synchro);
-
pthread_t tid2;
- lparam.target = 1;
- lparam.func = pong;
- if (pthread_create (&tid2, NULL, launch, &lparam) != 0) {
+ launch_param_t lparam_t2 = { 0 };
+ lparam_t2.target = 1;
+ lparam_t2.func = pong;
+ if (pthread_create (&tid2, NULL, launch, &lparam_t2) != 0) {
fprintf (stderr, "error on pthread_create\n");
return 1;
}
pthread_join (tid1, NULL);
pthread_join (tid2, NULL);
- pthread_mutex_destroy (&synchro);
+ pthread_barrier_destroy (&synchro);
if (finish ()) {
printf ("\033[1;31mKO\033[0;0m\n");
#include "mtime.h"
-int init (dts_t *buffer, int nb, pthread_mutex_t *synchro);
+int init (dts_t *buffer, int nb, pthread_barrier_t *synchro);
void *ping (void *arg);
#define MAXBUF 1024
#define TIMER 1000
-pthread_mutex_t *mutex = NULL;
+pthread_barrier_t *barrier = NULL;
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;;
ts_t ts1, ts2;
int rc = 0;
int localhost_ip = 0x7f000001;
remote.sin_port = htons (port);
remote.sin_addr.s_addr = htonl (localhost_ip);
+ pthread_barrier_wait (barrier);
printf ("Sending ping...\n");
- pthread_mutex_unlock (mutex);
usleep (TIMER);
for (int i = 0; i < nb_measurements; i++) {
char *msg = get_msg (MSGLEN);
- pthread_mutex_lock (mutex);
- pthread_mutex_unlock (mutex);
+ pthread_mutex_lock (&mutex);
+ pthread_mutex_unlock (&mutex);
sys_timestamp (&ts1);
if (sendto (sock, msg, MSGLEN, 0, (struct sockaddr *)&remote, sizeof (remote)) == -1) {
pthread_exit (NULL);
}
- printf ("Sending pong...\n");
+ pthread_barrier_wait (barrier);
+ printf ("Receiving pong...\n");
for (int i = 0; i < nb_measurements; i++) {
- pthread_mutex_unlock (mutex);
-
char buffer[MAXBUF] = { 0 };
struct sockaddr_in src = { 0 };
socklen_t alen = sizeof (src);
sys_timestamp (&ts2);
deltas[i] = diff_timestamp (&ts2, &ts1);
- pthread_mutex_lock (mutex);
+ pthread_mutex_lock (&mutex);
usleep (TIMER);
+ pthread_mutex_unlock (&mutex);
}
close (sock);
pthread_exit (NULL);
}
-int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
+int init (dts_t *buffer, int nb, pthread_barrier_t *synchro)
{
/* set global variables */
deltas = buffer;
nb_measurements = nb;
- mutex = synchro;
+ barrier = synchro;
return 0;
}