1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain. Use, modify, and |
4 |
|
|
* redistribute this code in any way without acknowledgement. |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
package java.util.concurrent; |
8 |
|
|
import java.util.concurrent.atomic.*; |
9 |
|
|
|
10 |
|
|
/** |
11 |
|
|
* A linked list node supporting atomic operations on both item and |
12 |
|
|
* next fields, Used by non-blocking linked-list based classes. |
13 |
|
|
*/ |
14 |
|
|
|
15 |
|
|
final class AtomicLinkedNode { |
16 |
|
|
private volatile Object item; |
17 |
|
|
private volatile AtomicLinkedNode next; |
18 |
|
|
|
19 |
|
|
private final static AtomicReferenceFieldUpdater<AtomicLinkedNode, AtomicLinkedNode> nextUpdater = |
20 |
|
|
new AtomicReferenceFieldUpdater<AtomicLinkedNode, AtomicLinkedNode>(new AtomicLinkedNode[0], new AtomicLinkedNode[0], "next"); |
21 |
|
|
private final static AtomicReferenceFieldUpdater<AtomicLinkedNode, Object> itemUpdater |
22 |
|
|
= new AtomicReferenceFieldUpdater<AtomicLinkedNode, Object>(new AtomicLinkedNode[0], new Object[0], "item"); |
23 |
|
|
|
24 |
|
|
AtomicLinkedNode(Object x) { item = x; } |
25 |
|
|
|
26 |
|
|
AtomicLinkedNode(Object x, AtomicLinkedNode n) { item = x; next = n; } |
27 |
|
|
|
28 |
|
|
Object getItem() { |
29 |
|
|
return item; |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
boolean casItem(Object cmp, Object val) { |
33 |
|
|
return itemUpdater.compareAndSet(this, cmp, val); |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
void setItem(Object val) { |
37 |
|
|
itemUpdater.set(this, val); |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
AtomicLinkedNode getNext() { |
41 |
|
|
return next; |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
boolean casNext(AtomicLinkedNode cmp, AtomicLinkedNode val) { |
45 |
|
|
return nextUpdater.compareAndSet(this, cmp, val); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
void setNext(AtomicLinkedNode val) { |
49 |
|
|
nextUpdater.set(this, val); |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
} |