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