Compare commits

..

3 Commits

Author SHA1 Message Date
1360e5627d [*] Update async header 2021-09-29 09:02:27 +01:00
097f805209 [*] Improve MemoryView 2021-09-29 09:02:01 +01:00
8a6abcfdb7 [+] Added IWorkItem::Cancel 2021-09-29 09:01:42 +01:00
4 changed files with 62 additions and 17 deletions

View File

@ -70,6 +70,8 @@ namespace Aurora::Async
std::function<AuOptional<Result_t>(const Info_t &)> onFrame = 0;
};
using FVoidTask = FTask<AVoid, AVoid>;
template<class Info_t = AVoid, class Result_t = AVoid>
struct CTask
{
@ -93,6 +95,7 @@ namespace Aurora::Async
virtual bool BlockUntilComplete() = 0;
virtual bool HasFinished() = 0;
virtual bool HasFailed() = 0;
virtual void Cancel() = 0;
};
AUKN_SYM AuSPtr<IWorkItem> NewWorkItem(const DispatchTarget_t &worker, const AuSPtr<IWorkItemHandler> &task, bool supportsBlocking = false);
@ -154,6 +157,11 @@ namespace Aurora::Async
caller = GetAsyncApp()->GetCurrentThread();
}
BasicWorkCallback(Task_t &&task) : task(std::move(task))
{
caller = GetAsyncApp()->GetCurrentThread();
}
Task_t task;
Job_t callback;
@ -309,28 +317,38 @@ namespace Aurora::Async
}
};
template<typename... Args>
static std::function<void(Args...)> TranslateAsyncFunctionToDispatcher(std::function<void(Args...)> func)
#define ASYNC_ERROR(exp) if constexpr (std::is_same_v<T, bool>) { SysPushErrorGen(exp); return {}; } else { throw std::exception(exp); }
#define ASYNC_FINISH if constexpr (std::is_same_v<T, bool>) { return true; }
template<typename T = void, typename... Args, AU_TEMPLATE_ENABLE_WHEN(std::is_same_v<T, bool> || std::is_void<T>::value)>
static std::function<T(Args...)> TranslateAsyncFunctionToDispatcherWithThread(WorkerId_t id, std::function<void(Args...)> func)
{
auto cur = GetAsyncApp()->GetCurrentThread();
return [=](Args... in) -> void
return [=](Args... in) -> T
{
auto work = AuMakeShared<BasicWorkStdFunc>([=]() -> void {
func(in...);
});
auto workItem = NewWorkItem(cur, work);
if (!workItem) throw "can't dispatch async call; out of memory";
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;
};
}
template<typename B, typename... Args>
static std::function<void(std::function<void(const B&)>, Args...)> TranslateAsyncReturnableFunctionToDispatcher(std::function<B(Args...)> func)
template<typename T = void, typename... Args, AU_TEMPLATE_ENABLE_WHEN(std::is_same_v<T, bool> || std::is_void<T>::value)>
static std::function<T(Args...)> TranslateAsyncFunctionToDispatcher(std::function<void(Args...)> func)
{
auto cur = GetAsyncApp()->GetCurrentThread();
return [=](std::function<void(const B&)> callback, Args... in) -> void
return TranslateAsyncFunctionToDispatcherWithThread<T, Args>(GetAsyncApp()->GetCurrentThread(), func);
}
template<typename B = void, typename T, typename... Args, AU_TEMPLATE_ENABLE_WHEN(std::is_same_v<T, bool> || std::is_void<T>::value)>
static std::function<T(std::function<void(const B&)>, Args...)> TranslateAsyncReturnableFunctionToDispatcherWithThread(WorkerId_t id, std::function<AuOptional<B>(Args...)> func)
{
return [=](std::function<T(const B&)> callback, Args... in) -> T
{
auto work = AuMakeShared<BasicWorkCallback<AVoid, B>>();
if (!work) ASYNC_ERROR("can't dispatch async call; out of memory");
work.task.onProcess = [](const AVoid &) -> AuOptional<B>
{
return func(in...);
@ -339,11 +357,21 @@ namespace Aurora::Async
{
callback(ret);
};
auto workItem = NewWorkItem(cur, work);
if (!workItem) throw "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;
};
}
template<typename B = void, typename T, typename... Args, AU_TEMPLATE_ENABLE_WHEN(std::is_same_v<T, bool> || std::is_void<T>::value)>
static std::function<T(std::function<void(const B&)>, Args...)> TranslateAsyncReturnableFunctionToDispatcher(std::function<AuOptional<B>(Args...)> func)
{
return TranslateAsyncReturnableFunctionToDispatcherWithThread<B, T, Args>(GetAsyncApp()->GetCurrentThread(), func);
}
#undef ASYNC_ERROR
#undef ASYNC_FINISH
#endif

View File

@ -13,6 +13,9 @@ namespace Aurora::Memory
struct MemoryView
{
using Void_t = std::conditional_t<Readonly_b, const void *, void *>;
template<typename T, int Z>
using StdArray_t = std::conditional_t<Readonly_b, const std::array<T, Z>, std::array<T, Z>>;
/*
YadaYada(MemoryView(tempstring/array/etc)) should be legal, right?
@ -44,14 +47,14 @@ namespace Aurora::Memory
this->ptr = str.data();
this->length = str.size();
}
template<typename T, int Z>
constexpr MemoryView(T(&a)[Z])
{
this->ptr = &a[0];
this->length = Z * sizeof(T);
}
template<typename T>
constexpr MemoryView(T *start, T *end)
{
@ -116,7 +119,8 @@ namespace Aurora::Memory
template<bool Readonly_b>
struct MemoryViewStream : MemoryView<Readonly_b>
{
template<typename T, typename std::enable_if<AuIsBaseOfTemplate<AURORA_RUNTIME_AU_LIST, T>::value>::type* = nullptr>
// template<typename T, typename std::enable_if<AuIsBaseOfTemplate<AURORA_RUNTIME_AU_LIST, T>::value>::type* = nullptr>
template<typename T, AU_TEMPLATE_ENABLE_WHEN(AuIsBaseOfTemplate<AURORA_RUNTIME_AU_LIST, T>::value)>
constexpr MemoryViewStream(T &list, AuUInt &length) : MemoryView<Readonly_b>(list), outVariable(length)
{
outVariable = 0;
@ -155,7 +159,7 @@ namespace Aurora::Memory
{
outVariable = 0;
}
template<typename T, int Z>
constexpr MemoryViewStream(T(&a)[Z]) : MemoryView<Readonly_b>(a), outVariable(unused)
{

View File

@ -271,6 +271,12 @@ namespace Aurora::Async
return finished;
}
void WorkItem::Cancel()
{
AU_LOCK_GUARD(lock);
Fail();
}
bool WorkItem::HasFailed()
{
return failed;
@ -288,6 +294,11 @@ namespace Aurora::Async
AUKN_SYM AuSPtr<IWorkItem> NewWorkItem(const DispatchTarget_t &worker, const AuSPtr<IWorkItemHandler> &task, bool supportsBlocking)
{
if (!task)
{
SysPushErrorNested("Constructed workitem with a null task. running on stack and reserve heaps alone?");
return {};
}
return AuMakeShared<WorkItem>(worker, task, supportsBlocking);
}
}

View File

@ -28,6 +28,8 @@ namespace Aurora::Async
bool BlockUntilComplete() override;
bool HasFinished() override;
bool HasFailed() override;
void Cancel() override;
void RunAsync() override;
void CancelAsync() override;