Skip to content

Commit

Permalink
Slight modifications to Nate's improvement for BaseTable.awaitUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
rcaudy committed Nov 23, 2024
1 parent 1e32450 commit 88955c7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
4 changes: 2 additions & 2 deletions engine/api/src/main/java/io/deephaven/engine/table/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -820,12 +820,12 @@ RollupTable rollup(Collection<? extends Aggregation> aggregations, boolean inclu
* <p>
* In some implementations, this call may also terminate in case of interrupt or spurious wakeup.
*
* @param timeout The maximum time to wait in milliseconds.
* @param timeoutMillis The maximum time to wait in milliseconds.
* @return false if the timeout elapses without notification, true otherwise.
* @throws InterruptedException In the event this thread is interrupted
* @see java.util.concurrent.locks.Condition#await()
*/
boolean awaitUpdate(long timeout) throws InterruptedException;
boolean awaitUpdate(long timeoutMillis) throws InterruptedException;

/**
* Subscribe for updates to this table. {@code listener} will be invoked via the {@link NotificationQueue}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,26 +525,23 @@ public void awaitUpdate() throws InterruptedException {
}

@Override
public boolean awaitUpdate(long timeout) throws InterruptedException {
public boolean awaitUpdate(long timeoutMillis) throws InterruptedException {

final long startTime = System.nanoTime();
if (!updateGraph.exclusiveLock().tryLock(timeout, TimeUnit.MILLISECONDS)) {
if (!updateGraph.exclusiveLock().tryLock(timeoutMillis, TimeUnit.MILLISECONDS)) {
// Usually, users will already be holding the exclusive lock when calling this method. If they are not and
// cannot acquire the lock within the timeout, we should return false now.
return false;
}
timeout -= (System.nanoTime() - startTime) / 1_000_000;
timeoutMillis -= TimeUnit.MILLISECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);

boolean result;
try {
// Note that we must reacquire the exclusive lock before returning from await. This deadline may be
// exceeded if the thread must wait to reacquire the lock.
result = ensureCondition().await(timeout, TimeUnit.MILLISECONDS);
return ensureCondition().await(timeoutMillis, TimeUnit.MILLISECONDS);
} finally {
updateGraph.exclusiveLock().unlock();
}

return result;
}

private Condition ensureCondition() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ default void awaitUpdate() throws InterruptedException {

@SuppressWarnings("RedundantThrows")
@Override
default boolean awaitUpdate(long timeout) throws InterruptedException {
default boolean awaitUpdate(long timeoutMillis) throws InterruptedException {
return throwUnsupported();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,8 @@ public void awaitUpdate() throws InterruptedException {
}

@Override
public boolean awaitUpdate(long timeout) throws InterruptedException {
return coalesce().awaitUpdate(timeout);
public boolean awaitUpdate(long timeoutMillis) throws InterruptedException {
return coalesce().awaitUpdate(timeoutMillis);
}

@Override
Expand Down

0 comments on commit 88955c7

Please sign in to comment.