-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.d
98 lines (84 loc) · 2.06 KB
/
stack.d
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
shared struct SharedStack(T) {
private shared struct Node {
T _value;
Node* _next;
this(T value){ _value = cast(shared)value;};
}
private Node* _root;
void put(T value){
auto n = new shared Node(value);
shared(Node)* oldRoot;
do {
oldRoot = _root;
n._next = oldRoot;
} while(!cas(&_root,oldRoot,n));
}
T* popFront() {
typeof(return) ret;
shared(Node)* oldRoot;
do {
oldRoot = _root;
if (_root is null) return null;
ret = cast(T*)&oldRoot._value;
} while (!cas(&_root, oldRoot, oldRoot._next));
return ret;
}
T* front() {
if (_root is null)
return null;
return cast(T*)_root._value;
}
}
/*
Still need to learn from http://www.drdobbs.com/parallel/writing-a-generalized-concurrent-queue/211601363?pgno=2
shared struct SharedFifo(T) {
private shared struct Node {
T _value;
Node* _next;
this(T value){ _value = cast(shared)value;};
}
private Node* _root;
private Node* _divider;
private Node* _last;
this(int i) {
_root = _divider = _last = new shared Node(T.init);
_last._next = new shared Node(T.init);
}
~this() {
while (_root !is null) {
auto tmp = _root;
_root = _root._next;
tmp._next = null;
}
}
void put(T value){
// this is safe in a single producer environment because the consumer never reads the last item in the queue, see front and or popFront
_last._next = new shared Node(value);
_last = _last._next;
//writeln("put %s %s %s", _last, cast(T)_last._value);
while (_root != _divider) {
auto tmp = _root;
_root = _root._next;
tmp._next = null; // the closest to delete I've got
delete tmp;
}
}
T* popFront() {
if (_divider == _last) return null;
auto ret = &_divider._next._value;
_divider = _divider._next;
//writeln("pop: %s %s %s", _divider, _divider._next, cast(T)_divider._next._value);
return cast(T*)ret;
}
T* front() {
if (_divider == _last) return null;
auto ret = _divider._next._value;
return cast(T*)ret;
}
void opAssign(shared SharedFifo!T rhs) {
this._root = rhs._root;
this._divider = rhs._divider;
this._last = rhs._last;
}
}
*/