forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx9_example_3.c
More file actions
214 lines (174 loc) · 5.66 KB
/
x9_example_3.c
File metadata and controls
214 lines (174 loc) · 5.66 KB
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
204
205
206
207
208
209
210
211
212
213
214
/* x9_example_3.c
*
* Two producers and simultaneously consumers.
*
* ┌────────┐ ┏━━━━━━━━┓ ┌────────┐
* │Producer│──────▷┃inbox 1 ┃◁ ─ ─ ─│Producer│
* │ │ ┗━━━━━━━━┛ │ │
* │ and │ │ and │
* │ │ ┏━━━━━━━━┓ │ │
* │Consumer│─ ─ ─ ▷┃inbox 2 ┃◁──────│Consumer│
* └────────┘ ┗━━━━━━━━┛ └────────┘
*
* This example showcases the use of: 'x9_write_to_inbox'
* and 'x9_read_from_inbox', which, unlike 'x9_write_to_inbox_spin' and
* 'x9_read_from_inbox_spin' do not block until are able to write/read a msg.
*
* Data structures used:
* - x9_inbox
* - x9_node
*
* Functions used:
* - x9_create_inbox
* - x9_inbox_is_valid
* - x9_create_node
* - x9_node_is_valid
* - x9_select_inbox_from_node
* - x9_write_to_inbox
* - x9_read_from_inbox
* - x9_free_node_and_attached_inboxes
*
* Test is considered passed iff:
* - None of the threads stall and exit cleanly after doing the work.
* - All messages sent by the producer(s) are received and asserted to be
* valid by the consumer(s).
*/
#include <assert.h> /* assert */
#include <math.h> /* fabs */
#include <pthread.h> /* pthread_t, pthread functions */
#include <stdio.h> /* printf */
#include <stdlib.h> /* rand, RAND_MAX */
#include "../x9.h"
/* Both producer and consumer loops, would commonly be infinite loops, but for
* the purpose of testing a reasonable NUMBER_OF_MESSAGES is defined. */
#define NUMBER_OF_MESSAGES 1000000
typedef struct {
x9_node* node;
} th_struct;
typedef struct {
int a;
int b;
int sum;
} msg_type_1;
typedef struct {
double x;
double y;
double product;
} msg_type_2;
static inline int random_int(int const min, int const max)
{
return min + rand() / (RAND_MAX / (max - min + 1) + 1);
}
static inline double random_double(int const min, int const max)
{
return ((double)(random_int(min, max) * 1.0));
}
static inline void fill_msg_type_1(msg_type_1* const msg)
{
msg->a = random_int(0, 10);
msg->b = random_int(0, 10);
msg->sum = msg->a + msg->b;
}
static inline void fill_msg_type_2(msg_type_2* const msg)
{
msg->x = random_double(0, 10);
msg->y = random_double(0, 10);
msg->product = msg->x * msg->y;
}
static void* producer_1_fn(void* args)
{
th_struct* data = (th_struct*)args;
x9_inbox* const write_inbox = x9_select_inbox_from_node(data->node, "ibx_1");
assert(write_inbox);
x9_inbox* const read_inbox = x9_select_inbox_from_node(data->node, "ibx_2");
assert(read_inbox);
uint64_t msgs_read = {0};
uint64_t msgs_sent = {0};
msg_type_2 incoming_msg = {0};
msg_type_1 outgoing_msg = {0};
for (;;) {
if ((msgs_read == NUMBER_OF_MESSAGES) &&
(msgs_sent == NUMBER_OF_MESSAGES)) {
break;
}
if (msgs_sent != NUMBER_OF_MESSAGES) {
fill_msg_type_1(&outgoing_msg);
if (x9_write_to_inbox(write_inbox, sizeof(msg_type_1), &outgoing_msg)) {
++msgs_sent;
}
}
if (msgs_read != NUMBER_OF_MESSAGES) {
if (x9_read_from_inbox(read_inbox, sizeof(msg_type_2), &incoming_msg)) {
++msgs_read;
assert(fabs(incoming_msg.product - (incoming_msg.x * incoming_msg.y)) <
0.1);
}
}
}
return 0;
}
static void* producer_2_fn(void* args)
{
th_struct* data = (th_struct*)args;
x9_inbox* const write_inbox = x9_select_inbox_from_node(data->node, "ibx_2");
assert(write_inbox);
x9_inbox* const read_inbox = x9_select_inbox_from_node(data->node, "ibx_1");
assert(read_inbox);
uint64_t msgs_read = {0};
uint64_t msgs_sent = {0};
msg_type_1 incoming_msg = {0};
msg_type_2 outgoing_msg = {0};
for (;;) {
if ((msgs_read == NUMBER_OF_MESSAGES) &&
(msgs_sent == NUMBER_OF_MESSAGES)) {
break;
}
if (msgs_read != NUMBER_OF_MESSAGES) {
if (x9_read_from_inbox(read_inbox, sizeof(msg_type_1), &incoming_msg)) {
++msgs_read;
assert(incoming_msg.sum == (incoming_msg.a + incoming_msg.b));
}
}
if (msgs_sent != NUMBER_OF_MESSAGES) {
fill_msg_type_2(&outgoing_msg);
if (x9_write_to_inbox(write_inbox, sizeof(msg_type_2), &outgoing_msg)) {
++msgs_sent;
}
}
}
return 0;
}
int main(void)
{
/* Seed random generator */
srand((uint32_t)time(0));
/* Create inboxes */
x9_inbox* const inbox_msg_type_1 =
x9_create_inbox(4, "ibx_1", sizeof(msg_type_1));
x9_inbox* const inbox_msg_type_2 =
x9_create_inbox(4, "ibx_2", sizeof(msg_type_2));
/* Using asserts to simplify code for presentation purpose. */
assert(x9_inbox_is_valid(inbox_msg_type_1));
assert(x9_inbox_is_valid(inbox_msg_type_2));
/* Create node */
x9_node* const node =
x9_create_node("my_node", 2, inbox_msg_type_1, inbox_msg_type_2);
/* Asserts - Same reason as above.*/
assert(x9_node_is_valid(node));
/* Producer 1 (left on diagram) */
pthread_t producer_1_th = {0};
th_struct producer_1_struct = {.node = node};
/* Producer 2 (right on diagram) */
pthread_t producer_2_th = {0};
th_struct producer_2_struct = {.node = node};
/* Launch threads */
pthread_create(&producer_1_th, NULL, producer_1_fn, &producer_1_struct);
pthread_create(&producer_2_th, NULL, producer_2_fn, &producer_2_struct);
/* Join them */
pthread_join(producer_1_th, NULL);
pthread_join(producer_2_th, NULL);
/* Cleanup */
x9_free_node_and_attached_inboxes(node);
printf("TEST PASSED: x9_example_3.c\n");
return EXIT_SUCCESS;
}