a26f9811242dd9fbb303753afe8db07ba6192cdf
[cdsspec-compiler.git] / benchmark / mpmc-queue / testcase2.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         bin = queue->write_prepare();
16         if (bin) {
17                 *bin = 1;
18                 printf("write_bin %d, val %d\n", bin, 1);
19                 queue->write_publish(bin);
20         } else {
21                 printf("write failed\n");
22         }
23         */
24
25         for (int i = 0; i < 1; i++) {
26                 bin = queue->write_prepare();
27                 if (bin) {
28                         *bin = 1;
29                         queue->write_publish(bin);
30                         printf("write_bin %d, val %d\n", bin, 1);
31                 } else {
32                         printf("write failed\n");
33                 }
34
35                 bin = queue->read_fetch();
36                 if (bin) {
37                         printf("read_bin: %d, val %d\n", bin, *bin);
38                         queue->read_consume(bin);
39                 } else {
40                         printf("read failed\n");
41                 }
42         }
43
44 }
45
46 void threadB(struct mpmc_boundq_1_alt<int32_t, 1> *queue)
47 {
48         int32_t *bin;
49         for (int i = 0; i < 1; i++) {
50                 bin = queue->read_fetch();
51                 if (bin) {
52                         printf("read_bin: %d, val %d\n", bin, *bin);
53                         queue->read_consume(bin);
54                 } else {
55                         printf("read failed\n");
56                 }
57         }
58
59
60 }
61
62 int user_main(int argc, char **argv)
63 {
64         struct mpmc_boundq_1_alt<int32_t, 1> queue;
65         thrd_t A, A1, B;
66
67         printf("Adding initial element\n");
68         int32_t *bin;
69         for (int i = 0; i < 1; i++) {
70                 printf("#%d, \n", i);
71                 bin = queue.write_prepare();
72                 *bin = 17;
73                 printf("init_write_bin %d, val %d\n", bin, 17);
74                 queue.write_publish(bin);
75
76                 bin = queue.read_fetch();
77                 if (bin) {
78                         printf("init_read: %d, val %d\n", bin, *bin);
79                         queue.read_consume(bin);
80                 }
81         }
82         
83         for (int i = 0; i < 3; i++) {
84                 
85         }
86
87         printf("Start threads\n");
88
89         thrd_create(&A, (thrd_start_t)&threadA, &queue);
90         thrd_create(&A1, (thrd_start_t)&threadA, &queue);
91         thrd_create(&B, (thrd_start_t)&threadB, &queue);
92
93         thrd_join(A);
94         thrd_join(A1);
95         thrd_join(B);
96         printf("Threads complete\n");
97
98         return 0;
99 }