2021-11-05 17:34:23 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: AsyncRunnable.hpp
|
|
|
|
Date: 2021-11-2
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
namespace Aurora::Async
|
|
|
|
{
|
2023-05-25 00:55:55 +00:00
|
|
|
struct IAsyncRunnable
|
2021-11-05 17:34:23 +00:00
|
|
|
{
|
2023-05-25 00:55:55 +00:00
|
|
|
inline virtual EWorkPrio GetPrio()
|
|
|
|
{
|
|
|
|
return EWorkPrio::eNormalPrio;
|
|
|
|
};
|
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
virtual void RunAsync() = 0;
|
|
|
|
|
|
|
|
virtual void CancelAsync() {}
|
|
|
|
};
|
|
|
|
|
2023-05-25 00:55:55 +00:00
|
|
|
struct AsyncFuncRunnable : IAsyncRunnable
|
2021-11-05 17:34:23 +00:00
|
|
|
{
|
2022-01-19 17:08:13 +00:00
|
|
|
AuFunction<void()> callback;
|
|
|
|
AuFunction<void()> fail;
|
2023-05-25 00:55:55 +00:00
|
|
|
AuThreadPrimitives::Mutex lock;
|
2021-11-05 17:34:23 +00:00
|
|
|
|
2022-01-19 18:18:13 +00:00
|
|
|
AsyncFuncRunnable(AuFunction<void()> &&callback) : callback(AuMove(callback))
|
2021-11-05 17:34:23 +00:00
|
|
|
{}
|
|
|
|
|
2022-01-19 18:18:13 +00:00
|
|
|
AsyncFuncRunnable(AuFunction<void()> &&callback, AuFunction<void()> &&fail) : callback(AuMove(callback)), fail(AuMove(fail))
|
2021-11-05 17:34:23 +00:00
|
|
|
{}
|
|
|
|
|
2022-01-19 17:08:13 +00:00
|
|
|
AsyncFuncRunnable(const AuFunction<void()> &callback) : callback(callback)
|
2021-11-05 17:34:23 +00:00
|
|
|
{}
|
|
|
|
|
2022-01-19 17:08:13 +00:00
|
|
|
AsyncFuncRunnable(const AuFunction<void()> &callback, const AuFunction<void()> &fail) : callback(callback), fail(fail)
|
2021-11-05 17:34:23 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
void RunAsync() override
|
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(lock);
|
2022-02-19 17:56:45 +00:00
|
|
|
SysAssertDbgExp(callback, "Missing callback function");
|
2021-11-05 17:34:23 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Debug::PrintError();
|
|
|
|
}
|
|
|
|
fail = {};
|
|
|
|
callback = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void CancelAsync() override
|
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(lock);
|
|
|
|
if (fail)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Debug::PrintError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fail = {};
|
|
|
|
callback = {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|