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

Replace synchronization by Lock in FutureImpl #2912

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
35 changes: 28 additions & 7 deletions vavr/src/main/java/io/vavr/concurrent/FutureImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

import java.util.Objects;
import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;

/**
Expand All @@ -45,7 +47,7 @@ final class FutureImpl<T> implements Future<T> {
/**
* Used to synchronize state changes.
*/
private final Object lock = new Object();
private final Lock lock;

/**
* Indicates if this Future is cancelled
Expand Down Expand Up @@ -87,8 +89,10 @@ final class FutureImpl<T> implements Future<T> {

// single constructor
private FutureImpl(Executor executor, Option<Try<T>> value, Queue<Consumer<Try<T>>> actions, Queue<Thread> waiters, Computation<T> computation) {
this.lock = new ReentrantLock();
this.executor = executor;
synchronized (lock) {
lock.lock();
try {
this.cancelled = false;
this.value = value;
this.actions = actions;
Expand All @@ -98,6 +102,8 @@ private FutureImpl(Executor executor, Option<Try<T>> value, Queue<Consumer<Try<T
} catch (Throwable x) {
tryComplete(Try.failure(x));
}
} finally {
lock.unlock();
}
}

Expand Down Expand Up @@ -219,8 +225,11 @@ private void _await(long start, long timeout, TimeUnit unit) {
public boolean block() {
try {
if (!threadEnqueued) {
synchronized (lock) {
lock.lock();
try {
waiters = waiters.enqueue(waitingThread);
} finally {
lock.unlock();
}
threadEnqueued = true;
}
Expand Down Expand Up @@ -256,14 +265,17 @@ public boolean isReleasable() {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
if (!isCompleted()) {
synchronized (lock) {
lock.lock();
try {
if (!isCompleted()) {
if (mayInterruptIfRunning && this.thread != null) {
this.thread.interrupt();
}
this.cancelled = tryComplete(Try.failure(new CancellationException()));
return this.cancelled;
}
} finally {
lock.unlock();
}
}
return false;
Expand All @@ -272,7 +284,8 @@ public boolean cancel(boolean mayInterruptIfRunning) {
private void updateThread() {
// cancellation may have been initiated by a different thread before this.thread is set by the worker thread
if (!isCompleted()) {
synchronized (lock) {
lock.lock();
try {
if (!isCompleted()) {
this.thread = Thread.currentThread();
try {
Expand All @@ -281,6 +294,8 @@ private void updateThread() {
// we are not allowed to set the uncaught exception handler of the worker thread ¯\_(ツ)_/¯
}
}
} finally {
lock.unlock();
}
}
}
Expand Down Expand Up @@ -322,12 +337,15 @@ public Future<T> onComplete(Consumer<? super Try<T>> action) {
if (isCompleted()) {
perform(action);
} else {
synchronized (lock) {
lock.lock();
try {
if (isCompleted()) {
perform(action);
} else {
actions = actions.enqueue((Consumer<Try<T>>) action);
}
} finally {
lock.unlock();
}
}
return this;
Expand Down Expand Up @@ -362,7 +380,8 @@ boolean tryComplete(Try<? extends T> value) {
final Queue<Consumer<Try<T>>> actions;
final Queue<Thread> waiters;
// it is essential to make the completed state public *before* performing the actions
synchronized (lock) {
lock.lock();
try {
if (isCompleted()) {
actions = null;
waiters = null;
Expand All @@ -374,6 +393,8 @@ boolean tryComplete(Try<? extends T> value) {
this.waiters = null;
this.thread = null;
}
} finally {
lock.unlock();
}
if (waiters != null) {
waiters.forEach(this::unlock);
Expand Down