From 124f7ea12a666a3a6d554cb6eddcbbae6769762f Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 15 Oct 2017 21:12:58 -0700 Subject: [PATCH] ircd::ctx: Add utils for getting this_ctxt name and id; truncate default name literal. --- include/ircd/ctx.h | 3 +++ ircd/ctx.cc | 20 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/ircd/ctx.h b/include/ircd/ctx.h index 0d820ee7e..14430644b 100644 --- a/include/ircd/ctx.h +++ b/include/ircd/ctx.h @@ -82,6 +82,9 @@ namespace ircd::ctx { inline namespace this_ctx ctx &cur(); // Assumptional reference to *current + const uint64_t &id(); // Unique ID for cur ctx + string_view name(); // Optional label for cur ctx + void wait(); // Returns when context is woken up. void yield(); // Allow other contexts to run before returning. diff --git a/ircd/ctx.cc b/ircd/ctx.cc index caff84dca..e655179e9 100644 --- a/ircd/ctx.cc +++ b/ircd/ctx.cc @@ -57,7 +57,7 @@ struct ircd::ctx::ctx void operator()(boost::asio::yield_context, const std::function) noexcept; - ctx(const char *const &name = "", + ctx(const char *const &name = "", const size_t &stack_max = DEFAULT_STACK_SIZE, const context::flags &flags = (context::flags)0, boost::asio::io_service *const &ios = ircd::ios); @@ -327,6 +327,22 @@ ircd::ctx::this_ctx::interruption_requested() return interruption(cur()); } +/// Returns unique ID of currently running context +const uint64_t & +ircd::ctx::this_ctx::id() +{ + static const uint64_t zero{0}; + return current? id(cur()) : zero; +} + +/// Returns optional developer-given name for currently running context +ircd::string_view +ircd::ctx::this_ctx::name() +{ + static const string_view nada{"*"}; + return current? name(cur()) : nada; +} + /// Yield to context `ctx`. /// /// @@ -569,7 +585,7 @@ ircd::ctx::context::context(function func, const flags &flags) :context { - "", DEFAULT_STACK_SIZE, flags, std::move(func) + "", DEFAULT_STACK_SIZE, flags, std::move(func) } { }