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", *bin);
24 queue->read_consume(bin);
28 void threadC(struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> *queue)
30 int32_t *bin = queue->write_prepare();
32 queue->write_publish(bin);
34 while (bin = queue->read_fetch()) {
35 printf("Read: %d\n", *bin);
36 queue->read_consume(bin);
44 #ifdef CONFIG_MPMC_READERS
45 #define DEFAULT_READERS (CONFIG_MPMC_READERS)
47 #define DEFAULT_READERS 2
50 #ifdef CONFIG_MPMC_WRITERS
51 #define DEFAULT_WRITERS (CONFIG_MPMC_WRITERS)
53 #define DEFAULT_WRITERS 2
56 #ifdef CONFIG_MPMC_RDWR
57 #define DEFAULT_RDWR (CONFIG_MPMC_RDWR)
59 #define DEFAULT_RDWR 0
62 int readers = DEFAULT_READERS, writers = DEFAULT_WRITERS, rdwr = DEFAULT_RDWR;
66 printf("Error: use the following options\n"
67 " -r <num> Choose number of reader threads\n"
68 " -w <num> Choose number of writer threads\n");
72 void process_params(int argc, char **argv)
74 const char *shortopts = "hr:w:";
78 while (!error && (opt = getopt(argc, argv, shortopts)) != -1) {
84 readers = atoi(optarg);
87 writers = atoi(optarg);
95 if (writers < 1 || writers > MAXWRITERS)
97 if (readers < 1 || readers > MAXREADERS)
104 int user_main(int argc, char **argv)
106 struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> queue;
107 thrd_t A[MAXWRITERS], B[MAXREADERS], C[MAXRDWR];
110 printf("%d reader(s), %d writer(s)\n", readers, writers);
112 #ifndef CONFIG_MPMC_NO_INITIAL_ELEMENT
113 printf("Adding initial element\n");
114 int32_t *bin = queue.write_prepare();
116 printf("init_write_bin %d, val %d\n", bin, 17);
117 queue.write_publish(bin);
120 printf("Start threads\n");
122 for (int i = 0; i < writers; i++)
123 thrd_create(&A[i], (thrd_start_t)&threadA, &queue);
124 for (int i = 0; i < readers; i++)
125 thrd_create(&B[i], (thrd_start_t)&threadB, &queue);
127 for (int i = 0; i < rdwr; i++)
128 thrd_create(&C[i], (thrd_start_t)&threadC, &queue);
130 for (int i = 0; i < writers; i++)
132 for (int i = 0; i < readers; i++)
134 for (int i = 0; i < rdwr; i++)
137 printf("Threads complete\n");