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