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 printf("write_bin %d, val %d\n", bin, 1);
16 queue->write_publish(bin);
19 void threadB(struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> *queue)
22 while (bin = queue->read_fetch()) {
23 printf("Read: %d\n", load_32(bin));
24 printf("read_bin %d, val %d\n", bin, load_32(bin));
25 queue->read_consume();
29 void threadC(struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> *queue)
31 int32_t *bin = queue->write_prepare();
33 queue->write_publish(bin);
35 while (bin = queue->read_fetch()) {
36 printf("Read: %d\n", load_32(bin));
37 queue->read_consume();
45 #ifdef CONFIG_MPMC_READERS
46 #define DEFAULT_READERS (CONFIG_MPMC_READERS)
48 #define DEFAULT_READERS 2
51 #ifdef CONFIG_MPMC_WRITERS
52 #define DEFAULT_WRITERS (CONFIG_MPMC_WRITERS)
54 #define DEFAULT_WRITERS 2
57 #ifdef CONFIG_MPMC_RDWR
58 #define DEFAULT_RDWR (CONFIG_MPMC_RDWR)
60 #define DEFAULT_RDWR 0
63 int readers = DEFAULT_READERS, writers = DEFAULT_WRITERS, rdwr = DEFAULT_RDWR;
67 printf("Error: use the following options\n"
68 " -r <num> Choose number of reader threads\n"
69 " -w <num> Choose number of writer threads\n");
73 void process_params(int argc, char **argv)
75 const char *shortopts = "hr:w:";
79 while (!error && (opt = getopt(argc, argv, shortopts)) != -1) {
85 readers = atoi(optarg);
88 writers = atoi(optarg);
96 if (writers < 1 || writers > MAXWRITERS)
98 if (readers < 1 || readers > MAXREADERS)
105 int user_main(int argc, char **argv)
107 struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> queue;
108 thrd_t A[MAXWRITERS], B[MAXREADERS], C[MAXRDWR];
110 /* Note: optarg() / optind is broken in model-checker - workaround is
111 * to just copy&paste this test a few times */
112 //process_params(argc, argv);
113 printf("%d reader(s), %d writer(s)\n", readers, writers);
115 #ifndef CONFIG_MPMC_NO_INITIAL_ELEMENT
116 printf("Adding initial element\n");
117 int32_t *bin = queue.write_prepare();
119 printf("init_write_bin %d, val %d\n", bin, 17);
120 queue.write_publish(bin);
123 printf("Start threads\n");
125 for (int i = 0; i < writers; i++)
126 thrd_create(&A[i], (thrd_start_t)&threadA, &queue);
127 for (int i = 0; i < readers; i++)
128 thrd_create(&B[i], (thrd_start_t)&threadB, &queue);
130 for (int i = 0; i < rdwr; i++)
131 thrd_create(&C[i], (thrd_start_t)&threadC, &queue);
133 for (int i = 0; i < writers; i++)
135 for (int i = 0; i < readers; i++)
137 for (int i = 0; i < rdwr; i++)
140 printf("Threads complete\n");