shz.linked.ConcurrentLDNode Maven / Gradle / Ivy
package shz.linked;
import shz.UnsafeHelp;
/**
* 元素类型为E支持并发的双向链表节点
*
* [24+E(类型字节)+对齐填充]*n(n为元素个数)
*
* B=16+24*n+(E+对齐填充)*n
*/
@SuppressWarnings({"restriction", "unchecked"})
public class ConcurrentLDNode implements DNode {
public volatile E val;
protected volatile ConcurrentLDNode next;
protected volatile ConcurrentLDNode prev;
protected ConcurrentLDNode(E val) {
this.val = val;
}
public static ConcurrentLDNode of(E e) {
return new ConcurrentLDNode<>(e);
}
public static ConcurrentLDNode of() {
return of(null);
}
private static final long valOffset;
private static final long nextOffset;
private static final long prevOffset;
static {
try {
Class> k = ConcurrentLDNode.class;
valOffset = UnsafeHelp.getUnsafe().objectFieldOffset(k.getDeclaredField("val"));
nextOffset = UnsafeHelp.getUnsafe().objectFieldOffset(k.getDeclaredField("next"));
prevOffset = UnsafeHelp.getUnsafe().objectFieldOffset(k.getDeclaredField("prev"));
} catch (Exception e) {
throw new Error(e);
}
}
public boolean casVal(E expect, E val) {
return UnsafeHelp.getUnsafe().compareAndSwapObject(this, valOffset, expect, val);
}
public boolean casNext(ConcurrentLDNode expect, ConcurrentLDNode next) {
return UnsafeHelp.getUnsafe().compareAndSwapObject(this, nextOffset, expect, next);
}
public boolean casPrev(ConcurrentLDNode expect, ConcurrentLDNode prev) {
return UnsafeHelp.getUnsafe().compareAndSwapObject(this, prevOffset, expect, prev);
}
@Override
public final ConcurrentLDNode next() {
return next;
}
@Override
public final void next(DNode node) {
next = (ConcurrentLDNode) node;
}
@Override
public final ConcurrentLDNode prev() {
return prev;
}
@Override
public final void prev(DNode node) {
prev = (ConcurrentLDNode) node;
}
@Override
public final ConcurrentLDNode addNext(DNode node) {
ConcurrentLDNode cldNode = (ConcurrentLDNode) node;
while (true) {
ConcurrentLDNode next = this.next;
cldNode.prev = this;
cldNode.next = next;
if (!casNext(next, cldNode)) continue;
if (next != null) next.prev = cldNode;
break;
}
return cldNode;
}
@Override
public final ConcurrentLDNode addPrev(DNode node) {
ConcurrentLDNode cldNode = (ConcurrentLDNode) node;
while (true) {
ConcurrentLDNode prev = this.prev;
cldNode.prev = prev;
cldNode.next = this;
if (!casPrev(prev, cldNode)) continue;
if (prev != null) prev.next = cldNode;
break;
}
return cldNode;
}
@Override
public final void poll() {
ConcurrentLDNode prev = this.prev;
ConcurrentLDNode next = this.next;
if (prev != null) {
if (prev.casNext(this, next)) if (next != null) next.prev = prev;
} else if (next != null) next.casPrev(this, null);
}
public final ConcurrentLDNode addNext(E e) {
return addNext(of(e));
}
@SafeVarargs
public final ConcurrentLDNode addNext(E... es) {
ConcurrentLDNode next = this;
for (E e : es) next = next.addNext(e);
return next;
}
public final ConcurrentLDNode addPrev(E e) {
return addPrev(of(e));
}
@SafeVarargs
public final ConcurrentLDNode addPrev(E... es) {
ConcurrentLDNode prev = this;
for (E e : es) prev = prev.addPrev(e);
return prev;
}
}