From 4db52804cba220261ce1bc7c5c83667fd1f1d2c0 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 19 Apr 2019 20:50:22 -0700 Subject: [PATCH] ircd::ctx: Use iteration closure rather than exposing ctx::list. --- include/ircd/ctx/ctx.h | 3 ++- ircd/ctx.cc | 17 ++++++++------ modules/console.cc | 50 ++++++++++++++++++++++++++---------------- 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/include/ircd/ctx/ctx.h b/include/ircd/ctx/ctx.h index 11a150200..7a7101f8a 100644 --- a/include/ircd/ctx/ctx.h +++ b/include/ircd/ctx/ctx.h @@ -44,7 +44,6 @@ namespace ircd::ctx IRCD_OVERLOAD(threadsafe) extern log::log log; - extern const std::list &ctxs; // List of all ctx instances const uint64_t &id(const ctx &); // Unique ID for context string_view name(const ctx &); // User's optional label for context @@ -68,6 +67,8 @@ namespace ircd::ctx void notify(ctx &, threadsafe_t); // Notify context with threadsafety. bool notify(ctx &); // Queue a context switch to arg void yield(ctx &); // Direct context switch to arg + + bool for_each(const std::function &); } #include "prof.h" diff --git a/ircd/ctx.cc b/ircd/ctx.cc index 199184318..eb1e427f0 100644 --- a/ircd/ctx.cc +++ b/ircd/ctx.cc @@ -40,13 +40,6 @@ ircd::util::instance_list::list allocator }; -/// Public interface linkage for the list of all ctx instances -decltype(ircd::ctx::ctxs) -ircd::ctx::ctxs -{ - reinterpret_cast(ctx::ctx::list) -}; - decltype(ircd::ctx::log) ircd::ctx::log { @@ -350,6 +343,16 @@ const // ctx/ctx.h // +bool +ircd::ctx::for_each(const std::function &closure) +{ + for(auto &ctx : ctx::list) + if(!closure(*ctx)) + return false; + + return true; +} + /// Yield to context `ctx`. /// /// diff --git a/modules/console.cc b/modules/console.cc index 2e1b8c6af..68d77855f 100644 --- a/modules/console.cc +++ b/modules/console.cc @@ -1575,13 +1575,17 @@ console_cmd__ctx__interrupt(opt &out, const string_view &line) "id", "[id]..." }}; - for(size_t i(0); i < param.count(); ++i) - for(auto *const &ctx : ctx::ctxs) - if(id(*ctx) == param.at(i)) + bool cont{true}; + for(size_t i(0); i < param.count() && cont; ++i) + cont = ctx::for_each([&](auto &ctx) + { + if(id(ctx) == param.at(i)) { - interrupt(*ctx); - break; + interrupt(ctx); + return false; } + else return true; + }); return true; } @@ -1615,16 +1619,20 @@ console_cmd__ctx__prof(opt &out, const string_view &line) return true; } - for(size_t i(0); i < param.count(); ++i) - for(auto *const &ctx : ctx::ctxs) - if(id(*ctx) == param.at(i)) + bool cont{true}; + for(size_t i(0); i < param.count() && cont; ++i) + cont = ctx::for_each([&](auto &ctx) + { + if(id(ctx) == param.at(i)) { - out << "Profile for ctx:" << id(*ctx) << " '" << name(*ctx) << "':\n" + out << "Profile for ctx:" << id(ctx) << " '" << name(ctx) << "':\n" << std::endl; - display(ctx::prof::get(*ctx)); - break; + display(ctx::prof::get(ctx)); + return false; } + else return true; + }); return true; } @@ -1637,13 +1645,17 @@ console_cmd__ctx__term(opt &out, const string_view &line) "id", "[id]..." }}; - for(size_t i(0); i < param.count(); ++i) - for(auto *const &ctx : ctx::ctxs) - if(id(*ctx) == param.at(i)) + bool cont {true}; + for(size_t i(0); i < param.count() && cont; ++i) + cont = ctx::for_each([&](auto &ctx) + { + if(id(ctx) == param.at(i)) { - ctx::terminate(*ctx); - break; + ctx::terminate(ctx); + return false; } + else return true; + }); return true; } @@ -1682,9 +1694,8 @@ console_cmd__ctx__list(opt &out, const string_view &line) << ":NAME" << std::endl; - for(const auto *const &ctxp : ctx::ctxs) + ctx::for_each([&out](auto &ctx) { - const auto &ctx{*ctxp}; out << std::setw(5) << std::right << id(ctx); out << " " << (started(ctx)? 'A' : '-') @@ -1736,7 +1747,8 @@ console_cmd__ctx__list(opt &out, const string_view &line) << name(ctx); out << std::endl; - } + return true; + }); return true; }