edits
[cdsspec-compiler.git] / output / mpmc-queue / testcase3.cc
1 #include <inttypes.h>
2 #include <threads.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6
7 #include <librace.h>
8
9 #include "mpmc-queue.h"
10
11 void threadA(struct mpmc_boundq_1_alt<int32_t, 1> *queue)
12 {
13         int32_t *bin;
14         
15
16         for (int i = 0; i < 1; i++) {
17                 bin = queue->write_prepare();
18                 if (bin) {
19                         *bin = 1;
20                         queue->write_publish(bin);
21                         printf("write_bin %d, val %d\n", bin, 1);
22                 } else {
23                         printf("write failed\n");
24                 }
25
26                 bin = queue->read_fetch();
27                 if (bin) {
28                         printf("read_bin: %d, val %d\n", bin, *bin);
29                         queue->read_consume(bin);
30                 } else {
31                         printf("read failed\n");
32                 }
33         }
34
35 }
36
37 void threadB(struct mpmc_boundq_1_alt<int32_t, 1> *queue)
38 {
39         int32_t *bin;
40         for (int i = 0; i < 1; i++) {
41                 bin = queue->read_fetch();
42                 if (bin) {
43                         printf("read_bin: %d, val %d\n", bin, *bin);
44                         queue->read_consume(bin);
45                 } else {
46                         printf("read failed\n");
47                 }
48         }
49
50
51 }
52
53 void threadC(struct mpmc_boundq_1_alt<int32_t, 1> *queue)
54 {
55         int     *bin;
56         bin = queue->write_prepare();
57         if (bin) {
58                 *bin = 1;
59                 queue->write_publish(bin);
60                 printf("write_bin %d, val %d\n", bin, 1);
61         } else {
62                 printf("write failed\n");
63         }
64 }
65
66 int user_main(int argc, char **argv)
67 {
68         struct mpmc_boundq_1_alt<int32_t, 1> queue;
69         thrd_t A, A1, B, B1, C, C1;
70
71         printf("Adding initial element\n");
72         int32_t *bin;
73         for (int i = 0; i < 0; i++) {
74                 printf("#%d, \n", i);
75                 bin = queue.write_prepare();
76                 *bin = 17;
77                 printf("init_write_bin %d, val %d\n", bin, 17);
78                 queue.write_publish(bin);
79
80         }
81         
82         for (int i = 0; i < 3; i++) {
83                 
84         }
85
86         printf("Start threads\n");
87
88         thrd_create(&A, (thrd_start_t)&threadB, &queue);
89         thrd_create(&A1, (thrd_start_t)&threadC, &queue);
90         thrd_create(&B, (thrd_start_t)&threadB, &queue);
91         thrd_create(&B1, (thrd_start_t)&threadC, &queue);
92         thrd_create(&C, (thrd_start_t)&threadB, &queue);
93         thrd_create(&C1, (thrd_start_t)&threadC, &queue);
94
95         thrd_join(A);
96         thrd_join(A1);
97         thrd_join(B);
98         thrd_join(B1);
99         thrd_join(C);
100         thrd_join(C1);
101         printf("Threads complete\n");
102
103         return 0;
104 }
105