< prev index next >
src/java.base/share/classes/java/util/concurrent/locks/LockSupport.java
Print this page
8229442: AQS and lock classes refresh
Reviewed-by: martin
@@ -138,12 +138,29 @@
*/
public class LockSupport {
private LockSupport() {} // Cannot be instantiated.
private static void setBlocker(Thread t, Object arg) {
- // Even though volatile, hotspot doesn't need a write barrier here.
- U.putReference(t, PARKBLOCKER, arg);
+ U.putReferenceOpaque(t, PARKBLOCKER, arg);
+ }
+
+ /**
+ * Sets the object to be returned by invocations of {@link
+ * #getBlocker getBlocker} for the current thread. This method may
+ * be used before invoking the no-argument version of {@link
+ * LockSupport#park() park()} from non-public objects, allowing
+ * more helpful diagnostics, or retaining compatibility with
+ * previous implementations of blocking methods. Previous values
+ * of the blocker are not automatically restored after blocking.
+ * To obtain the effects of {@code park(b}}, use {@code
+ * setCurrentBlocker(b); park(); setCurrentBlocker(null);}
+ *
+ * @param blocker the blocker object
+ * @since 14
+ */
+ public static void setCurrentBlocker(Object blocker) {
+ U.putReferenceOpaque(Thread.currentThread(), PARKBLOCKER, blocker);
}
/**
* Makes available the permit for the given thread, if it
* was not already available. If the thread was blocked on
@@ -290,11 +307,11 @@
* @since 1.6
*/
public static Object getBlocker(Thread t) {
if (t == null)
throw new NullPointerException();
- return U.getReferenceVolatile(t, PARKBLOCKER);
+ return U.getReferenceOpaque(t, PARKBLOCKER);
}
/**
* Disables the current thread for thread scheduling purposes unless the
* permit is available.
@@ -392,28 +409,10 @@
public static void parkUntil(long deadline) {
U.park(true, deadline);
}
/**
- * Returns the pseudo-randomly initialized or updated secondary seed.
- * Copied from ThreadLocalRandom due to package access restrictions.
- */
- static final int nextSecondarySeed() {
- int r;
- Thread t = Thread.currentThread();
- if ((r = U.getInt(t, SECONDARY)) != 0) {
- r ^= r << 13; // xorshift
- r ^= r >>> 17;
- r ^= r << 5;
- }
- else if ((r = java.util.concurrent.ThreadLocalRandom.current().nextInt()) == 0)
- r = 1; // avoid zero
- U.putInt(t, SECONDARY, r);
- return r;
- }
-
- /**
* Returns the thread id for the given thread. We must access
* this directly rather than via method Thread.getId() because
* getId() has been known to be overridden in ways that do not
* preserve unique mappings.
*/
@@ -421,13 +420,11 @@
return U.getLong(thread, TID);
}
// Hotspot implementation via intrinsics API
private static final Unsafe U = Unsafe.getUnsafe();
- private static final long PARKBLOCKER = U.objectFieldOffset
- (Thread.class, "parkBlocker");
- private static final long SECONDARY = U.objectFieldOffset
- (Thread.class, "threadLocalRandomSecondarySeed");
- private static final long TID = U.objectFieldOffset
- (Thread.class, "tid");
+ private static final long PARKBLOCKER
+ = U.objectFieldOffset(Thread.class, "parkBlocker");
+ private static final long TID
+ = U.objectFieldOffset(Thread.class, "tid");
}
< prev index next >