AuroraRuntime/Source/Async/AuAsyncFuncRunnable.cpp

70 lines
1.6 KiB
C++
Raw Normal View History

/***
Copyright (C) 2021-2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuAsyncFuncRunnable.cpp
Date: 2023-12-06
Date: 2021-11-2
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "IAsyncRunnable.hpp"
#include "AuAsyncFuncRunnable.hpp"
namespace Aurora::Async
{
AsyncFuncRunnable::AsyncFuncRunnable(AuFunction<void()> &&callback) :
callback(AuMove(callback))
{ }
AsyncFuncRunnable::AsyncFuncRunnable(AuFunction<void()> &&callback, AuFunction<void()> &&fail) :
callback(AuMove(callback)), fail(AuMove(fail))
{ }
AsyncFuncRunnable::AsyncFuncRunnable(const AuFunction<void()> &callback) :
callback(callback)
{ }
AsyncFuncRunnable::AsyncFuncRunnable(const AuFunction<void()> &callback, const AuFunction<void()> &fail) :
callback(callback),
fail(fail)
{ }
void AsyncFuncRunnable::RunAsync()
{
AU_LOCK_GUARD(this->lock);
try
{
if (auto callback = AuExchange(this->callback, {}))
{
callback();
}
}
catch (...)
{
SysPushErrorCatch();
this->CancelAsync();
}
this->fail = {};
}
void AsyncFuncRunnable::CancelAsync()
{
AU_LOCK_GUARD(this->lock);
try
{
if (auto callback = AuExchange(this->fail, {}))
{
callback();
}
}
catch (...)
{
SysPushErrorCatch();
}
this->callback = {};
}
}