edits
[cdsspec-compiler.git] / output / ms-queue / testcase3.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                 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, 1);
41                 printf("Thrd 2: Enqueue %d.\n", 1);
42         } else if (pid % 4 == 2) {
43                 
44                 output1 = 1;
45                 succ1 = dequeue(queue, &output1);
46                 if (succ1)
47                         printf("Thrd 3: Dequeue %d.\n", output1);
48                 else
49                         printf("Thrd 3: Dequeue NULL.\n");
50         }
51 }
52
53 int user_main(int argc, char **argv)
54 {
55         __sequential_init();
56         
57         int i;
58         int *param;
59         unsigned int in_sum = 0, out_sum = 0;
60
61         queue = calloc(1, sizeof(*queue));
62         
63         num_threads = procs;
64         threads = malloc(num_threads * sizeof(thrd_t));
65         param = malloc(num_threads * sizeof(*param));
66         input = calloc(num_threads, sizeof(*input));
67         output = calloc(num_threads, sizeof(*output));
68
69         init_queue(queue, num_threads);
70         for (i = 0; i < num_threads; i++) {
71                 param[i] = i;
72                 thrd_create(&threads[i], main_task, &param[i]);
73         }
74         for (i = 0; i < num_threads; i++)
75                 thrd_join(threads[i]);
76
77
78         free(param);
79         free(threads);
80         free(queue);
81
82         return 0;
83 }
84