From 5a4fcf7c980ea4b99cb71a83397c2eb99daea244 Mon Sep 17 00:00:00 2001 From: matcool <26722564+matcool@users.noreply.github.com> Date: Sat, 7 Dec 2024 14:24:48 -0300 Subject: [PATCH] guarantee coroutine task is ran in main thread --- loader/include/Geode/utils/Task.hpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/loader/include/Geode/utils/Task.hpp b/loader/include/Geode/utils/Task.hpp index eb0788cab..f19713162 100644 --- a/loader/include/Geode/utils/Task.hpp +++ b/loader/include/Geode/utils/Task.hpp @@ -938,8 +938,7 @@ namespace geode { // by the coroutine is cancelled, the coroutine will be destroyed as well and execution // stops as soon as possible. // -// The body of the coroutine is ran in whatever thread it got called in. -// TODO: maybe guarantee main thread? +// The body of the coroutine is ran in the main thread. // // The coroutine can also yield progress values using `co_yield`: // ``` @@ -963,7 +962,12 @@ namespace geode { MyTask::cancel(m_handle.lock()); } - std::suspend_never initial_suspend() noexcept { return {}; } + std::suspend_always initial_suspend() noexcept { + queueInMainThread([this] { + std::coroutine_handle::from_promise(*this).resume(); + }); + return {}; + } std::suspend_never final_suspend() noexcept { return {}; } // TODO: do something here? void unhandled_exception() {}