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

Made LazyValue virtual thread friendly #34479

Merged
merged 1 commit into from
Jul 26, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.arc.impl;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;

/**
Expand All @@ -10,6 +12,8 @@ public class LazyValue<T> {

private final Supplier<T> supplier;

private final Lock lock = new ReentrantLock();

private transient volatile T value;

public LazyValue(Supplier<T> supplier) {
Expand All @@ -21,11 +25,15 @@ public T get() {
if (valueCopy != null) {
return valueCopy;
}
synchronized (this) {

lock.lock();
try {
if (value == null) {
value = supplier.get();
}
return value;
} finally {
lock.unlock();
}
}

Expand All @@ -34,9 +42,9 @@ public T getIfPresent() {
}

public void clear() {
synchronized (this) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can check outside of this critical zone if it has already happened and save entering into it.
@mkouba do we expect to be able to init it again, after a clear?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that can happen - first thing coming to my mind is dynamic resolution and caching of its result which can then be cleared and subsequently cached again - the cache is a LazyValue.
See https://github.com/quarkusio/quarkus/blob/main/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/InstanceImpl.java#L304

value = null;
}
lock.lock();
value = null;
lock.unlock();
}

public boolean isSet() {
Expand Down