Skip to content

Commit

Permalink
Async: Add ReactivateAsyncPhase to allow skipping submission on react…
Browse files Browse the repository at this point in the history
…ivation

This prevents an assert about being in Submitting state when async request completes during re-activation run cycle.
  • Loading branch information
Pagghiu committed May 27, 2024
1 parent 75819d5 commit 3d48f77
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
21 changes: 19 additions & 2 deletions Libraries/Async/Async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,24 @@ int SC::AsyncEventLoop::Internal::getTotalNumberOfActiveHandle() const
return numberOfActiveHandles + numberOfExternals;
}

struct SC::AsyncEventLoop::Internal::ReactivateAsyncPhase
{
template <typename T>
SC::Result operator()(T& async)
{
async.state = AsyncRequest::State::Submitting;
if (KernelEvents::needsSubmissionWhenReactivating(async))
{
async.eventLoop->internal.submissions.queueBack(async);
}
else
{
async.eventLoop->internal.addActiveHandle(async);
}
return Result(true);
}
};

SC::Result SC::AsyncEventLoop::Internal::completeAndEventuallyReactivate(KernelEvents& kernelEvents,
AsyncRequest& async, Result&& returnCode)
{
Expand All @@ -532,8 +550,7 @@ SC::Result SC::AsyncEventLoop::Internal::completeAndEventuallyReactivate(KernelE
SC_TRY(completeAsync(kernelEvents, async, move(returnCode), reactivate));
if (reactivate)
{
async.state = AsyncRequest::State::Submitting;
submissions.queueBack(async);
SC_TRY(Internal::applyOnAsync(async, ReactivateAsyncPhase()));
}
else
{
Expand Down
1 change: 1 addition & 0 deletions Libraries/Async/Internal/AsyncInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ struct SC::AsyncEventLoop::Internal

struct SetupAsyncPhase;
struct TeardownAsyncPhase;
struct ReactivateAsyncPhase;
struct ActivateAsyncPhase;
struct CancelAsyncPhase;
struct CompleteAsyncPhase;
Expand Down
7 changes: 7 additions & 0 deletions Libraries/Async/Internal/AsyncLinux.inl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ struct SC::AsyncEventLoop::Internal::KernelEvents
template <typename T> [[nodiscard]] Result completeAsync(T&);
template <typename T> [[nodiscard]] Result cancelAsync(T&);

// If False, makes re-activation a no-op, that is a lightweight optimization.
// More importantly it prevents an assert about being Submitting state when async completes during re-activation run cycle.
template<typename T> static bool needsSubmissionWhenReactivating(T&)
{
return true;
}

template <typename T, typename P> [[nodiscard]] static Result executeOperation(T&, P& p);
// clang-format on
};
Expand Down
9 changes: 9 additions & 0 deletions Libraries/Async/Internal/AsyncPosix.inl
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,8 @@ struct SC::AsyncEventLoop::Internal::KernelEventsPosix
return KernelQueuePosix::stopSingleWatcherImmediate(async, async.fileDescriptor, INPUT_EVENTS_MASK);
}

static bool needsSubmissionWhenReactivating(AsyncFilePoll&) { return false; }

//-------------------------------------------------------------------------------------------------------
// File CLOSE
//-------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -833,6 +835,13 @@ struct SC::AsyncEventLoop::Internal::KernelEventsPosix
template <typename T> [[nodiscard]] Result completeAsync(T&) { return Result(true); }
template <typename T> [[nodiscard]] Result cancelAsync(T&) { return Result(true); }

// If False, makes re-activation a no-op, that is a lightweight optimization.
// More importantly it prevents an assert about being Submitting state when async completes during re-activation run cycle.
template<typename T> static bool needsSubmissionWhenReactivating(T&)
{
return true;
}

template <typename T, typename P> [[nodiscard]] static Result executeOperation(T&, P&) { return Result::Error("Implement executeOperation"); }
// clang-format on
};
17 changes: 12 additions & 5 deletions Libraries/Async/Internal/AsyncWindows.inl
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,17 @@ struct SC::AsyncEventLoop::Internal::KernelEvents
//-------------------------------------------------------------------------------------------------------
[[nodiscard]] static bool setupAsync(AsyncLoopTimeout&) { return true; }

//-------------------------------------------------------------------------------------------------------
// WAKEUP
//-------------------------------------------------------------------------------------------------------
[[nodiscard]] static bool setupAsync(AsyncLoopWakeUp&) { return true; }

Result activateAsync(AsyncLoopTimeout& async)
{
async.expirationTime = async.eventLoop->getLoopTime().offsetBy(async.relativeTimeout);
return Result(true);
}

//-------------------------------------------------------------------------------------------------------
// WAKEUP
//-------------------------------------------------------------------------------------------------------
[[nodiscard]] static bool setupAsync(AsyncLoopWakeUp&) { return true; }

//-------------------------------------------------------------------------------------------------------
// WORK
//-------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -649,6 +649,13 @@ struct SC::AsyncEventLoop::Internal::KernelEvents
template <typename T> [[nodiscard]] bool completeAsync(T&) { return true; }
template <typename T> [[nodiscard]] bool cancelAsync(T&) { return true; }

// If False, makes re-activation a no-op, that is a lightweight optimization.
// More importantly it prevents an assert about being Submitting state when async completes during re-activation run cycle.
template<typename T> static bool needsSubmissionWhenReactivating(T&)
{
return true;
}

template <typename T, typename P> [[nodiscard]] static Result executeOperation(T&, P&) { return Result::Error("Implement executeOperation"); }
// clang-format on
};
Expand Down

0 comments on commit 3d48f77

Please sign in to comment.