Skip to content

Commit

Permalink
guarantee coroutine task is ran in main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Dec 7, 2024
1 parent 0d607bf commit 5a4fcf7
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions loader/include/Geode/utils/Task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
// ```
Expand All @@ -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<TaskPromise>::from_promise(*this).resume();
});
return {};
}
std::suspend_never final_suspend() noexcept { return {}; }
// TODO: do something here?
void unhandled_exception() {}
Expand Down

0 comments on commit 5a4fcf7

Please sign in to comment.