ResourceCache
This commit is contained in:
298
Src/TOSAsync.hpp
298
Src/TOSAsync.hpp
@@ -1,20 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "boost/asio/awaitable.hpp"
|
||||
#include "boost/asio/co_spawn.hpp"
|
||||
#include "boost/asio/deadline_timer.hpp"
|
||||
#include "boost/asio/detached.hpp"
|
||||
#include "boost/asio/io_context.hpp"
|
||||
#include "boost/asio/use_awaitable.hpp"
|
||||
#include "TOSLib.hpp"
|
||||
#include <functional>
|
||||
#include "boost/system/detail/error_code.hpp"
|
||||
#include "boost/system/system_error.hpp"
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/experimental/awaitable_operators.hpp>
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
#include <list>
|
||||
|
||||
|
||||
|
||||
namespace TOS {
|
||||
|
||||
using namespace boost::asio::experimental::awaitable_operators;
|
||||
template<typename T = void>
|
||||
using coro = boost::asio::awaitable<T>;
|
||||
namespace asio = boost::asio;
|
||||
|
||||
class AsyncSemaphore
|
||||
{
|
||||
@@ -52,26 +56,183 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class IAsyncDestructible : public std::enable_shared_from_this<IAsyncDestructible> {
|
||||
protected:
|
||||
boost::asio::any_io_executor IOC;
|
||||
boost::asio::deadline_timer DestructLine;
|
||||
|
||||
virtual coro<> asyncDestructor() { DestructLine.cancel(); co_return; }
|
||||
/*
|
||||
Многие могут уведомлять одного
|
||||
Ждёт события. После доставки уведомления ждёт повторно
|
||||
*/
|
||||
class MultipleToOne_AsyncSymaphore {
|
||||
asio::deadline_timer Timer;
|
||||
|
||||
public:
|
||||
IAsyncDestructible(boost::asio::any_io_executor ioc)
|
||||
: IOC(ioc), DestructLine(ioc, boost::posix_time::ptime(boost::posix_time::pos_infin))
|
||||
MultipleToOne_AsyncSymaphore(asio::io_context &ioc)
|
||||
: Timer(ioc, boost::posix_time::ptime(boost::posix_time::pos_infin))
|
||||
{}
|
||||
|
||||
void notify() {
|
||||
Timer.cancel();
|
||||
}
|
||||
|
||||
void wait() {
|
||||
try { Timer.wait(); } catch(...) {}
|
||||
Timer.expires_at(boost::posix_time::ptime(boost::posix_time::pos_infin));
|
||||
}
|
||||
|
||||
coro<> async_wait() {
|
||||
try { co_await Timer.async_wait(); } catch(...) {}
|
||||
}
|
||||
};
|
||||
|
||||
class WaitableCoro {
|
||||
asio::io_context &IOC;
|
||||
std::shared_ptr<MultipleToOne_AsyncSymaphore> Symaphore;
|
||||
std::exception_ptr LastException;
|
||||
|
||||
public:
|
||||
WaitableCoro(asio::io_context &ioc)
|
||||
: IOC(ioc)
|
||||
{}
|
||||
|
||||
void co_spawn(coro<> token) {
|
||||
Symaphore = std::make_shared<MultipleToOne_AsyncSymaphore>(IOC);
|
||||
asio::co_spawn(IOC, [token = std::move(token), symaphore = Symaphore]() -> coro<> {
|
||||
try { co_await std::move(const_cast<coro<>&>(token)); } catch(...) {}
|
||||
symaphore->notify();
|
||||
}, asio::detached);
|
||||
}
|
||||
|
||||
void wait() {
|
||||
Symaphore->wait();
|
||||
}
|
||||
|
||||
coro<> async_wait() {
|
||||
return Symaphore->async_wait();
|
||||
}
|
||||
};
|
||||
|
||||
class AsyncUseControl {
|
||||
public:
|
||||
class Lock {
|
||||
AsyncUseControl *AUC;
|
||||
|
||||
public:
|
||||
Lock(AsyncUseControl *auc)
|
||||
: AUC(auc)
|
||||
{}
|
||||
|
||||
Lock()
|
||||
: AUC(nullptr)
|
||||
{}
|
||||
|
||||
~Lock() {
|
||||
if(AUC)
|
||||
unlock();
|
||||
}
|
||||
|
||||
Lock(const Lock&) = delete;
|
||||
Lock(Lock&& obj)
|
||||
: AUC(obj.AUC)
|
||||
{
|
||||
obj.AUC = nullptr;
|
||||
}
|
||||
|
||||
Lock& operator=(const Lock&) = delete;
|
||||
Lock& operator=(Lock&& obj) {
|
||||
if(&obj == this)
|
||||
return *this;
|
||||
|
||||
if(AUC)
|
||||
unlock();
|
||||
|
||||
AUC = obj.AUC;
|
||||
obj.AUC = nullptr;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void unlock() {
|
||||
assert(AUC);
|
||||
|
||||
if(--AUC->Uses == 0 && AUC->OnNoUse) {
|
||||
AUC->OnNoUse();
|
||||
}
|
||||
|
||||
AUC = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
std::move_only_function<void()> OnNoUse;
|
||||
std::atomic_int Uses = 0;
|
||||
|
||||
public:
|
||||
template<BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) Token = asio::default_completion_token_t<asio::io_context>>
|
||||
auto wait(Token&& token = asio::default_completion_token_t<asio::io_context>()) {
|
||||
auto initiation = [this](auto&& token) {
|
||||
int value;
|
||||
do {
|
||||
value = Uses.exchange(-1);
|
||||
} while(value == -1);
|
||||
|
||||
OnNoUse = std::move(token);
|
||||
|
||||
if(value == 0)
|
||||
OnNoUse();
|
||||
|
||||
Uses.exchange(value);
|
||||
};
|
||||
|
||||
return asio::async_initiate<Token, void()>(initiation, token);
|
||||
}
|
||||
|
||||
Lock use() {
|
||||
int value;
|
||||
do {
|
||||
value = Uses.exchange(-1);
|
||||
} while(value == -1);
|
||||
|
||||
if(OnNoUse)
|
||||
throw boost::system::system_error(asio::error::operation_aborted, "OnNoUse");
|
||||
|
||||
Uses.exchange(++value);
|
||||
return Lock(this);
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
Используется, чтобы вместо уничтожения объекта в умной ссылке, вызвать корутину с co_await asyncDestructor()
|
||||
*/
|
||||
class IAsyncDestructible : public std::enable_shared_from_this<IAsyncDestructible> {
|
||||
protected:
|
||||
asio::io_context &IOC;
|
||||
AsyncUseControl AUC;
|
||||
|
||||
virtual coro<> asyncDestructor() { co_await AUC.wait(); }
|
||||
|
||||
public:
|
||||
IAsyncDestructible(asio::io_context &ioc)
|
||||
: IOC(ioc)
|
||||
{}
|
||||
|
||||
virtual ~IAsyncDestructible() {}
|
||||
|
||||
coro<std::variant<std::monostate, std::monostate>> cancelable(coro<> &&c) { return std::move(c) || DestructLine.async_wait(boost::asio::use_awaitable); }
|
||||
protected:
|
||||
template<typename T, typename = typename std::is_same<IAsyncDestructible, T>>
|
||||
static std::shared_ptr<T> createShared(asio::io_context &ioc, T *ptr)
|
||||
{
|
||||
return std::shared_ptr<T>(ptr, [&ioc = ioc](T *ptr) {
|
||||
boost::asio::co_spawn(ioc, [&ioc = ioc](IAsyncDestructible *ptr) -> coro<> {
|
||||
try { co_await ptr->asyncDestructor(); } catch(...) { }
|
||||
delete ptr;
|
||||
co_return;
|
||||
} (ptr), boost::asio::detached);
|
||||
});
|
||||
}
|
||||
|
||||
template<typename T, typename = typename std::is_same<IAsyncDestructible, T>>
|
||||
static std::shared_ptr<T> createShared(boost::asio::any_io_executor ioc, T *ptr)
|
||||
static coro<std::shared_ptr<T>> createShared(T *ptr)
|
||||
{
|
||||
return std::shared_ptr<T>(ptr, [ioc = std::move(ioc)](IAsyncDestructible *ptr) {
|
||||
co_return std::shared_ptr<T>(ptr, [ioc = asio::get_associated_executor(co_await asio::this_coro::executor)](T *ptr) {
|
||||
boost::asio::co_spawn(ioc, [](IAsyncDestructible *ptr) -> coro<> {
|
||||
try { co_await ptr->asyncDestructor(); } catch(...) { }
|
||||
delete ptr;
|
||||
@@ -80,16 +241,113 @@ public:
|
||||
});
|
||||
}
|
||||
|
||||
template<typename T, typename ...Args, typename = typename std::is_same<IAsyncDestructible, T>>
|
||||
static std::shared_ptr<T> makeShared(boost::asio::any_io_executor ioc, Args&& ... args)
|
||||
template<typename T, typename = typename std::is_same<IAsyncDestructible, T>>
|
||||
static std::unique_ptr<T, std::function<void(T*)>> createUnique(asio::io_context &ioc, T *ptr)
|
||||
{
|
||||
std::shared_ptr<T>(new T(ioc, std::forward<Args>(args)..., [ioc = std::move(ioc)](IAsyncDestructible *ptr) {
|
||||
return std::unique_ptr<T, std::function<void(T*)>>(ptr, [&ioc = ioc](T *ptr) {
|
||||
boost::asio::co_spawn(ioc, [](IAsyncDestructible *ptr) -> coro<> {
|
||||
try { co_await ptr->asyncDestructor(); } catch(...) { }
|
||||
delete ptr;
|
||||
co_return;
|
||||
} (ptr), boost::asio::detached);
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
template<typename T, typename = typename std::is_same<IAsyncDestructible, T>>
|
||||
static coro<std::unique_ptr<T, std::function<void(T*)>>> createUnique(T *ptr)
|
||||
{
|
||||
co_return std::unique_ptr<T, std::function<void(T*)>>(ptr, [ioc = asio::get_associated_executor(co_await asio::this_coro::executor)](T *ptr) {
|
||||
boost::asio::co_spawn(ioc, [](IAsyncDestructible *ptr) -> coro<> {
|
||||
try { co_await ptr->asyncDestructor(); } catch(...) { }
|
||||
delete ptr;
|
||||
co_return;
|
||||
} (ptr), boost::asio::detached);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class AsyncMutexObject {
|
||||
public:
|
||||
class Lock {
|
||||
public:
|
||||
Lock(AsyncMutexObject* obj)
|
||||
: Obj(obj)
|
||||
{}
|
||||
|
||||
Lock(const Lock& other) = delete;
|
||||
Lock(Lock&& other)
|
||||
: Obj(other.Obj)
|
||||
{
|
||||
other.Obj = nullptr;
|
||||
}
|
||||
|
||||
~Lock() {
|
||||
if(Obj)
|
||||
unlock();
|
||||
}
|
||||
|
||||
Lock& operator=(const Lock& other) = delete;
|
||||
Lock& operator=(Lock& other) {
|
||||
if(&other == this)
|
||||
return *this;
|
||||
|
||||
if(Obj)
|
||||
unlock();
|
||||
|
||||
Obj = other.Obj;
|
||||
other.Obj = nullptr;
|
||||
}
|
||||
|
||||
T& get() const { assert(Obj); return Obj->value; }
|
||||
T* operator->() const { assert(Obj); return &Obj->value; }
|
||||
T& operator*() const { assert(Obj); return Obj->value; }
|
||||
|
||||
void unlock() {
|
||||
assert(Obj);
|
||||
|
||||
typename SpinlockObject<Context>::Lock ctx = Obj->Ctx.lock();
|
||||
if(ctx->Chain.empty()) {
|
||||
ctx->InExecution = false;
|
||||
} else {
|
||||
auto token = std::move(ctx->Chain.front());
|
||||
ctx->Chain.pop_front();
|
||||
ctx.unlock();
|
||||
token(Lock(Obj));
|
||||
}
|
||||
|
||||
Obj = nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
AsyncMutexObject *Obj;
|
||||
};
|
||||
|
||||
private:
|
||||
struct Context {
|
||||
std::list<std::move_only_function<void(Lock)>> Chain;
|
||||
bool InExecution = false;
|
||||
};
|
||||
|
||||
SpinlockObject<Context> Ctx;
|
||||
T value;
|
||||
|
||||
public:
|
||||
template<BOOST_ASIO_COMPLETION_TOKEN_FOR(void(Lock)) Token = asio::default_completion_token_t<asio::io_context::executor_type>>
|
||||
auto lock(Token&& token = Token()) {
|
||||
auto initiation = [this](auto&& token) mutable {
|
||||
typename SpinlockObject<Context>::Lock ctx = Ctx.lock();
|
||||
|
||||
if(ctx->InExecution) {
|
||||
ctx->Chain.emplace_back(std::move(token));
|
||||
} else {
|
||||
ctx->InExecution = true;
|
||||
ctx.unlock();
|
||||
token(Lock(this));
|
||||
}
|
||||
};
|
||||
|
||||
return boost::asio::async_initiate<Token, void(Lock)>(std::move(initiation), token);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user