mirror of
https://github.com/matrix-construct/construct
synced 2024-10-31 19:08:59 +01:00
ircd::run: Add a convenience barrier template tool.
This commit is contained in:
parent
c815de3621
commit
c3b709dbd7
2 changed files with 40 additions and 0 deletions
|
@ -23,6 +23,7 @@ namespace ircd::ctx
|
||||||
namespace ircd::run
|
namespace ircd::run
|
||||||
{
|
{
|
||||||
enum class level :int;
|
enum class level :int;
|
||||||
|
template<class> struct barrier;
|
||||||
struct changed;
|
struct changed;
|
||||||
|
|
||||||
string_view reflect(const enum level &);
|
string_view reflect(const enum level &);
|
||||||
|
@ -124,4 +125,26 @@ struct ircd::run::changed
|
||||||
/// Default construction for no-op
|
/// Default construction for no-op
|
||||||
changed() noexcept;
|
changed() noexcept;
|
||||||
~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)...
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
17
ircd/run.cc
17
ircd/run.cc
|
@ -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
|
/// 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
|
/// 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
|
/// instead invoke their function on the main stack in their own io_context
|
||||||
|
|
Loading…
Reference in a new issue