edits
[cdsspec-compiler.git] / benchmark / ms-queue / testcase2.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 /** 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 % 4 == 0) {
35                 output1 = 1;
36                 succ1 = dequeue(queue, &output1);
37                 if (succ1)
38                         printf("Thrd 1: Dequeue %d.\n", output1);
39                 else
40                         printf("Thrd 1: Dequeue NULL.\n");
41         } else if (pid % 4 == 1) {
42                 enqueue(queue, 2);
43                 output2 = 1;
44                 succ2 = dequeue(queue, &output2);
45                 if (succ2)
46                         printf("Thrd 2: Dequeue %d.\n", output2);
47                 else
48                         printf("Thrd 2: Dequeue NULL.\n");
49         }
50 }
51
52 int user_main(int argc, char **argv)
53 {
54         /**
55                 @Begin
56                 @Entry_point
57                 @End
58         */
59         int i;
60         int *param;
61         unsigned int in_sum = 0, out_sum = 0;
62
63         queue = calloc(1, sizeof(*queue));
64         //MODEL_ASSERT(queue);
65
66         num_threads = procs;
67         threads = malloc(num_threads * sizeof(thrd_t));
68         param = malloc(num_threads * sizeof(*param));
69         input = calloc(num_threads, sizeof(*input));
70         output = calloc(num_threads, sizeof(*output));
71
72         init_queue(queue, num_threads);
73         for (i = 0; i < num_threads; i++) {
74                 param[i] = i;
75                 thrd_create(&threads[i], main_task, &param[i]);
76         }
77         for (i = 0; i < num_threads; i++)
78                 thrd_join(threads[i]);
79 /*
80         for (i = 0; i < num_threads; i++) {
81                 in_sum += input[i];
82                 out_sum += output[i];
83         }
84         for (i = 0; i < num_threads; i++)
85                 printf("input[%d] = %u\n", i, input[i]);
86         for (i = 0; i < num_threads; i++)
87                 printf("output[%d] = %u\n", i, output[i]);
88         //MODEL_ASSERT(in_sum == out_sum);
89         */
90
91         free(param);
92         free(threads);
93         free(queue);
94
95         return 0;
96 }