0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-16 06:51:08 +01:00

ircd::ctx: Use iteration closure rather than exposing ctx::list.

This commit is contained in:
Jason Volk 2019-04-19 20:50:22 -07:00
parent bf3a23b8d2
commit 4db52804cb
3 changed files with 43 additions and 27 deletions

View file

@ -44,7 +44,6 @@ namespace ircd::ctx
IRCD_OVERLOAD(threadsafe)
extern log::log log;
extern const std::list<ctx *> &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<bool (ctx &)> &);
}
#include "prof.h"

View file

@ -40,13 +40,6 @@ ircd::util::instance_list<ircd::ctx::ctx>::list
allocator
};
/// Public interface linkage for the list of all ctx instances
decltype(ircd::ctx::ctxs)
ircd::ctx::ctxs
{
reinterpret_cast<const decltype(ircd::ctx::ctxs) &>(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<bool (ctx &)> &closure)
{
for(auto &ctx : ctx::list)
if(!closure(*ctx))
return false;
return true;
}
/// Yield to context `ctx`.
///
///

View file

@ -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<uint64_t>(i))
bool cont{true};
for(size_t i(0); i < param.count() && cont; ++i)
cont = ctx::for_each([&](auto &ctx)
{
interrupt(*ctx);
break;
if(id(ctx) == param.at<uint64_t>(i))
{
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<uint64_t>(i))
bool cont{true};
for(size_t i(0); i < param.count() && cont; ++i)
cont = ctx::for_each([&](auto &ctx)
{
out << "Profile for ctx:" << id(*ctx) << " '" << name(*ctx) << "':\n"
if(id(ctx) == param.at<uint64_t>(i))
{
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<uint64_t>(i))
bool cont {true};
for(size_t i(0); i < param.count() && cont; ++i)
cont = ctx::for_each([&](auto &ctx)
{
ctx::terminate(*ctx);
break;
if(id(ctx) == param.at<uint64_t>(i))
{
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;
}