minor fix
[cdsspec-compiler.git] / benchmark / 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         /**
45                 @Begin
46                 @Entry_point
47                 @End
48         */
49         int i;
50         int *param;
51         unsigned int in_sum = 0, out_sum = 0;
52
53         queue = calloc(1, sizeof(*queue));
54         MODEL_ASSERT(queue);
55
56         num_threads = procs;
57         threads = malloc(num_threads * sizeof(thrd_t));
58         param = malloc(num_threads * sizeof(*param));
59         input = calloc(num_threads, sizeof(*input));
60         output = calloc(num_threads, sizeof(*output));
61
62         init_queue(queue, num_threads);
63         for (i = 0; i < num_threads; i++) {
64                 param[i] = i;
65                 thrd_create(&threads[i], main_task, &param[i]);
66         }
67         for (i = 0; i < num_threads; i++)
68                 thrd_join(threads[i]);
69
70         for (i = 0; i < num_threads; i++) {
71                 in_sum += input[i];
72                 out_sum += output[i];
73         }
74         for (i = 0; i < num_threads; i++)
75                 printf("input[%d] = %u\n", i, input[i]);
76         for (i = 0; i < num_threads; i++)
77                 printf("output[%d] = %u\n", i, output[i]);
78         MODEL_ASSERT(in_sum == out_sum);
79
80         free(param);
81         free(threads);
82         free(queue);
83
84         return 0;
85 }