Added cancelling (and some exception handling) for coroutines.

This commit is contained in:
2023-11-03 00:48:13 +01:00
parent 54c63cfe69
commit 89bb110116
2 changed files with 141 additions and 47 deletions

View File

@@ -24,7 +24,10 @@ namespace mijin
// internal variables
//
thread_local TaskLoop::StoredTask* MultiThreadedTaskLoop::currentTask_ = nullptr;
namespace impl
{
thread_local TaskLoop::StoredTask* gCurrentTask = nullptr;
}
//
// internal functions
@@ -119,9 +122,9 @@ void MultiThreadedTaskLoop::workerThread(std::stop_token stopToken, std::size_t
}
// run it
currentTask_ = &*task;
impl::gCurrentTask = &*task;
tickTask(*task);
currentTask_ = nullptr;
impl::gCurrentTask = nullptr;
// and give it back
returningTasks_.push(std::move(*task));
@@ -180,8 +183,8 @@ void MultiThreadedTaskLoop::transferCurrentTask(TaskLoop& otherLoop) noexcept
MIJIN_ASSERT_FATAL(currentTask_ != nullptr, "Trying to call transferCurrentTask() while not running a task!");
// now start the transfer, first disown the task
StoredTask storedTask = std::move(*currentTask_);
currentTask_->task = nullptr; // just to be sure
StoredTask storedTask = std::move(*impl::gCurrentTask);
impl::gCurrentTask->task = nullptr; // just to be sure
// then send it over to the other loop
otherLoop.addStoredTask(std::move(storedTask));