0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-01 01:28:54 +02:00

ircd::run: Add a convenience barrier template tool.

This commit is contained in:
Jason Volk 2020-03-16 12:57:59 -07:00
parent c815de3621
commit c3b709dbd7
2 changed files with 40 additions and 0 deletions

View file

@ -23,6 +23,7 @@ namespace ircd::ctx
namespace ircd::run
{
enum class level :int;
template<class> struct barrier;
struct changed;
string_view reflect(const enum level &);
@ -124,4 +125,26 @@ struct ircd::run::changed
/// Default construction for no-op
changed() noexcept;
~changed() noexcept;
// convenience to wait on the dock for the levels
static void wait(const std::initializer_list<enum level> &);
};
/// Tool to yield a context until the run::level is RUN or QUIT. Once either is
/// satisfied (or if already satisfied) the run::level is checked to be RUN
/// otherwise an exception is thrown.
///
template<class exception>
struct ircd::run::barrier
{
template<class... args>
barrier(args&&... a)
{
changed::wait({level::RUN, level::QUIT});
if(unlikely(level != level::RUN))
throw exception
{
std::forward<args>(a)...
};
}
};

View file

@ -64,6 +64,23 @@ noexcept
{
}
void
ircd::run::changed::wait(const std::initializer_list<enum level> &levels)
{
changed::dock.wait([&levels]
{
return std::any_of(begin(levels), end(levels), []
(const auto &level)
{
return run::level == level;
});
});
}
//
// ircd::run
//
/// The notification will be posted to the io_context. This is important to
/// prevent the callback from continuing execution on some ircd::ctx stack and
/// instead invoke their function on the main stack in their own io_context