fixed commutativity rule
[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 = 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 % 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, 1);
43         } else if (pid % 4 == 2) {
44                 enqueue(queue, 2);
45         }
46 }
47
48 int user_main(int argc, char **argv)
49 {
50         /**
51                 @Begin
52                 @Entry_point
53                 @End
54         */
55         int i;
56         int *param;
57         unsigned int in_sum = 0, out_sum = 0;
58
59         queue = calloc(1, sizeof(*queue));
60         //MODEL_ASSERT(queue);
61
62         num_threads = procs;
63         threads = malloc(num_threads * sizeof(thrd_t));
64         param = malloc(num_threads * sizeof(*param));
65         input = calloc(num_threads, sizeof(*input));
66         output = calloc(num_threads, sizeof(*output));
67
68         init_queue(queue, num_threads);
69         for (i = 0; i < num_threads; i++) {
70                 param[i] = i;
71                 thrd_create(&threads[i], main_task, &param[i]);
72         }
73         for (i = 0; i < num_threads; i++)
74                 thrd_join(threads[i]);
75 /*
76         for (i = 0; i < num_threads; i++) {
77                 in_sum += input[i];
78                 out_sum += output[i];
79         }
80         for (i = 0; i < num_threads; i++)
81                 printf("input[%d] = %u\n", i, input[i]);
82         for (i = 0; i < num_threads; i++)
83                 printf("output[%d] = %u\n", i, output[i]);
84         //MODEL_ASSERT(in_sum == out_sum);
85         */
86
87         free(param);
88         free(threads);
89         free(queue);
90
91         return 0;
92 }