63 |
|
* |
64 |
|
* <p>StampedLocks are designed for use as internal utilities in the |
65 |
|
* development of thread-safe components. Their use relies on |
66 |
< |
* knowledge of the internal properties of the the data, objects, and |
66 |
> |
* knowledge of the internal properties of the data, objects, and |
67 |
|
* methods they are protecting. They are not reentrant, so locked |
68 |
|
* bodies should not call other unknown methods that may try to |
69 |
|
* re-acquire locks (although you may pass a stamp to other methods |
125 |
|
* } |
126 |
|
* |
127 |
|
* double distanceFromOriginV2() { // combines code paths |
128 |
+ |
* double currentX = 0.0, currentY = 0.0; |
129 |
|
* for (long stamp = sl.tryOptimisticRead(); ; stamp = sl.readLock()) { |
129 |
– |
* double currentX = 0.0, currentY = 0.0; |
130 |
|
* try { |
131 |
|
* currentX = x; |
132 |
|
* currentY = y; |
133 |
|
* } finally { |
134 |
|
* if (sl.tryConvertToOptimisticRead(stamp) != 0L) // unlock or validate |
135 |
< |
* return Math.sqrt(currentX * currentX + currentY * currentY); |
135 |
> |
* break; |
136 |
|
* } |
137 |
|
* } |
138 |
+ |
* return Math.sqrt(currentX * currentX + currentY * currentY); |
139 |
|
* } |
140 |
|
* |
141 |
|
* void moveIfAtOrigin(double newX, double newY) { // upgrade |
156 |
|
* } |
157 |
|
* } |
158 |
|
* } finally { |
159 |
< |
* sl.unlock(stamp); |
159 |
> |
* sl.unlock(stamp); |
160 |
|
* } |
161 |
|
* } |
162 |
|
* }}</pre> |
226 |
|
* threads. Both await methods use a similar spin strategy: If |
227 |
|
* the associated queue appears to be empty, then the thread |
228 |
|
* spin-waits up to SPINS times (where each iteration decreases |
229 |
< |
* spin count with 50% probablility) before enqueing, and then, if |
229 |
> |
* spin count with 50% probability) before enqueuing, and then, if |
230 |
|
* it is the first thread to be enqueued, spins again up to SPINS |
231 |
|
* times before blocking. If, upon wakening it fails to obtain |
232 |
|
* lock, and is still (or becomes) the first waiting thread (which |
995 |
|
else if ((time = deadline - System.nanoTime()) <= 0L) |
996 |
|
return cancelWriter(node, false); |
997 |
|
if (node.prev == p && p.status == WAITING && |
998 |
< |
(p != whead || (state & WBIT) != 0L)) // recheck |
998 |
> |
(p != whead || (state & ABITS) != 0L)) // recheck |
999 |
|
U.park(false, time); |
1000 |
|
if (interruptible && Thread.interrupted()) |
1001 |
|
return cancelWriter(node, true); |
1008 |
|
/** |
1009 |
|
* If node non-null, forces cancel status and unsplices from queue |
1010 |
|
* if possible. This is a variant of cancellation methods in |
1011 |
< |
* AbstractQueuedSynchronizer (see its detailed explanation in |
1011 |
> |
* AbstractQueuedSynchronizer (see its detailed explanation in AQS |
1012 |
|
* internal documentation) that more conservatively wakes up other |
1013 |
< |
* threads that may have had their links changed so as to preserve |
1013 |
> |
* threads that may have had their links changed, so as to preserve |
1014 |
|
* liveness in the main signalling methods. |
1015 |
|
*/ |
1016 |
|
private long cancelWriter(WNode node, boolean interrupted) { |
1204 |
|
private static sun.misc.Unsafe getUnsafe() { |
1205 |
|
try { |
1206 |
|
return sun.misc.Unsafe.getUnsafe(); |
1207 |
< |
} catch (SecurityException se) { |
1208 |
< |
try { |
1209 |
< |
return java.security.AccessController.doPrivileged |
1210 |
< |
(new java.security |
1211 |
< |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
1212 |
< |
public sun.misc.Unsafe run() throws Exception { |
1213 |
< |
java.lang.reflect.Field f = sun.misc |
1214 |
< |
.Unsafe.class.getDeclaredField("theUnsafe"); |
1215 |
< |
f.setAccessible(true); |
1216 |
< |
return (sun.misc.Unsafe) f.get(null); |
1217 |
< |
}}); |
1218 |
< |
} catch (java.security.PrivilegedActionException e) { |
1219 |
< |
throw new RuntimeException("Could not initialize intrinsics", |
1220 |
< |
e.getCause()); |
1221 |
< |
} |
1207 |
> |
} catch (SecurityException tryReflectionInstead) {} |
1208 |
> |
try { |
1209 |
> |
return java.security.AccessController.doPrivileged |
1210 |
> |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
1211 |
> |
public sun.misc.Unsafe run() throws Exception { |
1212 |
> |
Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class; |
1213 |
> |
for (java.lang.reflect.Field f : k.getDeclaredFields()) { |
1214 |
> |
f.setAccessible(true); |
1215 |
> |
Object x = f.get(null); |
1216 |
> |
if (k.isInstance(x)) |
1217 |
> |
return k.cast(x); |
1218 |
> |
} |
1219 |
> |
throw new NoSuchFieldError("the Unsafe"); |
1220 |
> |
}}); |
1221 |
> |
} catch (java.security.PrivilegedActionException e) { |
1222 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
1223 |
> |
e.getCause()); |
1224 |
|
} |
1225 |
|
} |
1223 |
– |
|
1226 |
|
} |