fix commit that mistakenly happened
[model-checker-benchmarks.git] / ms-queue / main.c
index 015fbd4c7737175294b9952b697558dca7bf66af..e46413840bf7e46cba895693392a1153853ddfe5 100644 (file)
@@ -1,80 +1,85 @@
-#include "main.h"
 #include <stdlib.h>
+#include <stdio.h>
+#include <threads.h>
 
-#define NUM_PROCESSORS                 12
+#include "my_queue.h"
+#include "model-assert.h"
 
-struct tms tim;
-struct tms tim1;
+static int procs = 2;
+static queue_t *queue;
+static thrd_t *threads;
+static unsigned int *input;
+static unsigned int *output;
+static int num_threads;
 
-int shmid;
-
-unsigned pid;
-char* name = "";
-unsigned procs = 1;
-unsigned multi = 1;
-unsigned iterations = 1;
-unsigned initial_nodes = 0;
-unsigned repetitions = 1;
-unsigned work = 0;
-private_t private;
-shared_mem_t *smp;
-
-void time_test()
+int get_thread_num()
 {
-       unsigned i,j;
-       struct tms time_val;
-       clock_t t1, t2;
-       unsigned val;
-
-       if(pid==0) {
-               init_queue();
-       }
-       init_memory();
-       init_private();
-       for(i=0;i<iterations;i++) {
-               val = private.value;
-               enqueue(val);
-               for(j=0; j<work;) j++;
-               val = dequeue();
-               for(j=0; j<work;) j++;
-               private.value++;
-       }
+       thrd_t curr = thrd_current();
+       int i;
+       for (i = 0; i < num_threads; i++)
+               if (curr.priv == threads[i].priv)
+                       return i;
+       MODEL_ASSERT(0);
+       return -1;
 }
 
-void main_task()
-{
-       unsigned processor;
-       unsigned i;
+bool succ1, succ2;
 
-       processor = (pid/multi)+1;
-       processor %= NUM_PROCESSORS;
-       for (i=0; i<repetitions; i++) {
-               time_test();
+static void main_task(void *param)
+{
+       unsigned int val;
+       int pid = *((int *)param);
+       if (!pid) {
+               input[0] = 17;
+               enqueue(queue, input[0]);
+               succ1 = dequeue(queue, &output[0]);
+               //printf("Dequeue: %d\n", output[0]);
+       } else {
+               input[1] = 37;
+               enqueue(queue, input[1]);
+               succ2 = dequeue(queue, &output[1]);
        }
 }
 
 int user_main(int argc, char **argv)
 {
-       int i, num_threads;
-       thrd_t *t;
+       int i;
+       int *param;
+       unsigned int in_sum = 0, out_sum = 0;
 
-       parse_args(argc, argv);
-       name = argv[0];
-       iterations = (iterations + ((procs*multi)>>1))/(procs*multi);
+       queue = calloc(1, sizeof(*queue));
+       MODEL_ASSERT(queue);
 
-       smp = (shared_mem_t *)calloc(1, sizeof(shared_mem_t));
-       assert(smp);
+       num_threads = procs;
+       threads = malloc(num_threads * sizeof(thrd_t));
+       param = malloc(num_threads * sizeof(*param));
+       input = calloc(num_threads, sizeof(*input));
+       output = calloc(num_threads, sizeof(*output));
 
-       num_threads = procs * multi;
-       t = malloc(num_threads * sizeof(thrd_t));
+       init_queue(queue, num_threads);
+       for (i = 0; i < num_threads; i++) {
+               param[i] = i;
+               thrd_create(&threads[i], main_task, &param[i]);
+       }
+       for (i = 0; i < num_threads; i++)
+               thrd_join(threads[i]);
 
+       for (i = 0; i < num_threads; i++) {
+               in_sum += input[i];
+               out_sum += output[i];
+       }
        for (i = 0; i < num_threads; i++)
-               thrd_create(&t[i], main_task, NULL);
+               printf("input[%d] = %u\n", i, input[i]);
        for (i = 0; i < num_threads; i++)
-               thrd_join(t[i]);
+               printf("output[%d] = %u\n", i, output[i]);
+       if (succ1 && succ2)
+               MODEL_ASSERT(in_sum == out_sum);
+       else
+               MODEL_ASSERT (false);
 
-       free(t);
-       free(smp);
+       free(param);
+       free(threads);
+       free(queue);
 
        return 0;
 }