From 3af70e75c3e54d7d6af23d22af73dcf917c4f2f6 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 7 Dec 2018 10:13:29 -0800 Subject: [PATCH] ircd::ctx: Minor cleanup / struct packing / type. --- include/ircd/ctx/ctx.h | 2 +- ircd/ctx.cc | 56 +++++++++++++++++++++--------------------- ircd/ctx.h | 10 +++++--- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/include/ircd/ctx/ctx.h b/include/ircd/ctx/ctx.h index 747da6b41..0e6b62ea6 100644 --- a/include/ircd/ctx/ctx.h +++ b/include/ircd/ctx/ctx.h @@ -49,7 +49,7 @@ namespace ircd::ctx string_view name(const ctx &); // User's optional label for context const size_t &stack_max(const ctx &); // Returns stack size allocated for ctx const size_t &stack_at(const ctx &); // Stack at last sleep (also see this_ctx.h) - const int64_t ¬es(const ctx &); // Peeks at internal semaphore count + const int32_t ¬es(const ctx &); // Peeks at internal semaphore count const uint64_t &yields(const ctx &); // Context switching counter const ulong &cycles(const ctx &); // Accumulated tsc (not counting cur slice) bool interruptible(const ctx &) noexcept; // Context can throw at interruption point diff --git a/ircd/ctx.cc b/ircd/ctx.cc index 66eb3daa5..37483d15e 100644 --- a/ircd/ctx.cc +++ b/ircd/ctx.cc @@ -118,33 +118,6 @@ catch(const std::exception &e) return; } -/// Direct context switch to this context. -/// -/// This currently doesn't work yet because the suspension state of this -/// context has to be ready to be jumped to and that isn't implemented yet. -void -ircd::ctx::ctx::jump() -{ - assert(this->yc); - assert(current != this); // can't jump to self - - auto &yc(*this->yc); - auto &target(*yc.coro_.lock()); - - // Jump from the currently running context (source) to *this (target) - // with continuation of source after target - { - current->notes = 0; // Unconditionally cleared here - const continuation continuation; - target(); - } - - assert(current != this); - assert(current->notes == 1); // notes = 1; set by continuation dtor on wakeup - - interruption_point(); -} - /// Yield (suspend) this context until notified. /// /// This context must be currently running otherwise bad things. Returns false @@ -180,6 +153,33 @@ ircd::ctx::ctx::wait() return true; } +/// Direct context switch to this context. +/// +/// This currently doesn't work yet because the suspension state of this +/// context has to be ready to be jumped to and that isn't implemented yet. +void +ircd::ctx::ctx::jump() +{ + assert(this->yc); + assert(current != this); // can't jump to self + + auto &yc(*this->yc); + auto &target(*yc.coro_.lock()); + + // Jump from the currently running context (source) to *this (target) + // with continuation of source after target + { + current->notes = 0; // Unconditionally cleared here + const continuation continuation; + target(); + } + + assert(current != this); + assert(current->notes == 1); // notes = 1; set by continuation dtor on wakeup + + interruption_point(); +} + /// Notifies this context to resume (wake up from waiting). /// /// Returns true if this note was the first note received by this context @@ -465,7 +465,7 @@ ircd::ctx::yields(const ctx &ctx) } /// Returns the notification count for `ctx` -const int64_t & +const int32_t & ircd::ctx::notes(const ctx &ctx) { return ctx.notes; diff --git a/ircd/ctx.h b/ircd/ctx.h index 9787f322c..4d8d8c3ec 100644 --- a/ircd/ctx.h +++ b/ircd/ctx.h @@ -40,16 +40,18 @@ struct ircd::ctx::profile struct ircd::ctx::ctx :instance_list { + using flags_t = std::underlying_type::type; + static uint64_t id_ctr; // monotonic uint64_t id {++id_ctr}; // Unique runtime ID const char *name {nullptr}; // User given name (optional) - context::flags flags {(context::flags)0}; // User given flags + flags_t flags {0}; // User given flags + int32_t notes {0}; // norm: 0 = asleep; 1 = awake; inc by others; dec by self boost::asio::io_service::strand strand; // mutex/serializer boost::asio::steady_timer alarm; // acting semaphore (64B) boost::asio::yield_context *yc {nullptr}; // boost interface continuation *cont {nullptr}; // valid when asleep; invalid when awake - int64_t notes {0}; // norm: 0 = asleep; 1 = awake; inc by others; dec by self ircd::ctx::stack stack; // stack related structure ircd::ctx::profile profile; // prof related structure list::node node; // node for ctx::list @@ -62,10 +64,10 @@ struct ircd::ctx::ctx bool termination_point(std::nothrow_t); // Check for terminate void interruption_point(); // throws interrupted or terminated - bool wait(); // yield context to ios queue (returns on this resume) - void jump(); // jump to context directly (returns on your resume) void wake(); // jump to context by queueing with ios (use note()) bool note(); // properly request wake() + void jump(); // jump to context directly (returns on your resume) + bool wait(); // yield context to ios queue (returns on this resume) void operator()(boost::asio::yield_context, const std::function) noexcept;