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