-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmx_msgq.c
More file actions
executable file
·473 lines (349 loc) · 10 KB
/
Copy pathmx_msgq.c
File metadata and controls
executable file
·473 lines (349 loc) · 10 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include <pthread.h>
#include <stdio.h>
#include <assert.h>
#ifndef MODEX
#include <stdatomic.h>
#endif
// verify -p -E 7_q_update.c # using embedded printfs
// or:
// modex -run 7_q_update.c; verify replay
//
// illustrates importing a data structure
// and defining an atomic function (cas)
// both are done with a .prx file
#define NUM_MSGS 5
#define INDEX_INVALID 0xffffffff
#define CONSUMED_FLAG 0x80000000
#define FIRST_FLAG 0x40000000
#define INDEX_MASK 0x3fffffff
#define ORIGIN_MASK CONSUMED_FLAG
#define CONSUME_RESULT_ERROR -2
#define CONSUME_RESULT_NO_MSG -1
#define CONSUME_RESULT_NO_UPDATE 0
#define CONSUME_RESULT_SUCCESS 1
#define CONSUME_RESULT_DISCARDED 2
#define PRODUCE_RESULT_ERROR -2
#define PRODUCE_RESULT_FAIL -1
#define PRODUCE_RESULT_SUCCESS 1
#define PRODUCE_RESULT_DISCARDED 2
typedef struct msgq {
unsigned *tail;
unsigned *head;
unsigned *chain;
} msgq_t;
typedef struct producer {
msgq_t msgq;
unsigned head; /* last message in chain that can be used by consumer, chain[head] is always INDEX_INVALID */
unsigned current; /* message used by producer, will become head */
unsigned overrun; /* message used by consumer when tail moved away by producer, will become current when released by consumer */
} producer_t;
typedef struct consumer {
msgq_t msgq;
unsigned current;
} consumer_t;
unsigned g_producer_current = INDEX_INVALID;
unsigned g_consumer_current = INDEX_INVALID;
unsigned g_msgq_shm_tail = INDEX_INVALID;
unsigned g_msgq_shm_head = INDEX_INVALID;
unsigned g_msgq_shm_chain[NUM_MSGS];
unsigned int g_shm_msgs[NUM_MSGS];
void enqueue_first_msg(producer_t *producer)
{
msgq_t *msgq;
msgq = &producer->msgq;
/* current message is the new end of chain*/
msgq->chain[producer->current] = INDEX_INVALID;
*msgq->tail = producer->current | FIRST_FLAG;
producer->head = producer->current;
/* announce the new head for consumer_get_head */
*msgq->head = producer->head;
}
/* set the current message as head */
void enqueue_msg(producer_t *producer)
{
msgq_t *msgq;
msgq = &producer->msgq;
/* current message is the new end of chain*/
msgq->chain[producer->current] = INDEX_INVALID;
/* append current message to the chain */
msgq->chain[producer->head] = producer->current;
producer->head = producer->current;
/* announce the new head for consumer_get_head */
*msgq->head = producer->head;
}
int producer_move_tail(producer_t *producer, unsigned tail)
{
msgq_t *msgq;
unsigned next;
int b;
msgq = &producer->msgq;
next = msgq->chain[tail & INDEX_MASK];
b = atomic_compare_exchange_strong(msgq->tail, &tail, next);
return b;
}
/* try to jump over tail blocked by consumer */
int producer_overrun(producer_t *producer, unsigned tail)
{
int b;
msgq_t *msgq;
unsigned new_current, new_tail, expected;
msgq = &producer->msgq;
new_current = msgq->chain[tail & INDEX_MASK]; /* next */
new_tail = msgq->chain[new_current]; /* after next */
/* if atomic_compare_exchange_strong fails expected will be overwritten */
expected = tail;
b = atomic_compare_exchange_strong(msgq->tail, &expected, new_tail);
if (b) {
producer->current = new_current;
producer->overrun = tail & INDEX_MASK;
} else {
/* consumer just released tail, so use it */
producer->current = tail & INDEX_MASK;
}
return b;
}
/* inserts the current message into the queue and
* if the queue is full, discard the last message that is not
* used by consumer. Returns pointer to new message */
int producer_force_push(producer_t *producer)
{
msgq_t *msgq;
msgq = &producer->msgq;
unsigned next, tail;
int consumed, full;
int discarded;
discarded = 0;
if (producer->current == INDEX_INVALID) {
producer->current = 0;
return producer->current;
}
next = msgq->chain[producer->current];
if (producer->head == INDEX_INVALID) {
enqueue_first_msg(producer);
producer->current = next;
return PRODUCE_RESULT_SUCCESS;
}
enqueue_msg(producer);
tail = *msgq->tail;
if ((tail & INDEX_MASK) >= NUM_MSGS) {
return PRODUCE_RESULT_ERROR;
}
consumed = !!(tail & CONSUMED_FLAG);
full = (next == (tail & INDEX_MASK));
if (producer->overrun != INDEX_INVALID) {
/* we overran the consumer and moved the tail, use overran message as
* soon as the consumer releases it */
if (consumed) {
/* consumer released overrun message, so we can use it */
/* requeue overrun */
msgq->chain[producer->overrun] = next;
producer->current = producer->overrun;
producer->overrun = INDEX_INVALID;
} else {
/* consumer still blocks overran message, move the tail again,
* because the message queue is still full */
discarded = producer_move_tail(producer, tail);
if (discarded) {
producer->current = tail & INDEX_MASK;
} else {
/* consumer just released overrun message, so we can use it */
/* requeue overrun */
msgq->chain[producer->overrun] = next;
producer->current = producer->overrun;
producer->overrun = INDEX_INVALID;
}
}
} else {
/* no previous overrun, use next or after next message */
if (!full) {
/* message queue not full, simply use next */
producer->current = next;
} else if (!consumed) {
/* message queue is full, but no message is consumed yet, so try to move tail */
discarded = producer_move_tail(producer, tail);
if (discarded) {
producer->current = tail & INDEX_MASK;
} else {
/* consumer just started and consumed tail
if consumer already moved on, we will use tail */
producer_overrun(producer, tail | CONSUMED_FLAG);
}
} else {
/* overrun the consumer, if the consumer keeps tail*/
discarded = producer_overrun(producer, tail);
}
}
if (discarded) {
return PRODUCE_RESULT_DISCARDED;
}
return PRODUCE_RESULT_SUCCESS;
}
int producer_try_push(producer_t *producer)
{
msgq_t *msgq;
msgq = &producer->msgq;
unsigned next, tail;
int consumed, full;
if (producer->current == INDEX_INVALID) {
producer->current = 0;
return producer->current;
}
next = msgq->chain[producer->current];
if (producer->head == INDEX_INVALID) {
enqueue_first_msg(producer);
producer->current = next;
return PRODUCE_RESULT_SUCCESS;
}
tail = *msgq->tail;
if ((tail & INDEX_MASK) >= NUM_MSGS)
return PRODUCE_RESULT_ERROR;
consumed = !!(tail & CONSUMED_FLAG);
full = (next == (tail & INDEX_MASK));
if (producer->overrun != INDEX_INVALID) {
if (consumed) {
/* consumer released overrun message, so we can use it */
/* requeue overrun */
enqueue_msg(producer);
msgq->chain[producer->overrun] = next;
producer->current = producer->overrun;
producer->overrun = INDEX_INVALID;
return PRODUCE_RESULT_SUCCESS;
}
} else {
/* no previous overrun, use next or after next message */
if (!full) {
enqueue_msg(producer);
producer->current = next;
return PRODUCE_RESULT_SUCCESS;
}
}
return PRODUCE_RESULT_FAIL;
}
int consumer_pop(consumer_t *consumer)
{
msgq_t *msgq;
unsigned tail;
msgq = &consumer->msgq;
tail = atomic_fetch_or(msgq->tail, CONSUMED_FLAG);
if (tail == INDEX_INVALID) {
return CONSUME_RESULT_NO_MSG;
}
if ((tail & INDEX_MASK) >= NUM_MSGS) {
return CONSUME_RESULT_ERROR;
}
if ((tail & CONSUMED_FLAG) == 0) {
consumer->current = tail & INDEX_MASK;
if (tail & FIRST_FLAG) {
return CONSUME_RESULT_SUCCESS;
} else {
return CONSUME_RESULT_DISCARDED;
}
}
unsigned next;
/* try to get next message */
next = msgq->chain[consumer->current];
if (next == INDEX_INVALID) {
return CONSUME_RESULT_NO_UPDATE;
}
if (next >= NUM_MSGS) {
return CONSUME_RESULT_ERROR;
}
int r;
r = atomic_compare_exchange_strong(msgq->tail, &tail, next | CONSUMED_FLAG);
if (r) {
consumer->current = next;
return CONSUME_RESULT_SUCCESS;
} else {
/* producer just moved tail, use it */
consumer->current = atomic_fetch_or(msgq->tail, CONSUMED_FLAG);
}
return CONSUME_RESULT_DISCARDED;
}
void *producer_thread(void *arg)
{
producer_t producer;
msgq_t *msgq;
int i,r;
unsigned int counter;
counter = 0;
msgq = &producer.msgq;
for (i = 0; i < NUM_MSGS - 1; i++) {
g_msgq_shm_chain[i] = i + 1;
}
g_msgq_shm_chain[NUM_MSGS - 1] = 0;
producer.current = 0;
producer.head = INDEX_INVALID;
producer.overrun = INDEX_INVALID;
msgq->head = &g_msgq_shm_head;
msgq->tail = &g_msgq_shm_tail;
msgq->chain = g_msgq_shm_chain;
for (i = 0; i < NUM_MSGS + 2; i++) {
g_shm_msgs[producer.current] = counter;
g_producer_current = INDEX_INVALID;
r = producer_force_push(&producer);
assert(r == PRODUCE_RESULT_SUCCESS || r == PRODUCE_RESULT_DISCARDED);
assert(g_producer_current != producer.current);
g_producer_current = producer.current;
assert(g_producer_current != g_consumer_current);
counter++;
}
return NULL;
}
void* consumer_thread(void *arg)
{
consumer_t consumer;
msgq_t *msgq;
int i, r;
int received_msg;
unsigned int counter;
counter = 0;
received_msg = 0;
msgq = &consumer.msgq;
msgq->head = &g_msgq_shm_head;
msgq->tail = &g_msgq_shm_tail;
msgq->chain = g_msgq_shm_chain;
consumer.current = INDEX_INVALID;
for (i = 0; i < NUM_MSGS + 2; i++) {
g_consumer_current = INDEX_INVALID;
r = consumer_pop(&consumer);
switch (r) {
case CONSUME_RESULT_NO_MSG:
assert(received_msg == 0);
assert(consumer.current == INDEX_INVALID);
break;
case CONSUME_RESULT_NO_UPDATE:
assert(g_shm_msgs[consumer.current] == counter);
break;
case CONSUME_RESULT_SUCCESS:
if (received_msg) {
assert(g_shm_msgs[consumer.current] == counter + 1);
}
counter = g_shm_msgs[consumer.current];
received_msg = 1;
break;
case CONSUME_RESULT_DISCARDED:
if (received_msg) {
assert(g_shm_msgs[consumer.current] > counter + 1);
}
counter = g_shm_msgs[consumer.current];
received_msg = 1;
break;
default:
assert(!r);
break;
}
g_consumer_current = consumer.current;
assert((g_producer_current != g_consumer_current) || (g_consumer_current == INDEX_INVALID));
}
}
int
main(void)
{
int i;
pthread_t a, b;
pthread_create(&a, 0, producer_thread, 0);
pthread_create(&b, 0, consumer_thread, 0);
pthread_join(a, 0);
pthread_join(b, 0);
return 0;
}