14 |
|
import java.util.NoSuchElementException; |
15 |
|
import java.util.concurrent.locks.LockSupport; |
16 |
|
import java.util.concurrent.atomic.AtomicReference; |
17 |
– |
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; |
17 |
|
|
18 |
|
/** |
19 |
|
* An unbounded {@linkplain TransferQueue} based on linked nodes. |
114 |
|
this.isData = isData; |
115 |
|
} |
116 |
|
|
118 |
– |
@SuppressWarnings("rawtypes") |
119 |
– |
static final AtomicReferenceFieldUpdater<Node, Node> |
120 |
– |
nextUpdater = AtomicReferenceFieldUpdater.newUpdater |
121 |
– |
(Node.class, Node.class, "next"); |
122 |
– |
|
117 |
|
final boolean casNext(Node<E> cmp, Node<E> val) { |
118 |
< |
return nextUpdater.compareAndSet(this, cmp, val); |
118 |
> |
return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val); |
119 |
|
} |
120 |
|
|
121 |
|
final void clearNext() { |
122 |
< |
nextUpdater.lazySet(this, this); |
122 |
> |
UNSAFE.putOrderedObject(this, nextOffset, this); |
123 |
> |
} |
124 |
> |
|
125 |
> |
// Unsafe mechanics |
126 |
> |
|
127 |
> |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
128 |
> |
private static final long nextOffset = |
129 |
> |
objectFieldOffset(UNSAFE, "next", Node.class); |
130 |
> |
|
131 |
> |
/** |
132 |
> |
* Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
133 |
> |
* Replace with a simple call to Unsafe.getUnsafe when integrating |
134 |
> |
* into a jdk. |
135 |
> |
* |
136 |
> |
* @return a sun.misc.Unsafe |
137 |
> |
*/ |
138 |
> |
private static sun.misc.Unsafe getUnsafe() { |
139 |
> |
try { |
140 |
> |
return sun.misc.Unsafe.getUnsafe(); |
141 |
> |
} catch (SecurityException se) { |
142 |
> |
try { |
143 |
> |
return java.security.AccessController.doPrivileged |
144 |
> |
(new java.security |
145 |
> |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
146 |
> |
public sun.misc.Unsafe run() throws Exception { |
147 |
> |
java.lang.reflect.Field f = sun.misc |
148 |
> |
.Unsafe.class.getDeclaredField("theUnsafe"); |
149 |
> |
f.setAccessible(true); |
150 |
> |
return (sun.misc.Unsafe) f.get(null); |
151 |
> |
}}); |
152 |
> |
} catch (java.security.PrivilegedActionException e) { |
153 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
154 |
> |
e.getCause()); |
155 |
> |
} |
156 |
> |
} |
157 |
|
} |
158 |
|
|
159 |
|
private static final long serialVersionUID = -3375979862319811754L; |
570 |
|
} |
571 |
|
|
572 |
|
/** |
573 |
< |
* @throws NullPointerException {@inheritDoc} |
574 |
< |
* @throwsIllegalArgumentException {@inheritDoc} |
573 |
> |
* @throws NullPointerException {@inheritDoc} |
574 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
575 |
|
*/ |
576 |
|
public int drainTo(Collection<? super E> c) { |
577 |
|
if (c == null) |
588 |
|
} |
589 |
|
|
590 |
|
/** |
591 |
< |
* @throws NullPointerException {@inheritDoc} |
592 |
< |
* @throwsIllegalArgumentException {@inheritDoc} |
591 |
> |
* @throws NullPointerException {@inheritDoc} |
592 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
593 |
|
*/ |
594 |
|
public int drainTo(Collection<? super E> c, int maxElements) { |
595 |
|
if (c == null) |
870 |
|
|
871 |
|
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
872 |
|
private static final long headOffset = |
873 |
< |
objectFieldOffset("head", LinkedTransferQueue.class); |
873 |
> |
objectFieldOffset(UNSAFE, "head", LinkedTransferQueue.class); |
874 |
|
private static final long tailOffset = |
875 |
< |
objectFieldOffset("tail", LinkedTransferQueue.class); |
875 |
> |
objectFieldOffset(UNSAFE, "tail", LinkedTransferQueue.class); |
876 |
|
private static final long cleanMeOffset = |
877 |
< |
objectFieldOffset("cleanMe", LinkedTransferQueue.class); |
877 |
> |
objectFieldOffset(UNSAFE, "cleanMe", LinkedTransferQueue.class); |
878 |
> |
|
879 |
|
|
880 |
< |
private static long objectFieldOffset(String field, Class<?> klazz) { |
880 |
> |
static long objectFieldOffset(sun.misc.Unsafe UNSAFE, |
881 |
> |
String field, Class<?> klazz) { |
882 |
|
try { |
883 |
|
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |
884 |
|
} catch (NoSuchFieldException e) { |