Skip to content

Commit

Permalink
Simplify offloading of tasks (java-native-access#547)
Browse files Browse the repository at this point in the history
Motivation:

How we did offload tasks was quite complicated and doesn't need to be so
complicated in the first place as we can just run all tasks in a loop.

Modifications:

Simplify implementation by running all tasks in a loop.

Result:

Code cleanup
  • Loading branch information
normanmaurer authored Jun 29, 2023
1 parent 0a18111 commit 52032f9
Showing 1 changed file with 24 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -986,28 +986,26 @@ private boolean runTasksDirectly() {
sslTaskExecutor == ImmediateEventExecutor.INSTANCE;
}

private void runAllTaskSend(Executor sslTaskExecutor, Runnable task) {
sslTaskExecutor.execute(decorateTaskSend(sslTaskExecutor, task));
private void runAllTaskSend(Runnable task) {
sslTaskExecutor.execute(decorateTaskSend(task));
}

private Runnable decorateTaskSend(Executor sslTaskExecutor, Runnable task) {
private void runAll(Runnable task) {
do {
task.run();
} while ((task = connection.sslTask()) != null);
}

private Runnable decorateTaskSend(Runnable task) {
return () -> {
try {
task.run();
runAll(task);
} finally {
// Move back to the EventLoop.
eventLoop().execute(() -> {
if (connection != null) {
Runnable nextTask = connection.sslTask();
// Consume all tasks before moving back to the EventLoop.
if (nextTask == null) {
// Call connection send to continue handshake if needed.
if (connectionSend()) {
forceFlushParent();
}
} else {
sslTaskExecutor.execute(decorateTaskSend(sslTaskExecutor, nextTask));
}
// Call connection send to continue handshake if needed.
if (connectionSend()) {
forceFlushParent();
}
});
}
Expand Down Expand Up @@ -1268,7 +1266,7 @@ private boolean connectionSend() {
// Let's try again sending after we did process all tasks.
return packetWasWritten | connectionSend();
} else {
runAllTaskSend(sslTaskExecutor, task);
runAllTaskSend(task);
}
} else {
// Notify about early data ready if needed.
Expand Down Expand Up @@ -1442,7 +1440,7 @@ void connectionRecv(InetSocketAddress recipient, InetSocketAddress sender, ByteB
} while ((task = connection.sslTask()) != null);
processReceived(connAddr);
} else {
runAllTaskRecv(sslTaskExecutor, task);
runAllTaskRecv(task);
}
} else {
processReceived(connAddr);
Expand Down Expand Up @@ -1507,29 +1505,23 @@ private void processReceived(long connAddr) {
}
}

private void runAllTaskRecv(Executor sslTaskExecutor, Runnable task) {
sslTaskExecutor.execute(decorateTaskRecv(sslTaskExecutor, task));
private void runAllTaskRecv(Runnable task) {
sslTaskExecutor.execute(decorateTaskRecv(task));
}

private Runnable decorateTaskRecv(Executor sslTaskExecutor, Runnable task) {
private Runnable decorateTaskRecv(Runnable task) {
return () -> {
try {
task.run();
runAll(task);
} finally {
// Move back to the EventLoop.
eventLoop().execute(() -> {
if (connection != null) {
Runnable nextTask = connection.sslTask();
// Consume all tasks before moving back to the EventLoop.
if (nextTask == null) {
processReceived(connection.address());

// Call connection send to continue handshake if needed.
if (connectionSend()) {
forceFlushParent();
}
} else {
sslTaskExecutor.execute(decorateTaskRecv(sslTaskExecutor, nextTask));
processReceived(connection.address());

// Call connection send to continue handshake if needed.
if (connectionSend()) {
forceFlushParent();
}
}
});
Expand Down

0 comments on commit 52032f9

Please sign in to comment.