ms-queue: don't initialize node 0
[model-checker-benchmarks.git] / ms-queue / main.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <threads.h>
4
5 #include "my_queue.h"
6 #include "model-assert.h"
7
8 static int procs = 2;
9 static queue_t *queue;
10 static thrd_t *threads;
11 static unsigned int *input;
12 static unsigned int *output;
13 static int num_threads;
14
15 int get_thread_num()
16 {
17         thrd_t curr = thrd_current();
18         int i;
19         for (i = 0; i < num_threads; i++)
20                 if (curr.priv == threads[i].priv)
21                         return i;
22         MODEL_ASSERT(0);
23         return -1;
24 }
25
26 static void main_task(void *param)
27 {
28         unsigned int val;
29         int pid = *((int *)param);
30
31         if (!pid) {
32                 input[0] = 17;
33                 enqueue(queue, input[0]);
34                 output[0] = dequeue(queue);
35         } else {
36                 input[1] = 37;
37                 enqueue(queue, input[1]);
38                 output[1] = dequeue(queue);
39         }
40 }
41
42 int user_main(int argc, char **argv)
43 {
44         int i;
45         int *param;
46         unsigned int in_sum = 0, out_sum = 0;
47
48         queue = calloc(1, sizeof(*queue));
49         MODEL_ASSERT(queue);
50
51         num_threads = procs;
52         threads = malloc(num_threads * sizeof(thrd_t));
53         param = malloc(num_threads * sizeof(*param));
54         input = calloc(num_threads, sizeof(*input));
55         output = calloc(num_threads, sizeof(*output));
56
57         init_queue(queue, num_threads);
58         for (i = 0; i < num_threads; i++) {
59                 param[i] = i;
60                 thrd_create(&threads[i], main_task, &param[i]);
61         }
62         for (i = 0; i < num_threads; i++)
63                 thrd_join(threads[i]);
64
65         for (i = 0; i < num_threads; i++) {
66                 in_sum += input[i];
67                 out_sum += output[i];
68         }
69         for (i = 0; i < num_threads; i++)
70                 printf("input[%d] = %u\n", i, input[i]);
71         for (i = 0; i < num_threads; i++)
72                 printf("output[%d] = %u\n", i, output[i]);
73         MODEL_ASSERT(in_sum == out_sum);
74
75         free(param);
76         free(threads);
77         free(queue);
78
79         return 0;
80 }