From dcfae310abba972e1481d1257a311f41d6b3a7a7 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 16 Mar 2023 19:31:56 -0700 Subject: [PATCH] ircd::m::vm::notify: Add promise/future based notify interface. --- include/ircd/m/vm/notify.h | 15 +++++++++++++++ matrix/vm_notify.cc | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/include/ircd/m/vm/notify.h b/include/ircd/m/vm/notify.h index e5d168f49..4b7bd099b 100644 --- a/include/ircd/m/vm/notify.h +++ b/include/ircd/m/vm/notify.h @@ -13,6 +13,8 @@ namespace ircd::m::vm::notify { + struct future; + using value_type = std::pair *>; using alloc_type = allocator::node; using map_type = std::multimap *, std::less<>, alloc_type::allocator>; @@ -24,6 +26,19 @@ namespace ircd::m::vm::notify bool wait(const event::id &, const milliseconds); } +class ircd::m::vm::notify::future +:public ctx::future<> +{ + node_type node; + ctx::promise<> promise; + unique_iterator it; + + public: + future(const event::id &); + future(future &&) = delete; + future(const future &) = delete; +}; + /// Yields ctx until event was successfully evaluated. Returns false if /// timeout occurred. inline bool diff --git a/matrix/vm_notify.cc b/matrix/vm_notify.cc index dc258ff05..843cd6553 100644 --- a/matrix/vm_notify.cc +++ b/matrix/vm_notify.cc @@ -112,6 +112,44 @@ ircd::m::vm::notify::wait(const vector_view &event_id, return exists; } +// +// future::future +// + +ircd::m::vm::notify::future::future(const event::id &event_id) +:ctx::future<>{ctx::already} +{ + if(m::exists(event_id)) + return; + + const auto &s + { + map.get_allocator().s + }; + + assert(s); + assert(!s->next); + const scope_restore next + { + s->next, reinterpret_cast(&node) + }; + + it = + { + map, map.emplace(event_id, &promise) + }; + + assert(it->second); + static_cast &>(*this) = ctx::future<> + { + *it->second + }; +} + +// +// internal +// + void ircd::m::vm::notify::hook_handle(const m::event &event, vm::eval &)