9 #include "mpmc-queue.h"
11 void threadA(struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> *queue)
13 int32_t *bin = queue->write_prepare();
16 printf("write_bin %d, val %d\n", bin, 1);
17 queue->write_publish(bin);
20 void threadB(struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> *queue)
23 while (bin = queue->read_fetch()) {
24 printf("Read: %d\n", load_32(bin));
25 printf("read_bin %d, val %d\n", bin, load_32(bin));
26 printf("Read: %d\n", *bin);
27 queue->read_consume(bin);
31 void threadC(struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> *queue)
33 int32_t *bin = queue->write_prepare();
36 queue->write_publish(bin);
38 while (bin = queue->read_fetch()) {
39 printf("Read: %d\n", load_32(bin));
40 printf("Read: %d\n", *bin);
41 queue->read_consume(bin);
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, 2> 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);
119 #ifndef CONFIG_MPMC_NO_INITIAL_ELEMENT
120 printf("Adding initial element\n");
121 int32_t *bin = queue.write_prepare();
124 printf("init_write_bin %d, val %d\n", bin, 17);
125 queue.write_publish(bin);
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");