edits
[model-checker-benchmarks.git] / treiber-stack / testcase1.cc
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <threads.h>
4 #include "model-assert.h"
5
6 #ifdef WILDCARD
7 #include "stack_wildcard.h"
8 #else
9 #include "stack.h"
10 #endif
11
12 static int procs = 4;
13 static stack *s;
14
15 unsigned int idx1, idx2;
16 unsigned int a, b;
17
18
19 atomic_int x[3];
20
21 static void main_task(void *param)
22 {
23         unsigned int val;
24         int pid = *((int *)param);
25
26         if (pid % 4 == 0) {
27                 atomic_store_explicit(&x[1], 17, relaxed);
28         } else if (pid % 4 == 1) {
29                 atomic_store_explicit(&x[2], 37, relaxed);
30                 s->push(2);
31         } else if (pid % 4 == 2) {
32                 idx1 = s->pop();
33                 if (idx1 != 0) {
34                         a = atomic_load_explicit(&x[idx1], relaxed);
35                 }
36         } else {
37                 idx2 = s->pop();
38                 if (idx2 != 0) {
39                         b = atomic_load_explicit(&x[idx2], relaxed);
40                 }
41         }
42 }
43
44 int user_main(int argc, char **argv)
45 {
46         int i;
47         int *param;
48
49         atomic_init(&x[1], 0);
50         atomic_init(&x[2], 0);
51
52         s = new stack;
53
54         int num_threads = 4;
55
56         param = new int[num_threads];
57         thrd_t *threads = new thrd_t[num_threads];
58
59         for (i = 0; i < num_threads; i++) {
60                 param[i] = i;
61                 thrd_create(&threads[i], main_task, &param[i]);
62         }
63         for (i = 0; i < num_threads; i++)
64                 thrd_join(threads[i]);
65
66         delete param;
67         delete threads;
68         delete s;
69         
70         return 0;
71 }