2022-04-10 15:40:49 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
namespace Aurora::Async
|
|
|
|
{
|
|
|
|
#pragma region EASE_OF_READING
|
|
|
|
struct BasicWorkStdFunc : IWorkItemHandler
|
|
|
|
{
|
|
|
|
AuFunction<void()> callback;
|
|
|
|
AuFunction<void()> shutdown; // error
|
|
|
|
|
2022-05-30 08:46:24 +00:00
|
|
|
inline BasicWorkStdFunc(AuFunction<void()> &&callback, AuFunction<void()> &&shutdown) : callback(std::move(callback)), shutdown(std::move(shutdown))
|
2022-04-10 15:40:49 +00:00
|
|
|
{}
|
|
|
|
|
2022-05-30 08:46:24 +00:00
|
|
|
inline BasicWorkStdFunc(AuFunction<void()> &&callback) : callback(std::move(callback))
|
2022-04-10 15:40:49 +00:00
|
|
|
{}
|
|
|
|
|
2022-05-30 08:46:24 +00:00
|
|
|
inline BasicWorkStdFunc(const AuFunction<void()> &callback) : callback(callback)
|
2022-04-10 15:40:49 +00:00
|
|
|
{}
|
|
|
|
|
2022-05-30 08:46:24 +00:00
|
|
|
inline BasicWorkStdFunc(const AuFunction<void()> &callback, const AuFunction<void()> &shutdown) : callback(callback), shutdown(shutdown)
|
2022-04-10 15:40:49 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
private:
|
|
|
|
#if !defined(_CPPSHARP)
|
2022-05-16 23:41:27 +00:00
|
|
|
inline void DispatchFrame(ProcessInfo &info) override
|
2022-04-10 15:40:49 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Debug::PrintError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 23:41:27 +00:00
|
|
|
inline void OnFailure() override
|
2022-04-10 15:40:49 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (shutdown)
|
|
|
|
{
|
|
|
|
shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Debug::PrintError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// @hideinitializer
|
|
|
|
template<typename Frame_t = AuFunction<void()>, typename Cleanup_t = AuFunction<void()>>
|
|
|
|
struct WorkItemCallable : IWorkItemHandler
|
|
|
|
{
|
|
|
|
Frame_t frame;
|
|
|
|
Cleanup_t cleanup;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void DispatchFrame(ProcessInfo &info) override
|
|
|
|
{
|
|
|
|
if constexpr (AuIsBaseOfTemplate<AuFunction, Frame_t>::value)
|
|
|
|
{
|
|
|
|
if (!frame)
|
|
|
|
{
|
2022-06-12 13:53:35 +00:00
|
|
|
info.type = ETickType::eFinished;
|
2022-04-10 15:40:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
frame();
|
2022-06-12 13:53:35 +00:00
|
|
|
info.type = ETickType::eFinished;
|
2022-04-10 15:40:49 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 13:53:35 +00:00
|
|
|
void OnFailure() override
|
2022-04-10 15:40:49 +00:00
|
|
|
{
|
|
|
|
if constexpr (AuIsBaseOfTemplate<AuFunction, Cleanup_t>::value)
|
|
|
|
{
|
|
|
|
if (!cleanup)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define ASYNC_ERROR(exp) { if constexpr (AuIsSame_v<T, bool>) { SysPushErrorGen(exp); return {}; } else { throw AuString(exp); } }
|
|
|
|
#define ASYNC_FINISH { if constexpr (AuIsSame_v<T, bool>) { return true; } }
|
|
|
|
|
|
|
|
template<typename T = void, typename... Args, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v<T, bool> || AuIsVoid_v<T>)>
|
2022-05-30 08:46:24 +00:00
|
|
|
static AuFunction<T(Args&&...)> TranslateAsyncFunctionToDispatcherWithThread(WorkerPId_t id, AuFunction<void(Args...)> func)
|
2022-04-10 15:40:49 +00:00
|
|
|
{
|
|
|
|
if (!func) return {};
|
|
|
|
return [=](Args&&... in) -> T
|
|
|
|
{
|
|
|
|
auto work = AuMakeShared<BasicWorkStdFunc>([=]() -> void {
|
|
|
|
func(in...);
|
|
|
|
});
|
|
|
|
if (!work) ASYNC_ERROR("can't dispatch async call. out of memory");
|
|
|
|
auto workItem = NewWorkItem(id, work);
|
|
|
|
if (!workItem) ASYNC_ERROR("can't dispatch async call. out of memory");
|
|
|
|
workItem->Dispatch();
|
|
|
|
ASYNC_FINISH;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Async app only
|
|
|
|
template<typename T = void, typename... Args, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v<T, bool> || AuIsVoid_v<T>)>
|
|
|
|
static AuFunction<T(Args&&...)> TranslateAsyncFunctionToDispatcher(AuFunction<void(Args...)> func)
|
|
|
|
{
|
|
|
|
return TranslateAsyncFunctionToDispatcherWithThread(GetAsyncApp()->GetCurrentThread(), func);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Async app only
|
|
|
|
template<typename B = void, typename T, typename... Args, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v<T, bool> || AuIsVoid_v<T>)>
|
2022-05-30 08:46:24 +00:00
|
|
|
static AuFunction<T(AuFunction<void(const B&)>, Args...)> TranslateAsyncReturnableFunctionToDispatcherWithThread(WorkerPId_t id, AuFunction<B(Args...)> func)
|
2022-04-10 15:40:49 +00:00
|
|
|
{
|
|
|
|
return [=](AuFunction<T(const B&)> callback, Args... in) -> T
|
|
|
|
{
|
|
|
|
auto work = AuMakeShared<WorkPairImpl<AVoid, B>>();
|
|
|
|
if (!work) ASYNC_ERROR("can't dispatch async call; out of memory");
|
|
|
|
work.task.onProcess = [=](const AVoid &) -> B
|
|
|
|
{
|
|
|
|
if (!func) return B{};
|
|
|
|
return func(in...);
|
|
|
|
};
|
|
|
|
work.callback.onSuccess = [=](const AVoid &, const B &ret)
|
|
|
|
{
|
|
|
|
callback(ret);
|
|
|
|
};
|
|
|
|
auto workItem = NewWorkItem(id, work);
|
|
|
|
if (!workItem) ASYNC_ERROR("can't dispatch async call; out of memory");
|
|
|
|
workItem->Dispatch();
|
|
|
|
ASYNC_FINISH;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef ASYNC_ERROR
|
|
|
|
#undef ASYNC_FINISH
|
|
|
|
|
|
|
|
#pragma endregion EASE_OF_READING
|
2022-03-20 09:56:15 +00:00
|
|
|
}
|