Skip to content

Commit

Permalink
Add a new async awaiter for returning to a named thread from somewher…
Browse files Browse the repository at this point in the history
…e else
  • Loading branch information
landelare committed Oct 15, 2023
1 parent 1ac8f98 commit 8ee35de
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Plugins/UE5Coro/Source/UE5Coro/Private/AsyncAwaiters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ FAsyncAwaiter Async::MoveToGameThread()
return FAsyncAwaiter(ENamedThreads::GameThread);
}

FAsyncAwaiter Async::MoveToSimilarThread()
{
return FAsyncAwaiter(FTaskGraphInterface::Get().GetCurrentThreadIfKnown());
}

FAsyncYieldAwaiter Async::Yield()
{
return {};
Expand Down
6 changes: 6 additions & 0 deletions Plugins/UE5Coro/Source/UE5Coro/Public/UE5Coro/AsyncAwaiters.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ UE5CORO_API Private::FAsyncAwaiter MoveToThread(ENamedThreads::Type);
* the game thread. */
UE5CORO_API Private::FAsyncAwaiter MoveToGameThread();

/** Convenience function to resume on the same kind of named thread that this
* function was called on.<br>
* co_await MoveToSimilarThread() is not useful. The return value should be
* stored to "remember" the original thread, then co_awaited later. */
UE5CORO_API Private::FAsyncAwaiter MoveToSimilarThread();

/** Always suspends the coroutine and resumes it on the same kind of named
* thread that it's currently running on, or AnyThread otherwise.<br>
* The return value of this function is reusable and always refers to the
Expand Down
18 changes: 18 additions & 0 deletions Plugins/UE5Coro/Source/UE5CoroTests/Private/AsyncAwaiterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@ void DoTest(FAutomationTestBase& Test)
Test.TestTrue(TEXT("Triggered"), CoroToTest->Wait());
}

{
FEventRef CoroToTest;
std::atomic<bool> bMovedOut = false;
std::atomic<bool> bMovedIn = false;
World.Run(CORO
{
auto Return = Async::MoveToSimilarThread();
co_await Async::MoveToNewThread();
bMovedOut = !IsInGameThread();
co_await Return;
bMovedIn = IsInGameThread();
CoroToTest->Trigger();
});
FTestHelper::PumpGameThread(World, [&] { return CoroToTest->Wait(0); });
Test.TestTrue(TEXT("Moved out"), bMovedOut);
Test.TestTrue(TEXT("Moved back in"), bMovedIn);
}

{
std::atomic<int> State = 0;
World.Run(CORO
Expand Down

0 comments on commit 8ee35de

Please sign in to comment.