-
Notifications
You must be signed in to change notification settings - Fork 1
/
EBStack.java
204 lines (187 loc) · 4 KB
/
EBStack.java
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright (c) 2008 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.amino.ds.lockfree;
import java.util.EmptyStackException;
import java.util.concurrent.atomic.AtomicReference;
import org.amino.utility.EliminationArray;
/**
* @author Zhi Gan (ganzhi@gmail.com)
*
* @param <E>
* Type of elements
*/
public class EBStack<E> implements IStack<E> {
private static final int INIT_SIZE_OF_ELIMARRAY = 8;
/**
* Wait for a while if CAS operation fails. (Millisecond)
*/
private static final int BACKOFF = 6;
/**
* Top pointer of stack.
*/
AtomicReference<Node<E>> top = new AtomicReference<Node<E>>(null);
/**
* Elimination array.
*/
EliminationArray ea;
/**
* Default constructor.
*/
public EBStack() {
this(INIT_SIZE_OF_ELIMARRAY);
}
/**
* dump array.
*/
public void dump() {
ea.dump();
}
/**
* Specify size of internal elimination array.
*
* @param size
* default size of stack
*/
public EBStack(int size) {
ea = new EliminationArray(size);
}
/**
* Back off for a while if CAS operation fails.
*/
static final boolean BACK_OFF = true;
/**
* Node definition for stack.
*
* @param <E>
* type of element in node
*/
static class Node<E> {
/**
* Data on the node.
*/
final E data;
/**
* Next pointer.
*/
Node<E> next;
/**
* @param d
* data
*/
public Node(E d) {
super();
this.data = d;
}
}
/**
* Pop data from the Stack.
*
* @return topmost element of the stack.
*/
public E pop() {
Node<E> oldTop, newTop;
// int oldStamp, newStamp;
int exp;
if (BACK_OFF)
exp = 1;
while (true) {
// oldStamp = top.getStamp();
// oldTop = top.getReference();
oldTop = top.get();
if (oldTop == null)
throw new EmptyStackException();
newTop = oldTop.next;
/**
* Modification to top pointer should be atomic operation. If CAS
* fails, thread will retry with BACKOFF until succeed.
*/
if (top.compareAndSet(oldTop, newTop))
break;
if (BACK_OFF) {
Object res;
try {
res = ea.tryRemove(BACKOFF * exp);
if (res != null)
return (E) res;
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
exp = exp << 1;
}
}
return oldTop.data;
}
/**
* Push data onto Stack.
*
* @param d
* data to be pushed onto the stack.
*/
public void push(E d) {
Node<E> oldTop, newTop;
newTop = new Node<E>(d);
int exp;
if (BACK_OFF)
exp = 1;
while (true) {
oldTop = top.get();
newTop.next = oldTop;
/**
* Modification to top pointer should be atomic operation. If CAS
* fails, thread will retry with BACKOFF until succeed.
*/
if (top.compareAndSet(oldTop, newTop))
return;
if (BACK_OFF) {
try {
if (ea.tryAdd(d, BACKOFF))
return;
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
exp = exp << 1;
}
}
}
/**
* Check to see if Stack is empty.
*
* @return true if stack is empty.
*/
public boolean isEmpty() {
// if (top.getReference() == null)
return top.get() == null;
// if (top.get() == null) {
// return true;
// } else {
// return false;
// }
}
/**
* Return copy of the top data on the Stack.
*
* @return copy of top of stack, or null if empty.
*/
public E peek() {
// if (top.getReference() == null)
if (top.get() == null) {
return null;
} else {
// return top.getReference().data;
return top.get().data;
}
}
}