spsc-queue: add spsc-relacy build
[model-checker-benchmarks.git] / mpmc-queue / mpmc-queue.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, sizeof(int32_t)> *queue)
12 {
13         int32_t *bin = queue->write_prepare();
14         store_32(bin, 1);
15         queue->write_publish();
16 }
17
18 void threadB(struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> *queue)
19 {
20         int32_t *bin;
21         while (bin = queue->read_fetch()) {
22                 printf("Read: %d\n", load_32(bin));
23                 queue->read_consume();
24         }
25 }
26
27 #define MAXREADERS 3
28 #define MAXWRITERS 3
29
30 int readers = 2, writers = 2;
31
32 void print_usage()
33 {
34         printf("Error: use the following options\n"
35                 " -r <num>              Choose number of reader threads\n"
36                 " -w <num>              Choose number of writer threads\n");
37         exit(EXIT_FAILURE);
38 }
39
40 void process_params(int argc, char **argv)
41 {
42         const char *shortopts = "hr:w:";
43         int opt;
44         bool error = false;
45
46         while (!error && (opt = getopt(argc, argv, shortopts)) != -1) {
47                 switch (opt) {
48                 case 'h':
49                         print_usage();
50                         break;
51                 case 'r':
52                         readers = atoi(optarg);
53                         break;
54                 case 'w':
55                         writers = atoi(optarg);
56                         break;
57                 default: /* '?' */
58                         error = true;
59                         break;
60                 }
61         }
62
63         if (writers < 1 || writers > MAXWRITERS)
64                 error = true;
65         if (readers < 1 || readers > MAXREADERS)
66                 error = true;
67
68         if (error)
69                 print_usage();
70 }
71
72 int user_main(int argc, char **argv)
73 {
74         struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> queue;
75         thrd_t A[MAXWRITERS], B[MAXREADERS];
76
77         /* Note: optarg() / optind is broken in model-checker - workaround is
78          * to just copy&paste this test a few times */
79         //process_params(argc, argv);
80         printf("%d reader(s), %d writer(s)\n", readers, writers);
81
82         int32_t *bin = queue.write_prepare();
83         store_32(bin, 17);
84         queue.write_publish();
85
86         printf("Start threads\n");
87
88         for (int i = 0; i < writers; i++)
89                 thrd_create(&A[i], (thrd_start_t)&threadA, &queue);
90         for (int i = 0; i < readers; i++)
91                 thrd_create(&B[i], (thrd_start_t)&threadB, &queue);
92
93         for (int i = 0; i < writers; i++)
94                 thrd_join(A[i]);
95         for (int i = 0; i < readers; i++)
96                 thrd_join(B[i]);
97
98         printf("Threads complete\n");
99
100         return 0;
101 }