AuroraRuntime/Source/Async/AuAsyncFuncWorker.cpp

62 lines
1.5 KiB
C++
Raw Normal View History

/***
Copyright (C) 2021-2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuAsyncFuncWorker.cpp
Date: 2023-12-06
Date: 2021-11-2
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "IAsyncRunnable.hpp"
#include "AuAsyncFuncWorker.hpp"
#include "ThreadPool.hpp"
namespace Aurora::Async
{
AsyncFuncWorker::AsyncFuncWorker(IThreadPoolInternal *owner,
const WorkerPId_t &worker,
AuVoidFunc &&func) :
WorkItem(owner, worker, {}),
func(func)
{ }
void AsyncFuncWorker::DispatchTask(IWorkItemHandler::ProcessInfo &info)
{
auto func = AuExchange(this->func, {});
if (!this->CheckAlive())
{
return;
}
if (func)
{
func();
}
}
void AsyncFuncWorker::Cleanup()
{
AuResetMember(this->func);
}
AUKN_SYM AuSPtr<IWorkItem> NewWorkFunction(const WorkerPId_t &worker,
AuVoidFunc func)
{
AU_DEBUG_MEMCRUNCH;
if (!func)
{
SysPushErrorArg("WorkItem has null function");
return {};
}
auto pWorker = worker.GetPool();
if (!pWorker)
{
pWorker = GetCurrentWorkerPId().GetPool();
}
return AuMakeSharedThrow<AsyncFuncWorker>(AuStaticPointerCast<ThreadPool>(pWorker).get(), worker, AuMove(func));
}
}