Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent pinned CarrierThreads on JDK-21 while using Virtual Threads #4079

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions java/dagger/internal/DoubleCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import static dagger.internal.Preconditions.checkNotNull;

import dagger.Lazy;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.inject.Provider;

/**
Expand All @@ -31,6 +33,8 @@ public final class DoubleCheck<T> implements Provider<T>, Lazy<T> {
private volatile Provider<T> provider;
private volatile Object instance = UNINITIALIZED;

private final Lock lock = new ReentrantLock();

private DoubleCheck(Provider<T> provider) {
assert provider != null;
this.provider = provider;
Expand All @@ -41,7 +45,8 @@ private DoubleCheck(Provider<T> provider) {
public T get() {
Object result = instance;
if (result == UNINITIALIZED) {
synchronized (this) {
lock.lock();
try {
result = instance;
if (result == UNINITIALIZED) {
result = provider.get();
Expand All @@ -50,6 +55,8 @@ public T get() {
* can make it eligible for GC. */
provider = null;
}
} finally {
lock.unlock();
}
}
return (T) result;
Expand Down Expand Up @@ -99,4 +106,4 @@ public static <P extends Provider<T>, T> Lazy<T> lazy(P provider) {
}
return new DoubleCheck<T>(checkNotNull(provider));
}
}
}