diff --git a/Source/Async/WorkItem.cpp b/Source/Async/WorkItem.cpp index 5d574ec1..ae6c8eec 100644 --- a/Source/Async/WorkItem.cpp +++ b/Source/Async/WorkItem.cpp @@ -478,7 +478,11 @@ namespace Aurora::Async } auto pWorker = GetCurrentWorkerPId().pool; - if (!pWorker) AuUnsafeRaiiToShared(gAsyncApp); + if (!pWorker) + { + pWorker = AuUnsafeRaiiToShared(gAsyncApp); + } + return AuMakeShared(ToInternal(pWorker).get(), WorkerPId_t { pWorker , worker }, task); } @@ -492,7 +496,18 @@ namespace Aurora::Async return {}; } - return AuMakeSharedThrow(ToInternal(worker.pool).get(), worker, AuMove(func)); + auto pWorker = worker.pool; + if (!pWorker) + { + pWorker = GetCurrentWorkerPId().pool; + } + + if (!pWorker) + { + pWorker = AuUnsafeRaiiToShared(gAsyncApp); + } + + return AuMakeSharedThrow(ToInternal(pWorker).get(), worker, AuMove(func)); } AUKN_SYM AuSPtr NewWorkItem(const WorkerPId_t &worker, const AuSPtr &task) @@ -504,14 +519,28 @@ namespace Aurora::Async SysPushErrorArg("WorkItem has null task. Running out of memory?"); return {}; } - - return AuMakeSharedThrow(ToInternal(worker.pool).get(), worker, task); + + auto pWorker = worker.pool; + if (!pWorker) + { + pWorker = GetCurrentWorkerPId().pool; + } + + if (!pWorker) + { + pWorker = AuUnsafeRaiiToShared(gAsyncApp); + } + + return AuMakeSharedThrow(ToInternal(pWorker).get(), worker, task); } AUKN_SYM AuSPtr NewFence() { auto pWorker = GetCurrentWorkerPId().pool; - if (!pWorker) AuUnsafeRaiiToShared(gAsyncApp); + if (!pWorker) + { + pWorker = AuUnsafeRaiiToShared(gAsyncApp); + } return AuMakeShared((IThreadPoolInternal *)ToInternal(pWorker).get(), WorkerPId_t {}, AuSPtr{}); }