changes to ms-queue spec
[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 bool succ1, succ2;
27
28 static void main_task(void *param)
29 {
30
31         unsigned int val;
32         int pid = *((int *)param);
33 /*
34         if (!pid) {
35                 input[0] = 17;
36                 succ1 = dequeue(queue, &input[0]);
37         } else {
38                 input[1] = 37;
39                 enqueue(queue, input[1]);
40         }
41 */
42
43         if (!pid) {
44                 input[0] = 17;
45                 enqueue(queue, input[0]);
46                 printf("Enqueue %d.\n", 17);
47                 succ1 = dequeue(queue, &input[0]);
48                 if (succ1)
49                         printf("Dequeue %d.\n", input[0]);
50                 else
51                         printf("Dequeue NULL.\n");
52         } else {
53                 input[1] = 37;
54                 enqueue(queue, input[1]);
55                 printf("Enqueue %d.\n", 37);
56                 succ2 = dequeue(queue, &output[1]);
57                 if (succ2)
58                         printf("Dequeue %d.\n", input[1]);
59                 else
60                         printf("Dequeue NULL.\n");
61         }
62
63 }
64
65 int user_main(int argc, char **argv)
66 {
67         /**
68                 @Begin
69                 @Entry_point
70                 @End
71         */
72         int i;
73         int *param;
74         unsigned int in_sum = 0, out_sum = 0;
75
76         queue = calloc(1, sizeof(*queue));
77         //MODEL_ASSERT(queue);
78
79         num_threads = procs;
80         threads = malloc(num_threads * sizeof(thrd_t));
81         param = malloc(num_threads * sizeof(*param));
82         input = calloc(num_threads, sizeof(*input));
83         output = calloc(num_threads, sizeof(*output));
84
85         init_queue(queue, num_threads);
86         for (i = 0; i < num_threads; i++) {
87                 param[i] = i;
88                 thrd_create(&threads[i], main_task, &param[i]);
89         }
90         for (i = 0; i < num_threads; i++)
91                 thrd_join(threads[i]);
92 /*
93         for (i = 0; i < num_threads; i++) {
94                 in_sum += input[i];
95                 out_sum += output[i];
96         }
97         for (i = 0; i < num_threads; i++)
98                 printf("input[%d] = %u\n", i, input[i]);
99         for (i = 0; i < num_threads; i++)
100                 printf("output[%d] = %u\n", i, output[i]);
101         //MODEL_ASSERT(in_sum == out_sum);
102         */
103
104         free(param);
105         free(threads);
106         free(queue);
107
108         return 0;
109 }