deque: re-insert deleted MODEL_ASSERT()
[model-checker-benchmarks.git] / mpmc-queue / mpmc-queue.cc
index 741811aaf357578bc0d0056d4ce4272f66881dc8..baca598bfa49b39e70134672f12e67b9135a95f5 100644 (file)
@@ -1,6 +1,8 @@
 #include <inttypes.h>
 #include <threads.h>
 #include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
 
 #include <librace.h>
 
@@ -22,25 +24,91 @@ void threadB(struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> *queue)
        }
 }
 
+#define MAXREADERS 3
+#define MAXWRITERS 3
+
+#ifdef CONFIG_MPMC_READERS
+#define DEFAULT_READERS (CONFIG_MPMC_READERS)
+#else
+#define DEFAULT_READERS 2
+#endif
+
+#ifdef CONFIG_MPMC_WRITERS
+#define DEFAULT_WRITERS (CONFIG_MPMC_WRITERS)
+#else
+#define DEFAULT_WRITERS 2
+#endif
+
+int readers = DEFAULT_READERS, writers = DEFAULT_WRITERS;
+
+void print_usage()
+{
+       printf("Error: use the following options\n"
+               " -r <num>              Choose number of reader threads\n"
+               " -w <num>              Choose number of writer threads\n");
+       exit(EXIT_FAILURE);
+}
+
+void process_params(int argc, char **argv)
+{
+       const char *shortopts = "hr:w:";
+       int opt;
+       bool error = false;
+
+       while (!error && (opt = getopt(argc, argv, shortopts)) != -1) {
+               switch (opt) {
+               case 'h':
+                       print_usage();
+                       break;
+               case 'r':
+                       readers = atoi(optarg);
+                       break;
+               case 'w':
+                       writers = atoi(optarg);
+                       break;
+               default: /* '?' */
+                       error = true;
+                       break;
+               }
+       }
+
+       if (writers < 1 || writers > MAXWRITERS)
+               error = true;
+       if (readers < 1 || readers > MAXREADERS)
+               error = true;
+
+       if (error)
+               print_usage();
+}
+
 int user_main(int argc, char **argv)
 {
        struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> queue;
-       thrd_t A1, A2, B1, B2;
+       thrd_t A[MAXWRITERS], B[MAXREADERS];
 
+       /* Note: optarg() / optind is broken in model-checker - workaround is
+        * to just copy&paste this test a few times */
+       //process_params(argc, argv);
+       printf("%d reader(s), %d writer(s)\n", readers, writers);
+
+#ifndef CONFIG_MPMC_NO_INITIAL_ELEMENT
+       printf("Adding initial element\n");
        int32_t *bin = queue.write_prepare();
        store_32(bin, 17);
        queue.write_publish();
+#endif
 
        printf("Start threads\n");
 
-       thrd_create(&A1, (thrd_start_t)&threadA, &queue);
-       thrd_create(&A2, (thrd_start_t)&threadA, &queue);
-       thrd_create(&B1, (thrd_start_t)&threadB, &queue);
-       thrd_create(&B2, (thrd_start_t)&threadB, &queue);
-       thrd_join(A1);
-       thrd_join(A2);
-       thrd_join(B1);
-       thrd_join(B2);
+       for (int i = 0; i < writers; i++)
+               thrd_create(&A[i], (thrd_start_t)&threadA, &queue);
+       for (int i = 0; i < readers; i++)
+               thrd_create(&B[i], (thrd_start_t)&threadB, &queue);
+
+       for (int i = 0; i < writers; i++)
+               thrd_join(A[i]);
+       for (int i = 0; i < readers; i++)
+               thrd_join(B[i]);
 
        printf("Threads complete\n");