Skip to content

Commit

Permalink
Async: Add atomic pending flags to avoid multiple invocations of wake…
Browse files Browse the repository at this point in the history
…up in the same loop iteration

This was an issue on windows, where we would get multiple entries in the kernel queue, due to  PostQueuedCompletionStatus being called multiple times.
  • Loading branch information
Pagghiu committed Apr 12, 2024
1 parent bee5228 commit c7870cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 13 additions & 4 deletions Libraries/Async/Async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,16 @@ SC::Result SC::AsyncEventLoop::createAsyncTCPSocket(SocketFlags::AddressFamily f
return associateExternallyCreatedTCPSocket(outDescriptor);
}

SC::Result SC::AsyncEventLoop::wakeUpFromExternalThread() { return internalSelf.wakeUpFromExternalThread(); }
SC::Result SC::AsyncEventLoop::wakeUpFromExternalThread()
{
if (not privateSelf.wakeUpPending.exchange(true))
{
// This executes if current thread is lucky enough to atomically exchange pending from false to true.
// This effectively allows coalescing calls from different threads into a single notification.
return internalSelf.wakeUpFromExternalThread();
}
return Result(true);
}

SC::Result SC::AsyncEventLoop::associateExternallyCreatedTCPSocket(SocketDescriptor& outDescriptor)
{
Expand Down Expand Up @@ -851,9 +860,7 @@ SC::Result SC::AsyncEventLoop::wakeUpFromExternalThread(AsyncLoopWakeUp& async)
AsyncLoopWakeUp& notifier = *static_cast<AsyncLoopWakeUp*>(&async);
if (not notifier.pending.exchange(true))
{
// This executes if current thread is lucky enough to atomically exchange pending from false to true.
// This effectively allows coalescing calls from different threads into a single notification.
SC_TRY(wakeUpFromExternalThread());
return wakeUpFromExternalThread();
}
return Result(true);
}
Expand All @@ -879,6 +886,8 @@ void SC::AsyncEventLoop::Private::executeWakeUps(AsyncResult& result)
notifier->pending.exchange(false); // allow executing the notification again
}
}

wakeUpPending.exchange(false);
}

void SC::AsyncEventLoop::Private::removeActiveHandle(AsyncRequest& async)
Expand Down
2 changes: 2 additions & 0 deletions Libraries/Async/Internal/AsyncPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ struct SC::AsyncEventLoop::Private
{
AsyncEventLoop* eventLoop = nullptr;

Atomic<bool> wakeUpPending = false;

int numberOfActiveHandles = 0;
int numberOfManualCompletions = 0;
int numberOfExternals = 0;
Expand Down

0 comments on commit c7870cd

Please sign in to comment.