cleanup AbstractPromise (still messy)

This commit is contained in:
WhatCats
2025-01-10 22:14:59 +01:00
parent 4d01a8a418
commit 5447e06455
3 changed files with 149 additions and 127 deletions

View File

@@ -42,12 +42,6 @@ public abstract class BasePromise<T, FS, FA> extends AbstractPromise<T, FS, FA>
this.listeners = Collections.EMPTY_LIST;
}
protected T joinCompletion() throws ExecutionException {
PromiseCompletion<T> completion = Objects.requireNonNull(getCompletion());
if (completion.isSuccess()) return completion.getResult();
throw new ExecutionException(completion.getException());
}
protected void handleCompletion(@NotNull PromiseCompletion<T> cmp) {
if (!COMPLETION_HANDLE.compareAndSet(this, null, cmp)) return;
sync.releaseShared(1);
@@ -99,31 +93,36 @@ public abstract class BasePromise<T, FS, FA> extends AbstractPromise<T, FS, FA>
@Override
public T get() throws InterruptedException, ExecutionException {
sync.acquireSharedInterruptibly(1);
return joinCompletion();
if (!isCompleted()) {
sync.acquireSharedInterruptibly(1);
}
return joinCompletionChecked();
}
@Override
public T get(long time, @NotNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
boolean success = sync.tryAcquireSharedNanos(1, unit.toNanos(time));
if (!success) {
throw new TimeoutException("Promise stopped waiting after " + time + " " + unit);
if (!isCompleted()) {
boolean success = sync.tryAcquireSharedNanos(1, unit.toNanos(time));
if (!success) {
throw new TimeoutException("Promise stopped waiting after " + time + " " + unit);
}
}
return joinCompletion();
return joinCompletionChecked();
}
@Override
public T await() {
try {
sync.acquireSharedInterruptibly(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
if (!isCompleted()) {
try {
sync.acquireSharedInterruptibly(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
PromiseCompletion<T> completion = Objects.requireNonNull(getCompletion());
if (completion.isSuccess()) return completion.getResult();
throw new CompletionException(completion.getException());
return joinCompletionUnchecked();
}
@Override