The CDSSpec checker's benchmarks
[model-checker-benchmarks.git] / mpmc-queue / testcase2.cc
1 #include <threads.h>
2 #include <unistd.h>
3 #include <librace.h>
4
5 #include "mpmc-queue.h"
6
7 atomic_int x;
8
9 void threadA(struct mpmc_boundq_1_alt<int32_t> *queue)
10 {
11         int32_t *bin = write_prepare(queue);
12     if (bin) {
13         //printf("Thread A: bin=%d", bin);
14         //store_32(bin, 1);
15         write_publish(queue, bin);
16     }
17 }
18
19 void threadB(struct mpmc_boundq_1_alt<int32_t> *queue)
20 {
21         int32_t *bin;
22         if ((bin = read_fetch(queue)) != NULL) {
23                 //printf("Read: %d\n", load_32(bin));
24                 read_consume(queue, bin);
25         }
26 }
27
28 void threadC(struct mpmc_boundq_1_alt<int32_t> *queue)
29 {
30         int32_t *bin = write_prepare(queue);
31     if (bin) {
32         //store_32(bin, 1);
33         write_publish(queue, bin);
34     }
35
36         while ((bin = read_fetch(queue)) != NULL) {
37                 //printf("Read: %d\n", load_32(bin));
38                 read_consume(queue, bin);
39         }
40 }
41
42
43 int user_main(int argc, char **argv)
44 {
45         mpmc_boundq_1_alt<int32_t> *queue = createMPMC(2);
46         thrd_t A, B, C;
47         /** @Entry */
48
49         // A queue of capacity 2, 2 writes happened very early
50         int32_t *bin = write_prepare(queue);
51         write_publish(queue, bin);
52         bin = write_prepare(queue);
53         write_publish(queue, bin);
54
55         printf("Start threads\n");
56
57         thrd_create(&A, (thrd_start_t)&threadA, queue);
58         thrd_create(&B, (thrd_start_t)&threadB, queue);
59         //thrd_create(&C, (thrd_start_t)&threadC, queue);
60
61         thrd_join(A);
62         thrd_join(B);
63         //thrd_join(C);
64
65         printf("Threads complete\n");
66
67         destroyMPMC(queue);
68         return 0;
69 }