changes
[model-checker-benchmarks.git] / treiber-stack / main.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <threads.h>
4
5 #include "my_stack.h"
6 #include "model-assert.h"
7
8 static int procs = 4;
9 static stack_t *stack;
10 static thrd_t *threads;
11 static int num_threads;
12
13 unsigned int idx1, idx2;
14 unsigned int a, b;
15
16
17 atomic_int x[3];
18
19 int get_thread_num()
20 {
21         thrd_t curr = thrd_current();
22         int i;
23         for (i = 0; i < num_threads; i++)
24                 if (curr.priv == threads[i].priv)
25                         return i;
26         MODEL_ASSERT(0);
27         return -1;
28 }
29
30 static void main_task(void *param)
31 {
32         unsigned int val;
33         int pid = *((int *)param);
34
35         if (pid % 4 == 0) {
36                 atomic_store_explicit(&x[1], 17, relaxed);
37                 push(stack, 1);
38         } else if (pid % 4 == 1) {
39                 atomic_store_explicit(&x[2], 37, relaxed);
40                 push(stack, 2);
41         } else if (pid % 4 == 2) {/*
42                 idx1 = pop(stack);
43                 if (idx1 != 0) {
44                         a = atomic_load_explicit(&x[idx1], relaxed);
45                         printf("a: %d\n", a);
46                 }*/
47         } else {
48                 idx2 = pop(stack);
49                 if (idx2 != 0) {
50                         b = atomic_load_explicit(&x[idx2], relaxed);
51                         printf("b: %d\n", b);
52                 }
53         }
54 }
55
56 int user_main(int argc, char **argv)
57 {
58         int i;
59         int *param;
60         unsigned int in_sum = 0, out_sum = 0;
61
62         atomic_init(&x[1], 0);
63         atomic_init(&x[2], 0);
64
65         stack = calloc(1, sizeof(*stack));
66
67         num_threads = procs;
68         threads = malloc(num_threads * sizeof(thrd_t));
69         param = malloc(num_threads * sizeof(*param));
70
71         init_stack(stack, num_threads);
72         
73         for (i = 0; i < num_threads; i++) {
74                 param[i] = i;
75                 thrd_create(&threads[i], main_task, &param[i]);
76         }
77         for (i = 0; i < num_threads; i++)
78                 thrd_join(threads[i]);
79
80         bool correct = false;
81         //correct |= (a == 17 || a == 37 || a == 0);
82         //MODEL_ASSERT(correct);
83
84         free(param);
85         free(threads);
86         free(stack);
87         
88         return 0;
89 }