[+] AuAsync IPC promises - New Early Impl

> Added AuNullPromise
> Added AuPromise<Out_t>
> Added AuInOutPromise<In_t, Out_t>
This commit is contained in:
Reece Wilson 2022-07-19 16:29:20 +01:00
parent 433898709e
commit 8fcebb91a3
2 changed files with 624 additions and 0 deletions

View File

@ -41,6 +41,8 @@ namespace Aurora::Async
}
#if !defined(_CPPSHARP)
#include "IPCPromises.hpp"
#include "JobFrom.hpp"
#include "TaskFrom.hpp"
#include "WorkPairImpl.hpp"

View File

@ -0,0 +1,622 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: IPCPromises.hpp
Date: 2022-07-19
Author: Reece
***/
#pragma once
namespace Aurora::Async
{
struct IPromiseCompleter
{
virtual void Complete() = 0;
virtual void Fail() = 0;
};
struct IPromiseComplete
{
virtual void OnSuccess(void *in) = 0;
};
struct IPromiseFailure
{
virtual void OnFailure(void *in) = 0;
};
struct IPromisePtrs
{
virtual void *GetSuccessPtr() = 0;
virtual void *GetErrorPtr() = 0;
virtual AuSPtr<void> GetData() = 0;
};
template <class SuccessValue_t>
struct PromiseSuccessTmpl : IPromiseComplete
{
using OnSuccess_f = AuFunction<void(const AuSPtr<SuccessValue_t> &)>;
inline virtual void OnSuccess(void *in);
inline void Init(IPromisePtrs *parent, const OnSuccess_f &onDelegate) noexcept
{
this->parent_ = parent;
this->onSuccess_ = onDelegate;
}
private:
IPromisePtrs *parent_;
OnSuccess_f onSuccess_;
};
template <class ErrorValue_t>
struct PromiseFailTmpl : IPromiseFailure
{
using OnFailure_f = AuFunction<void(const AuSPtr<ErrorValue_t> &)>;
inline virtual void OnFailure(void *in) override;
inline void Init(IPromisePtrs *parent, const OnFailure_f &onDelegate) noexcept
{
this->parent_ = parent;
this->onFailure_ = onDelegate;
}
private:
IPromisePtrs *parent_;
OnFailure_f onFailure_;
};
template <class SuccessValue_t, class ErrorValue_t>
struct Promise;
template <class SuccessValue_t, class ErrorValue_t>
struct IPromiseAccessor : IPromiseCompleter
{
inline virtual AuSPtr<SuccessValue_t> WriteIntoSuccess() = 0;
inline virtual AuSPtr<ErrorValue_t> WriteIntoError() = 0;
};
struct IPromiseDelegate
{
virtual void OnPromiseDelegate(const AuSPtr<IPromiseCompleter> &promise) = 0;
};
template <class SuccessValue_t, class ErrorValue_t>
struct PromiseCallTmpl : IPromiseDelegate
{
using OnDelegate_f = AuFunction<void(const AuSPtr<IPromiseAccessor<SuccessValue_t, ErrorValue_t>> &)>;
inline virtual void OnPromiseDelegate(const AuSPtr<IPromiseCompleter> &promise) override;
inline void Init(IPromisePtrs *parent, const OnDelegate_f &onDelegate) noexcept
{
this->parent_ = parent;
this->onDelegate_ = onDelegate;
}
private:
IPromisePtrs *parent_;
OnDelegate_f onDelegate_;
};
struct IPromisePrivateCallbacks
{
virtual void OnCBTick() = 0;
virtual void OnCBDelegate() = 0;
virtual void OnCBFailure() = 0;
};
struct PromiseStartupAdapter : IWorkItemHandler
{
inline PromiseStartupAdapter(IPromisePrivateCallbacks *parent) noexcept
{
this->parent_ = parent;
}
inline virtual void DispatchFrame(IWorkItemHandler::ProcessInfo &info) override
{
this->parent_->OnCBDelegate();
};
inline virtual void OnFailure() override
{
SysPushErrorGeneric("Didnt fire initial delegate");
this->parent_->OnCBFailure();
};
private:
IPromisePrivateCallbacks *parent_;
};
struct PromiseTickAdapter : IWorkItemHandler
{
inline PromiseTickAdapter(IPromisePrivateCallbacks *parent) noexcept
{
this->parent_ = parent;
}
inline virtual void DispatchFrame(IWorkItemHandler::ProcessInfo &info) override
{
this->parent_->OnCBTick();
};
inline virtual void OnFailure() override
{
SysPushErrorGeneric("Didnt fire initial delegate");
this->parent_->OnCBFailure();
};
private:
IPromisePrivateCallbacks *parent_;
};
template <class SuccessValue_t, class ErrorValue_t>
struct Promise final : IPromiseAccessor<SuccessValue_t, ErrorValue_t>, AuEnableSharedFromThis<Promise<SuccessValue_t, ErrorValue_t>>, private IPromisePtrs, private IPromisePrivateCallbacks
{
using This_t = Promise<SuccessValue_t, ErrorValue_t>;
AU_SAFE_DESTROY(Promise);
Promise(bool inlineResponse = false, bool allowSync = false);
using FunctionalCall_t = PromiseCallTmpl<SuccessValue_t, ErrorValue_t>;
using FunctionalResponse_t = PromiseSuccessTmpl<SuccessValue_t>;
using FunctionalFail_t = PromiseFailTmpl<ErrorValue_t>;
using FunctionalResponse_f = FunctionalResponse_t::OnSuccess_f;
using FunctionalFail_f = FunctionalFail_t::OnFailure_f;
using FunctionalDelegate_f = FunctionalCall_t::OnDelegate_f;
inline AuSPtr<This_t> OnSuccessFunctional(const FunctionalResponse_f &in);
inline AuSPtr<This_t> OnSuccessCallback(const AuSPtr<IPromiseComplete> &in);
inline AuSPtr<This_t> OnFailureFunctional(const FunctionalFail_f &in);
inline AuSPtr<This_t> OnFailureCallback(const AuSPtr<IPromiseFailure> &in);
inline AuSPtr<SuccessValue_t> WriteIntoSuccess() override;
inline AuSPtr<ErrorValue_t> WriteIntoError() override;
inline void Complete() override;
inline void Fail() override;
//inline AuSPtr<IWorkItem> GetBarrier() = 0;
inline bool WaitBeforeWork(const AuSPtr<IWorkItem> &startingBarrier);
inline bool BeginWork(WorkerPId_t worker = GetCurrentWorkerPId());
inline bool BeginWorkEx(const AuSPtr<IPromiseDelegate> &promise, WorkerPId_t worker = GetCurrentWorkerPId());
inline bool BeginWorkFunc(const FunctionalDelegate_f &promise, WorkerPId_t worker = GetCurrentWorkerPId());
inline bool FinalizeWithWork(const AuSPtr<IWorkItem> &sendingFires);
private:
bool bHasBeganWork {};
AuSPtr<IWorkItem> startingBarrier_;
PromiseStartupAdapter startupAdapter;
PromiseTickAdapter tickAdapter;
inline virtual void OnCBTick() override;
inline virtual void OnCBDelegate() override;
inline virtual void OnCBFailure() override;
inline void DestroyPrivate();
inline void *GetSuccessPtr() override;
inline void *GetErrorPtr() override;
inline AuSPtr<void> GetData() override;
void DoStartBlocking();
void DoFailureBlocking();
void DoSuccessBlocking();
void DoEndBlocking();
WorkerPId_t currentThread_;
SuccessValue_t success;
ErrorValue_t error;
bool bHasSuccess_ {};
bool bHasError_ {};
bool bHasFiredSuccess_ {};
bool bHasFiredError_ {};
bool bInlineResponse_ {};
FunctionalResponse_t responseFunctional_;
AuSPtr<IPromiseComplete> responseCallback_;
FunctionalFail_t failureFunctional_;
AuSPtr<IPromiseFailure> failureCallback_;
FunctionalCall_t delegateFunctional_;
AuSPtr<IPromiseDelegate> delegateCallback_;
bool bHasSentError_ {};
bool bHasSentSuccess_ {};
Aurora::Threading::Primitives::SpinLock lock_;
AuSPtr<IWorkItem> notifier_;
AuList<AuSPtr<IWorkItem>> startBarriers_;
};
template<class SuccessValue_t, class ErrorValue_t>
inline Promise<SuccessValue_t, ErrorValue_t>::Promise(bool inlineResponse, bool allowSync) : startupAdapter(this), tickAdapter(this)
{
this->bInlineResponse_ = inlineResponse;
auto pid = GetCurrentWorkerPId();
}
template<class SuccessValue_t, class ErrorValue_t>
inline void Promise<SuccessValue_t, ErrorValue_t>::DestroyPrivate()
{
if (this->notifier_)
{
this->notifier_->Cancel();
}
this->notifier_.reset();
if (!this->bHasFiredSuccess_ &&
!this->bHasFiredError_)
{
this->bHasFiredError_ = true;
DoFailureBlocking();
}
}
template<class SuccessValue_t, class ErrorValue_t>
inline void Promise<SuccessValue_t, ErrorValue_t>::DoStartBlocking()
{}
template<class SuccessValue_t, class ErrorValue_t>
inline void Promise<SuccessValue_t, ErrorValue_t>::DoEndBlocking()
{
this->failureCallback_.reset();
this->responseCallback_.reset();
this->currentThread_ = {};
}
template<class SuccessValue_t, class ErrorValue_t>
inline void Promise<SuccessValue_t, ErrorValue_t>::OnCBTick()
{
if (AuExchange(this->bHasFiredError_, false))
{
DoFailureBlocking();
}
else if (AuExchange(this->bHasFiredSuccess_, false))
{
DoSuccessBlocking();
}
else
{
SysPanic("");
}
this->bHasSentSuccess_ = true;
this->bHasSentError_ = true;
}
template<class SuccessValue_t, class ErrorValue_t>
inline void Promise<SuccessValue_t, ErrorValue_t>::OnCBDelegate()
{
//SysAssert(this->currentThread_ == GetCurrentWorkerPId());
SysAssert(this->delegateCallback_);
AuExchange(this->delegateCallback_, decltype(this->delegateCallback_) {})->OnPromiseDelegate(AuSharedFromThis());
}
template<class SuccessValue_t, class ErrorValue_t>
inline void Promise<SuccessValue_t, ErrorValue_t>::OnCBFailure()
{
Destroy();
}
template<class SuccessValue_t, class ErrorValue_t>
inline void Promise<SuccessValue_t, ErrorValue_t>::DoFailureBlocking()
{
{
AU_LOCK_GUARD(this->lock_);
if (AuExchange(this->bHasSentError_, true))
{
return;
}
}
this->DoStartBlocking();
if (this->failureCallback_)
{
this->failureCallback_->OnFailure(this->bHasError_ ? &this->error : nullptr);
}
this->DoEndBlocking();
}
template<class SuccessValue_t, class ErrorValue_t>
inline void Promise<SuccessValue_t, ErrorValue_t>::DoSuccessBlocking()
{
{
AU_LOCK_GUARD(this->lock_);
if (AuExchange(this->bHasSentError_, true))
{
return;
}
if (AuExchange(this->bHasSentSuccess_, true))
{
return;
}
}
this->DoStartBlocking();
if (this->responseCallback_)
{
this->responseCallback_->OnSuccess(this->bHasSuccess_ ? &this->success : nullptr);
}
this->DoEndBlocking();
}
template<class SuccessValue_t, class ErrorValue_t>
inline void *Promise<SuccessValue_t, ErrorValue_t>::GetSuccessPtr()
{
return &this->success;
}
template<class SuccessValue_t, class ErrorValue_t>
inline void *Promise<SuccessValue_t, ErrorValue_t>::GetErrorPtr()
{
return &this->error;
}
template<class SuccessValue_t, class ErrorValue_t>
inline AuSPtr<void> Promise<SuccessValue_t, ErrorValue_t>::GetData()
{
return AuSPtr<void>(AuSharedFromThis());
}
template<class SuccessValue_t, class ErrorValue_t>
inline void Promise<SuccessValue_t, ErrorValue_t>::Complete()
{
SysAssert(!this->bHasFiredSuccess_);
SysAssert(!this->bHasFiredError_);
this->bHasFiredSuccess_ = true;
if (this->bInlineResponse_ &&
this->currentThread_ == GetCurrentWorkerPId())
{
this->DoSuccessBlocking();
}
else
{
if (!this->notifier_)
{
this->notifier_ = this->currentThread_.pool->NewWorkItem(this->currentThread_, AuSPtr<IWorkItemHandler>(AuSharedFromThis(), &this->tickAdapter));
}
this->notifier_->Dispatch();
}
}
template<class SuccessValue_t, class ErrorValue_t>
inline void Promise<SuccessValue_t, ErrorValue_t>::Fail()
{
SysAssert(!this->bHasFiredSuccess_);
this->bHasFiredError_ = true;
if (this->bInlineResponse_ &&
this->currentThread_ == GetCurrentWorkerPId())
{
this->DoFailureBlocking();
}
else
{
this->notifier_->Dispatch();
}
}
template<class SuccessValue_t, class ErrorValue_t>
inline bool Promise<SuccessValue_t, ErrorValue_t>::WaitBeforeWork(const AuSPtr<IWorkItem> &startingBarrier)
{
if (!this->startingBarrier_)
{
this->startingBarrier_ = GetCurrentWorkerPId().pool->NewFence();
}
if (!this->startingBarrier_)
{
return false;
}
this->startingBarrier_->WaitFor(startingBarrier);
return true;
}
template<class SuccessValue_t, class ErrorValue_t>
inline bool Promise<SuccessValue_t, ErrorValue_t>::BeginWork(WorkerPId_t worker)
{
this->currentThread_ = GetCurrentWorkerPId();
SysAssert(worker.pool);
if (worker == GetCurrentWorkerPId())
{
if (this->startingBarrier_)
{
this->startingBarrier_->Dispatch();
return true;
}
this->OnCBDelegate();
return true;
}
else
{
auto newItem = worker.pool->NewWorkItem(worker, AuSPtr<IWorkItemHandler>(AuSharedFromThis(), &this->startupAdapter));
if (!newItem)
{
return {};
}
if (this->startingBarrier_)
{
this->startingBarrier_->Then(newItem);
}
else
{
newItem->Dispatch();
}
return true;
}
}
template<class SuccessValue_t, class ErrorValue_t>
inline bool Promise<SuccessValue_t, ErrorValue_t>::BeginWorkEx(const AuSPtr<IPromiseDelegate> &promise, WorkerPId_t worker)
{
this->delegateCallback_ = promise;
return BeginWork(worker);
}
template<class SuccessValue_t, class ErrorValue_t>
inline bool Promise<SuccessValue_t, ErrorValue_t>::BeginWorkFunc(const FunctionalDelegate_f &promise, WorkerPId_t worker)
{
this->delegateFunctional_.Init(this, promise);
return BeginWorkEx(AuUnsafeRaiiToShared(&this->delegateFunctional_), worker);
}
template<class SuccessValue_t, class ErrorValue_t>
inline AuSPtr<Promise<SuccessValue_t, ErrorValue_t>> Promise<SuccessValue_t, ErrorValue_t>::OnSuccessFunctional(const FunctionalResponse_f &in)
{
this->responseFunctional_.Init(this, in);
return OnSuccessCallback(AuUnsafeRaiiToShared(&this->responseFunctional_));
}
template<class SuccessValue_t, class ErrorValue_t>
inline AuSPtr<Promise<SuccessValue_t, ErrorValue_t>> Promise<SuccessValue_t, ErrorValue_t>::OnSuccessCallback(const AuSPtr<IPromiseComplete> &in)
{
bool succeedFast {};
{
AU_LOCK_GUARD(this->lock_);
if (this->bHasSentSuccess_)
{
succeedFast = true;
}
else
{
this->responseCallback_ = in;
}
}
if (succeedFast)
{
in->OnSuccess(this->bHasSuccess_ ? &this->success : nullptr);
}
return AuSPtr<Promise>(AuSharedFromThis());
}
template<class SuccessValue_t, class ErrorValue_t>
inline AuSPtr<Promise<SuccessValue_t, ErrorValue_t>> Promise<SuccessValue_t, ErrorValue_t>::OnFailureFunctional(const FunctionalFail_f &in)
{
this->failureFunctional_.Init(this, in);
return OnFailureCallback(AuUnsafeRaiiToShared(&this->failureFunctional_));
}
template<class SuccessValue_t, class ErrorValue_t>
inline AuSPtr<Promise<SuccessValue_t, ErrorValue_t>> Promise<SuccessValue_t, ErrorValue_t>::OnFailureCallback(const AuSPtr<IPromiseFailure> &in)
{
bool failFast {};
{
AU_LOCK_GUARD(this->lock_);
if (this->bHasSentError_)
{
failFast = true;
}
else
{
this->failureCallback_ = in;
}
}
if (failFast)
{
in->OnFailure(this->bHasError_ ? &this->success : nullptr);
}
return AuSPtr<Promise>(AuSharedFromThis());
}
template<class SuccessValue_t, class ErrorValue_t>
inline AuSPtr<SuccessValue_t> Promise<SuccessValue_t, ErrorValue_t>::WriteIntoSuccess()
{
this->bHasSuccess_ = true;
return AuSPtr<SuccessValue_t>(AuSharedFromThis(), &this->success);
}
template<class SuccessValue_t, class ErrorValue_t>
inline AuSPtr<ErrorValue_t> Promise<SuccessValue_t, ErrorValue_t>::WriteIntoError()
{
this->bHasError_ = true;
return AuSPtr<SuccessValue_t>(AuSharedFromThis(), &this->error);
}
template<class SuccessValue_t, class ErrorValue_t>
inline void PromiseCallTmpl<SuccessValue_t, ErrorValue_t>::OnPromiseDelegate(const AuSPtr<IPromiseCompleter> &promise)
{
auto ptr = static_cast<IPromiseAccessor<SuccessValue_t, ErrorValue_t> *>(promise.get());
auto sptr = AuSPtr<IPromiseAccessor<SuccessValue_t, ErrorValue_t>>(promise, ptr);
SysAssert(this->onDelegate_);
this->onDelegate_(sptr);
}
template<class ErrorValue_t>
inline void PromiseFailTmpl<ErrorValue_t>::OnFailure(void *in)
{
SysAssert(this->onFailure_);
if (!in)
{
this->onFailure_({});
}
else
{
this->onFailure_(AuSPtr<ErrorValue_t>(this->parent_->GetData(), (ErrorValue_t *) in));
}
}
template<class SuccessValue_t>
inline void PromiseSuccessTmpl<SuccessValue_t>::OnSuccess(void *in)
{
SysAssert(this->onSuccess_);
if (!in)
{
this->onSuccess_({});
}
else
{
this->onSuccess_(AuSPtr<SuccessValue_t>(this->parent_->GetData(), (SuccessValue_t *)in));
}
}
}
struct AuNullS
{
AuUInt8 null;
};
using AuNullPromise = Aurora::Async::Promise<AuNullS, AuNullS>;
template <class Out_t, class Error_t = AuNullS>
using AuPromise = Aurora::Async::Promise<Out_t, Error_t>;