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

Fix concurrency issue in SafeLazy #740

Merged
merged 1 commit into from Feb 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 18 additions & 18 deletions internal/compiler-interface/src/main/java/xsbti/api/SafeLazy.java
Expand Up @@ -42,30 +42,30 @@ public T get() {
});
}

private static final class Impl<T> extends xsbti.api.AbstractLazy<T> {
private Supplier<T> thunk = null;
private T result = null;

Impl(Supplier<T> thunk) {
private static final class Thunky<T> {
final Supplier<T> thunk;
final T result;
Thunky(Supplier<T> thunk, T result) {
this.thunk = thunk;
this.result = result;
}
}

/**
* Return cached result or force lazy evaluation.
*
* Don't call it in a multi-threaded environment.
*/
public synchronized T get() {
if (thunk == null) return result;
else {
result = thunk.get();
private static final class Impl<T> extends xsbti.api.AbstractLazy<T> {
private Thunky<T> thunky;

thunk = null; // also allows it to be GC'ed
Impl(Supplier<T> thunk) {
this.thunky = new Thunky(thunk, null);
}

return result;
public T get() {
Thunky<T> t = thunky;
if (t.result == null) {
T r = t.thunk.get();
t = new Thunky(null, r);
thunky = t;
}
return t.result;
}
}
}