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