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();
15 queue->write_publish();
18 void threadB(struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> *queue)
21 while (bin = queue->read_fetch()) {
22 printf("Read: %d\n", load_32(bin));
23 queue->read_consume();
30 #ifdef CONFIG_MPMC_READERS
31 #define DEFAULT_READERS (CONFIG_MPMC_READERS)
33 #define DEFAULT_READERS 2
36 #ifdef CONFIG_MPMC_WRITERS
37 #define DEFAULT_WRITERS (CONFIG_MPMC_WRITERS)
39 #define DEFAULT_WRITERS 2
42 int readers = DEFAULT_READERS, writers = DEFAULT_WRITERS;
46 printf("Error: use the following options\n"
47 " -r <num> Choose number of reader threads\n"
48 " -w <num> Choose number of writer threads\n");
52 void process_params(int argc, char **argv)
54 const char *shortopts = "hr:w:";
58 while (!error && (opt = getopt(argc, argv, shortopts)) != -1) {
64 readers = atoi(optarg);
67 writers = atoi(optarg);
75 if (writers < 1 || writers > MAXWRITERS)
77 if (readers < 1 || readers > MAXREADERS)
84 int user_main(int argc, char **argv)
86 struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> queue;
87 thrd_t A[MAXWRITERS], B[MAXREADERS];
89 /* Note: optarg() / optind is broken in model-checker - workaround is
90 * to just copy&paste this test a few times */
91 //process_params(argc, argv);
92 printf("%d reader(s), %d writer(s)\n", readers, writers);
94 #ifndef CONFIG_MPMC_NO_INITIAL_ELEMENT
95 printf("Adding initial element\n");
96 int32_t *bin = queue.write_prepare();
98 queue.write_publish();
101 printf("Start threads\n");
103 for (int i = 0; i < writers; i++)
104 thrd_create(&A[i], (thrd_start_t)&threadA, &queue);
105 for (int i = 0; i < readers; i++)
106 thrd_create(&B[i], (thrd_start_t)&threadB, &queue);
108 for (int i = 0; i < writers; i++)
110 for (int i = 0; i < readers; i++)
113 printf("Threads complete\n");