edits
[cdsspec-compiler.git] / output / 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                 return -1;
23 }
24
25
26 bool succ1, succ2;
27 unsigned int output1, output2;
28
29 static void main_task(void *param)
30 {
31         int pid = *((int *)param);
32         if (pid % 4 == 0) {
33                 output1 = 1;
34                 succ1 = dequeue(queue, &output1);
35                 if (succ1)
36                         printf("Thrd 1: Dequeue %d.\n", output1);
37                 else
38                         printf("Thrd 1: Dequeue NULL.\n");
39         } else if (pid % 4 == 1) {
40                 enqueue(queue, 2);
41                 output2 = 1;
42                 succ2 = dequeue(queue, &output2);
43                 if (succ2)
44                         printf("Thrd 2: Dequeue %d.\n", output2);
45                 else
46                         printf("Thrd 2: Dequeue NULL.\n");
47         }
48 }
49
50 int user_main(int argc, char **argv)
51 {
52         __sequential_init();
53         
54         int i;
55         int *param;
56         unsigned int in_sum = 0, out_sum = 0;
57
58         queue = calloc(1, sizeof(*queue));
59         
60         num_threads = procs;
61         threads = malloc(num_threads * sizeof(thrd_t));
62         param = malloc(num_threads * sizeof(*param));
63         input = calloc(num_threads, sizeof(*input));
64         output = calloc(num_threads, sizeof(*output));
65
66         init_queue(queue, num_threads);
67         for (i = 0; i < num_threads; i++) {
68                 param[i] = i;
69                 thrd_create(&threads[i], main_task, &param[i]);
70         }
71         for (i = 0; i < num_threads; i++)
72                 thrd_join(threads[i]);
73
74
75         free(param);
76         free(threads);
77         free(queue);
78
79         return 0;
80 }
81