-
Notifications
You must be signed in to change notification settings - Fork 4
/
SafeStackRelacy.cpp
114 lines (97 loc) · 2.39 KB
/
SafeStackRelacy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <relacy/relacy_std.hpp>
template<typename T>
struct SafeStackItem
{
volatile T Value;
std::atomic<int> Next;
};
template<typename T>
class SafeStack
{
std::atomic<int> head;
std::atomic<int> count;
public:
SafeStackItem<T>* array;
SafeStack(int pushCount)
{
array = new SafeStackItem<T> [pushCount];
count.store(pushCount, std::memory_order_relaxed);
head.store(0, std::memory_order_relaxed);
for (int i = 0; i < pushCount - 1; i++)
array[i].Next.store(i + 1, std::memory_order_relaxed);
array[pushCount - 1].Next.store(-1, std::memory_order_relaxed);
}
~SafeStack()
{
delete [] array;
}
int Pop()
{
while (count.load(std::memory_order_acquire) > 1)
{
int head1 = head.load(std::memory_order_acquire);
int next1 = array[head1].Next.exchange(-1, std::memory_order_seq_cst);
if (next1 >= 0)
{
int head2 = head1;
if (head.compare_exchange_strong(head2, next1, std::memory_order_seq_cst))
{
count.fetch_sub(1, std::memory_order_seq_cst);
return head1;
}
else
{
array[head1].Next.exchange(next1, std::memory_order_seq_cst);
}
}
else
{
pthread_yield();
}
}
return -1;
}
void Push(int index)
{
int head1 = head.load(std::memory_order_acquire);
do
{
array[index].Next.store(head1, std::memory_order_release);
} while (!head.compare_exchange_strong(head1, index, std::memory_order_seq_cst));
count.fetch_add(1, std::memory_order_seq_cst);
}
};
struct test : rl::test_suite<test, 3>
{
SafeStack<int> stack;
test()
: stack (3)
{
}
void thread(unsigned idx)
{
for (size_t i = 0; i != 2; i += 1)
{
int elem;
for (;;)
{
elem = stack.Pop();
if (elem >= 0)
break;
pthread_yield();
}
stack.array[elem].Value = idx;
pthread_yield();
assert(stack.array[elem].Value == idx);
stack.Push(elem);
}
}
};
int main()
{
rl::test_params p;
p.iteration_count = 1000000;
//p.search_type = rl::sched_bound;
//p.context_bound = 5;
rl::simulate<test>(p);
}