changes
[cdsspec-compiler.git] / benchmark / ms-queue / testcase.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 /** Be careful, we must make these variables to be global so that they will be
27  * 'available' when the execution is generated */
28 bool succ1, succ2;
29 unsigned int output1, output2;
30
31 static void main_task(void *param)
32 {
33         int pid = *((int *)param);
34         if (pid % 3 == 0) {
35                 output1 = 1;
36                 succ1 = dequeue(queue, &output1);
37                 if (succ1)
38                         printf("Thrd 2: Dequeue %d.\n", output1);
39                 else
40                         printf("Thrd 2: Dequeue NULL.\n");
41         } else if (pid % 3 == 1) {
42                 output2 = 2;
43                 succ2 = dequeue(queue, &output2);
44                 if (succ2)
45                         printf("Thrd 3: Dequeue %d.\n", output2);
46                 else
47                         printf("Thrd 3: Dequeue NULL.\n");
48         } else if (pid %3 == 2) {
49                 int input = 47;
50                 enqueue(queue, input);
51                 printf("Thrd 4 Enqueue %d.\n", input);
52         }
53 }
54
55 int user_main(int argc, char **argv)
56 {
57         /**
58                 @Begin
59                 @Entry_point
60                 @End
61         */
62         int i;
63         int *param;
64         unsigned int in_sum = 0, out_sum = 0;
65
66         queue = calloc(1, sizeof(*queue));
67         //MODEL_ASSERT(queue);
68
69         num_threads = procs;
70         threads = malloc(num_threads * sizeof(thrd_t));
71         param = malloc(num_threads * sizeof(*param));
72         input = calloc(num_threads, sizeof(*input));
73         output = calloc(num_threads, sizeof(*output));
74
75         init_queue(queue, num_threads);
76         for (i = 0; i < num_threads; i++) {
77                 param[i] = i;
78                 thrd_create(&threads[i], main_task, &param[i]);
79         }
80         for (i = 0; i < num_threads; i++)
81                 thrd_join(threads[i]);
82 /*
83         for (i = 0; i < num_threads; i++) {
84                 in_sum += input[i];
85                 out_sum += output[i];
86         }
87         for (i = 0; i < num_threads; i++)
88                 printf("input[%d] = %u\n", i, input[i]);
89         for (i = 0; i < num_threads; i++)
90                 printf("output[%d] = %u\n", i, output[i]);
91         //MODEL_ASSERT(in_sum == out_sum);
92         */
93
94         free(param);
95         free(threads);
96         free(queue);
97
98         return 0;
99 }