Skip to content

Commit a9f75d1

Browse files
committed
Annotate LockSupport.
The docs for the `park*(Object blocker...)` overloads are silent on the possibility of `null`. In defense of my proposal to annotate `blocker` as `@Nullable`, I'd say: - Their implementations appear to tolerate it fine (by calling `setBlocker`, which is implemented equivalently to `setCurrentBlocker`, which is explicitly documented as supporting `null`, albeit specifically to clear the blocker after blocking). - Each method comes with an overload in which `blocker` is not provided.
1 parent 7463e70 commit a9f75d1

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/java.base/share/classes/java/util/concurrent/locks/LockSupport.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import jdk.internal.access.JavaLangAccess;
4040
import jdk.internal.access.SharedSecrets;
4141
import jdk.internal.misc.Unsafe;
42+
import org.jspecify.annotations.NullMarked;
43+
import org.jspecify.annotations.Nullable;
4244

4345
/**
4446
* Basic thread blocking primitives for creating locks and other
@@ -139,6 +141,7 @@
139141
*
140142
* @since 1.5
141143
*/
144+
@NullMarked
142145
public final class LockSupport {
143146
private LockSupport() {} // Cannot be instantiated.
144147

@@ -160,7 +163,7 @@ private static void setBlocker(Thread t, Object arg) {
160163
* @param blocker the blocker object
161164
* @since 14
162165
*/
163-
public static void setCurrentBlocker(Object blocker) {
166+
public static void setCurrentBlocker(@Nullable Object blocker) {
164167
U.putReferenceOpaque(Thread.currentThread(), PARKBLOCKER, blocker);
165168
}
166169

@@ -175,7 +178,7 @@ public static void setCurrentBlocker(Object blocker) {
175178
* @param thread the thread to unpark, or {@code null}, in which case
176179
* this operation has no effect
177180
*/
178-
public static void unpark(Thread thread) {
181+
public static void unpark(@Nullable Thread thread) {
179182
if (thread != null) {
180183
if (thread.isVirtual()) {
181184
JLA.unparkVirtualThread(thread);
@@ -213,7 +216,7 @@ public static void unpark(Thread thread) {
213216
* thread parking
214217
* @since 1.6
215218
*/
216-
public static void park(Object blocker) {
219+
public static void park(@Nullable Object blocker) {
217220
Thread t = Thread.currentThread();
218221
setBlocker(t, blocker);
219222
try {
@@ -260,7 +263,7 @@ public static void park(Object blocker) {
260263
* @param nanos the maximum number of nanoseconds to wait
261264
* @since 1.6
262265
*/
263-
public static void parkNanos(Object blocker, long nanos) {
266+
public static void parkNanos(@Nullable Object blocker, long nanos) {
264267
if (nanos > 0) {
265268
Thread t = Thread.currentThread();
266269
setBlocker(t, blocker);
@@ -309,7 +312,7 @@ public static void parkNanos(Object blocker, long nanos) {
309312
* to wait until
310313
* @since 1.6
311314
*/
312-
public static void parkUntil(Object blocker, long deadline) {
315+
public static void parkUntil(@Nullable Object blocker, long deadline) {
313316
Thread t = Thread.currentThread();
314317
setBlocker(t, blocker);
315318
try {
@@ -331,7 +334,7 @@ public static void parkUntil(Object blocker, long deadline) {
331334
* @throws NullPointerException if argument is null
332335
* @since 1.6
333336
*/
334-
public static Object getBlocker(Thread t) {
337+
public static @Nullable Object getBlocker(Thread t) {
335338
if (t == null)
336339
throw new NullPointerException();
337340
return U.getReferenceOpaque(t, PARKBLOCKER);

0 commit comments

Comments
 (0)