-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLockFreeLinkedList.java
198 lines (171 loc) · 4.48 KB
/
LockFreeLinkedList.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
package org.psly.concurrent;
import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class LockFreeLinkedList<E extends Comparable<E>> {
public LockFreeLinkedList() {
super();
this.tail = new Node<E>(null, null);
this.head = new Node<E>(null, new NodePacket<E>(this.tail, false));
}
public boolean insert(E e){
Node<E> node = new Node<E>(e, null);
Object[] nodes;
//查找该元素
for(;;){
nodes = search(e);
@SuppressWarnings("unchecked")
Node<E> left = (Node<E>) nodes[0];
@SuppressWarnings("unchecked")
NodePacket<E> leftNext = (NodePacket<E>) nodes[1];
if(leftNext.node != tail && (leftNext.node.value.compareTo(e) == 0))
return false;
node.next = new NodePacket<E>(leftNext.node, false);
if(left.casNext(leftNext, node, leftNext.deleting))
return true;
}
}
@SuppressWarnings("unchecked")
public boolean delete(E e){
Object[] nodes;
Node<E> left;
NodePacket<E> leftNext;
Node<E> right;
for(;;) {
nodes = search(e);
left = (Node<E>) nodes[0];
leftNext = (NodePacket<E>) nodes[1];
NodePacket<E> rightNext;
if((right = leftNext.node) == tail || (right.value.compareTo(e) != 0))
return false;
if((!(rightNext = right.next).deleting)
&& right.casNext(rightNext, rightNext.node, true))
break;
}
if(!left.casNext(leftNext, right.next.node, leftNext.deleting))
search(e);
return true;
}
private Object[] search(E key){
searchAgain:
for(;;){
Node<E> prev = head;
NodePacket<E> prevNext = head.next;
Node<E> curNode = prevNext.node;
/* 1: Find left_node and right_node */
for(;;){
if(curNode == tail)
break;
if(curNode.next.deleting){
curNode = curNode.next.node;
continue;
}
if(key.compareTo(curNode.value) <= 0){
break;
} else {
prev = curNode;
prevNext = curNode.next;
curNode = prevNext.node;
}
}
Node<E> rightNode = curNode;
//相邻的两个
if(prevNext.node == rightNode){
if((rightNode != tail) && rightNode.next.deleting)
continue searchAgain;
else
return new Object[]{prev, prevNext};
}
//不相邻,将中间的这些通通删除
if(prev.casNext(prevNext, prevNext = new NodePacket<E>(rightNode, prevNext.deleting))){
if((rightNode != tail) && rightNode.next.deleting)
continue searchAgain;
else
return new Object[]{prev, prevNext};
}
}
}
public String toString(){
NodePacket<E> nNext = head.next;
StringBuilder strBuild = new StringBuilder();
for(;;){
if(nNext.node == tail)
break;
//获取节点
Node<E> node = nNext.node;
strBuild.append(node.value).append("\n");
nNext = node.next;
}
return strBuild.toString();
}
public int size(){
NodePacket<E> nNext = head.next;
int num = 0;
for(;;){
if(nNext.node == tail)
break;
//获取节点
Node<E> node = nNext.node;
++num;
nNext = node.next;
}
return num;
}
public final Node<E> head;
public final Node<E> tail;
public static class Node<E>{
final E value;
volatile NodePacket<E> next;
private static final sun.misc.Unsafe UNSAFE;
public Node(E value, NodePacket<E> next) {
super();
this.value = value;
this.next = next;
}
private static final long nextOffset;
boolean casNext(NodePacket<E> cmp, Node<E> node, boolean deleting){
return casNext(cmp, new NodePacket<E>(node, deleting));
}
boolean casNext(NodePacket<E> cmp, NodePacket<E> val){
return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val);
}
static{
try{
UNSAFE = UtilUnsafe.getUnsafe();
nextOffset = UNSAFE.objectFieldOffset(Node.class.getDeclaredField("next"));
} catch(Exception e){
throw new Error(e);
}
}
}
public static class NodePacket<E>{
final Node<E> node;
final boolean deleting;
public NodePacket(Node<E> node, boolean deleting) {
super();
this.node = node;
this.deleting = deleting;
}
public Node<E> getNode() {
return node;
}
public boolean isDeleting() {
return deleting;
}
}
private static class UtilUnsafe {
private UtilUnsafe() {
}
/** Fetch the Unsafe. Use With Caution. */
public static Unsafe getUnsafe() {
if (UtilUnsafe.class.getClassLoader() == null)
return Unsafe.getUnsafe();
try {
final Field fld = Unsafe.class.getDeclaredField("theUnsafe");
fld.setAccessible(true);
return (Unsafe) fld.get(UtilUnsafe.class);
} catch (Exception e) {
throw new RuntimeException("Could not obtain access to sun.misc.Unsafe", e);
}
}
}
}