f6973a706ced450952d2e7180579ddb0d9c4acc9
[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 = 3;
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 % 3 == 0) {
44                 input[0] = 17;
45                 enqueue(queue, input[0]);
46                 printf("Enqueue %d.\n", 17);
47                 
48                 succ1 = dequeue(queue, &input[0]);
49                 if (succ1)
50                         printf("Thrd 2: Dequeue %d.\n", input[0]);
51                 else
52                         printf("Thrd 2: Dequeue NULL.\n");
53                 
54         } else if (pid % 3 == 1) {
55                 
56                 input[1] = 37;
57                 enqueue(queue, input[1]);
58                 printf("Enqueue %d.\n", 37);
59                 
60                 succ1 = dequeue(queue, &input[0]);
61                 if (succ1)
62                         printf("Thrd 3: Dequeue %d.\n", input[0]);
63                 else
64                         printf("Thrd 3: Dequeue NULL.\n");
65         } else if (pid %3 == 2) {
66                 /*
67                 succ2 = dequeue(queue, &output[1]);
68                 if (succ2)
69                         printf("Dequeue %d.\n", input[1]);
70                 else
71                         printf("Dequeue NULL.\n");
72                         */
73         }
74
75 }
76
77 int user_main(int argc, char **argv)
78 {
79         /**
80                 @Begin
81                 @Entry_point
82                 @End
83         */
84         int i;
85         int *param;
86         unsigned int in_sum = 0, out_sum = 0;
87
88         queue = calloc(1, sizeof(*queue));
89         //MODEL_ASSERT(queue);
90
91         num_threads = procs;
92         threads = malloc(num_threads * sizeof(thrd_t));
93         param = malloc(num_threads * sizeof(*param));
94         input = calloc(num_threads, sizeof(*input));
95         output = calloc(num_threads, sizeof(*output));
96
97         init_queue(queue, num_threads);
98         for (i = 0; i < num_threads; i++) {
99                 param[i] = i;
100                 thrd_create(&threads[i], main_task, &param[i]);
101         }
102         for (i = 0; i < num_threads; i++)
103                 thrd_join(threads[i]);
104 /*
105         for (i = 0; i < num_threads; i++) {
106                 in_sum += input[i];
107                 out_sum += output[i];
108         }
109         for (i = 0; i < num_threads; i++)
110                 printf("input[%d] = %u\n", i, input[i]);
111         for (i = 0; i < num_threads; i++)
112                 printf("output[%d] = %u\n", i, output[i]);
113         //MODEL_ASSERT(in_sum == out_sum);
114         */
115
116         free(param);
117         free(threads);
118         free(queue);
119
120         return 0;
121 }