9 #include "mpmc-queue-wildcard.h"
13 void threadA(struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> *queue)
15 int32_t *bin = queue->write_prepare();
17 x.store(1, memory_order_relaxed);
18 queue->write_publish();
21 void threadB(struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> *queue)
24 while ((bin = queue->read_fetch()) != NULL) {
25 x.load(memory_order_relaxed);
26 //printf("Read: %d\n", load_32(bin));
27 queue->read_consume();
31 void threadC(struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> *queue)
33 int32_t *bin = queue->write_prepare();
35 x.store(1, memory_order_relaxed);
37 queue->write_publish();
39 while ((bin = queue->read_fetch()) != NULL) {
40 x.load(memory_order_relaxed);
41 //printf("Read: %d\n", load_32(bin));
42 queue->read_consume();
49 #ifdef CONFIG_MPMC_READERS
50 #define DEFAULT_READERS (CONFIG_MPMC_READERS)
52 #define DEFAULT_READERS 2
55 #ifdef CONFIG_MPMC_WRITERS
56 #define DEFAULT_WRITERS (CONFIG_MPMC_WRITERS)
58 #define DEFAULT_WRITERS 2
61 #ifdef CONFIG_MPMC_RDWR
62 #define DEFAULT_RDWR (CONFIG_MPMC_RDWR)
64 #define DEFAULT_RDWR 0
67 int readers = DEFAULT_READERS, writers = DEFAULT_WRITERS, rdwr = DEFAULT_RDWR;
71 printf("Error: use the following options\n"
72 " -r <num> Choose number of reader threads\n"
73 " -w <num> Choose number of writer threads\n");
77 void process_params(int argc, char **argv)
79 const char *shortopts = "hr:w:";
83 while (!error && (opt = getopt(argc, argv, shortopts)) != -1) {
89 readers = atoi(optarg);
92 writers = atoi(optarg);
100 if (writers < 1 || writers > MAXWRITERS)
102 if (readers < 1 || readers > MAXREADERS)
109 int user_main(int argc, char **argv)
111 struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> queue;
112 thrd_t A[MAXWRITERS], B[MAXREADERS], C[MAXRDWR];
114 /* Note: optarg() / optind is broken in model-checker - workaround is
115 * to just copy&paste this test a few times */
116 //process_params(argc, argv);
117 printf("%d reader(s), %d writer(s)\n", readers, writers);
121 #ifndef CONFIG_MPMC_NO_INITIAL_ELEMENT
122 printf("Adding initial element\n");
123 int32_t *bin = queue.write_prepare();
125 queue.write_publish();
128 printf("Start threads\n");
130 for (int i = 0; i < writers; i++)
131 thrd_create(&A[i], (thrd_start_t)&threadA, &queue);
132 for (int i = 0; i < readers; i++)
133 thrd_create(&B[i], (thrd_start_t)&threadB, &queue);
135 for (int i = 0; i < rdwr; i++)
136 thrd_create(&C[i], (thrd_start_t)&threadC, &queue);
138 for (int i = 0; i < writers; i++)
140 for (int i = 0; i < readers; i++)
142 for (int i = 0; i < rdwr; i++)
145 printf("Threads complete\n");