-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
203 lines (157 loc) · 4.12 KB
/
main.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <errno.h>
#include <pthread.h>
#include "barrier.h"
#include "sq.h"
#include "t.h"
static unsigned long base_t;
/* stupid helper to establish a "base time" to make human reading of time easier */
static unsigned long _t(void)
{
static bool first = true;
time_t t;
time(&t);
if (first) {
base_t = t;
first = false;
}
return (unsigned long)t - base_t;
}
/* returns the current time in msec */
unsigned long now(void)
{
return _t() * 1000;
}
/* creates a timespec which is for some number of msec in the future of the current time */
void future_ts(struct timespec *ts_out, unsigned int msec)
{
time_t t;
time(&t);
ts_out->tv_sec = t;
ts_out->tv_nsec = msec * 1000000;
if (ts_out->tv_nsec > 1000000000) {
ts_out->tv_nsec -= 1000000000;
ts_out->tv_sec += 1;
}
}
/* returns a random number between 1 and max */
unsigned long rand_num(unsigned long max)
{
bool first = true;
if (first) {
srandom(now() + base_t);
first = false;
}
return 1 + (random() % max);
}
/* takes a received message and deals with it */
int process_msg(const char *tname, sq_elem_t *e)
{
if (e) {
fprintf(stderr, "[%-5s] %5ld rx \"%s\"\n", tname, now(), (char *)e->data);
if (e->flags & SQ_FLAG_FREE) {
free(e->data);
}
free(e);
}
return 0;
}
/* creates a new message and fills out the provided sq_elem_t struct */
sq_elem_t *generate_msg(sq_elem_t *dest_e, const char *tname, const char *s, int val)
{
int len;
char *buf;
const char *fmt = "[%-5s] %03d %s";
len = snprintf(NULL, 0, fmt, tname, val, s);
if ((buf = malloc(len)) == NULL) {
return NULL;
}
/*
* VOLATILE - we want sq_push() to make a copy of the data
* FREE - we malloc()'d the message data because this function might be called re-entrantly, so caller must free()
*/
dest_e->data = buf;
dest_e->dlen = len;
dest_e->flags = SQ_FLAG_VOLATILE | SQ_FLAG_FREE;
snprintf(buf, len + 1, fmt, tname, val, s);
return dest_e;
}
/*
* demo message loop function
* called by each thread in their own loop
* waits 100ms for anyone to send the thread a message
* processes any messages that were sent our way
* if it's time to transmit a message of our wn, do so
*/
int thread_msg_loop(thread_data_t *td)
{
int ret;
unsigned long t;
struct timespec ts;
bool did_something;
/* wait for a message to be published to our queue or a timeout */
pthread_mutex_lock(&td->nd_mtx);
future_ts(&ts, 100);
ret = pthread_cond_timedwait(&td->newdata, &td->nd_mtx, &ts);
t = now();
did_something = false;
if (ret != ETIMEDOUT) {
//fprintf(stderr, "[%-5s] %5ld cond_timedwait returned %d\n", td->name, t, ret);
}
/* condition var changed */
if (ret == 0) {
sq_elem_t *e;
do {
ret = sq_pop(td->q, &e);
if (ret == SQ_ERR_NO_ERROR) {
if (e) {
process_msg(td->name, e);
++td->num_rx;
}
} else if (ret != SQ_ERR_EMPTY) {
fprintf(stderr, "[%-5s] sq_pop returned %d\n", td->name, ret);
}
} while (ret == SQ_ERR_NO_ERROR);
did_something = true;
}
/* time to transmit? */
if (t > td->tx_time) {
sq_elem_t e;
fprintf(stderr, "[%-5s] %5ld tx\n", td->name, t);
if (generate_msg(&e, td->name, "hello", td->count)) {
if ((ret = sq_publish(td->list, &e)) != SQ_ERR_NO_ERROR) {
fprintf(stderr, "[%-5s] sq_publish returned %d\n", td->name, ret);
}
++td->count;
++td->num_tx;
}
if (e.flags & SQ_FLAG_FREE) {
free(e.data);
}
td->tx_time = t + rand_num(2500);
did_something = true;
}
if (did_something) {
fprintf(stderr, "[%-5s] (tx %d rx %d)\n", td->name, td->num_tx, td->num_rx);
}
pthread_mutex_unlock(&td->nd_mtx);
return 0;
}
int main(int argc, char **argv)
{
pthread_t t1, t2, t3;
pthread_barrier_t pb;
pthread_barrier_init(&pb, NULL, 3);
/* create the threads, passing each the barrier so they can all wait for each other to start up */
pthread_create(&t1, NULL, thread1, (void *)&pb);
pthread_create(&t2, NULL, thread2, (void *)&pb);
pthread_create(&t3, NULL, thread3, (void *)&pb);
/* wait for everyone to quit */
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
return 0;
}