Skip to content

Commit

Permalink
track timeout generation
Browse files Browse the repository at this point in the history
  • Loading branch information
yawkat committed Feb 1, 2024
1 parent c8b5294 commit ea7d261
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public final class IOUringEventLoop extends SingleThreadEventLoop {
private final Runnable submitIOTask = () -> getRingBuffer().ioUringSubmissionQueue().submit();

private long prevDeadlineNanos = NONE;
/**
* This is a "generation" counter that is passed to addTimeout. It ensures that the expiry of a previous timeout
* doesn't make us think the current timeout has expired, which could lead to wrongly not removing the current
* timeout when it's adjusted again.
*/
private short prevTimeoutGeneration = -1;
private boolean pendingWakeup;

IOUringEventLoop(IOUringEventLoopGroup parent, Executor executor, int ringSize, int iosqeAsyncThreshold,
Expand Down Expand Up @@ -180,10 +186,12 @@ protected void run() {
if (!hasTasks()) {
if (curDeadlineNanos != prevDeadlineNanos) {
if (prevDeadlineNanos != NONE) {
submissionQueue.removeTimeout((short) 0);
submissionQueue.removeTimeout(prevTimeoutGeneration);
}
if (curDeadlineNanos != NONE) {
submissionQueue.addTimeout(deadlineToDelayNanos(curDeadlineNanos), (short) 0);
short generation = (short) (prevTimeoutGeneration + 1);
submissionQueue.addTimeout(deadlineToDelayNanos(curDeadlineNanos), generation);
prevTimeoutGeneration = generation;
}
prevDeadlineNanos = curDeadlineNanos;
}
Expand Down Expand Up @@ -253,7 +261,7 @@ private void handle(int fd, int res, int flags, byte op, short data) {
pendingWakeup = false;
addEventFdRead(ringBuffer.ioUringSubmissionQueue());
} else if (op == Native.IORING_OP_TIMEOUT) {
if (res == Native.ERRNO_ETIME_NEGATIVE) {
if (res == Native.ERRNO_ETIME_NEGATIVE && data == prevTimeoutGeneration) {
prevDeadlineNanos = NONE;
}
} else if (op == Native.IORING_OP_TIMEOUT_REMOVE) {
Expand Down

0 comments on commit ea7d261

Please sign in to comment.