-
Notifications
You must be signed in to change notification settings - Fork 1
/
multi_thread.c
140 lines (118 loc) · 3.27 KB
/
multi_thread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* network latency measurer
* Copyright (C) 2018 Ricardo Biehl Pasquali <pasqualirb@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* 14/05/2018
*/
#include <poll.h> /* POLL* */
#include <signal.h> /* sigfillset() sigwait() */
#include <stdio.h> /* printf() */
#include "measurer_elements.h"
#include "receiver.h" /* receiver_thread_routine() */
#include "storer.h" /* storer_thread_routine() */
#include "sender.h" /* sender_thread_routine() */
#include "thread_context.h" /* struct thread_ctx */
static void
wait_for_signal(void)
{
sigset_t mask;
int sig = 0;
while (1) {
/* mask all signals and wait */
sigfillset(&mask);
sigwait(&mask, &sig);
switch (sig) {
case SIGINT:
case SIGQUIT:
return;
}
}
}
/*
* the sender waits for timer expiration
*
* ----------
*
* the storer waits for timestamps of packets we've sent
*
* We poll for messages in socket's error queue, where our
* send-timestamp will be returned in a control message
* (aka cmsg). Packet payload is also returned, allowing us
* to know the id to correlate with timestamp.
*
* ----------
*
* the receiver waits for packets from mirror
*
* NOTE: it can read packets before storer store their
* timestamps. A delay introduced before routine solves
* this problem. usleep(1000);
*/
#define set_and_goto(var, val, label) \
do { \
var = val; \
goto label; \
} while (0)
enum {
RECEIVER,
STORER,
SENDER,
LAST,
};
int
multithread_run(struct measurer_elements *e)
{
int ret = 0;
struct thread_ctx threads[LAST];
int i;
if (thread_context_setup(&threads[RECEIVER],
(void*) receiver_do_its_job, e->receiver,
e->receiver->sfd, POLLIN) == -1)
return 1;
if (thread_context_setup(&threads[STORER],
(void*) storer_do_its_job, e->storer,
e->storer->sfd, POLLPRI) == -1)
set_and_goto(ret, 1, _go_cleanup_receiver);
if (thread_context_setup(&threads[SENDER],
(void*) sender_do_its_job, e->sender,
e->sender->tfd, POLLIN) == -1)
set_and_goto(ret, 1, _go_cleanup_storer);
/* start threads */
for (i = 0; i < LAST; i++) {
if (thread_start(&threads[i]) == -1)
set_and_goto(ret, 1, _go_terminate_threads);
}
/* start sender timer fd */
sender_timer_start(e->sender);
printf("All threads started\n");
/* wait for signal, then proceed exiting */
wait_for_signal();
printf("Terminating threads\n");
_go_terminate_threads:
/* wake up and wait for all threads to terminate */
while (i--) {
if (thread_terminate(&threads[i]))
ret = 1;
}
//_go_cleanup_sender:
thread_context_cleanup(&threads[SENDER]);
_go_cleanup_storer:
thread_context_cleanup(&threads[STORER]);
_go_cleanup_receiver:
thread_context_cleanup(&threads[RECEIVER]);
return ret;
}