diff --git a/include/ircd/ctx/critical_assertion.h b/include/ircd/ctx/critical_assertion.h new file mode 100644 index 000000000..fc7774c10 --- /dev/null +++ b/include/ircd/ctx/critical_assertion.h @@ -0,0 +1,37 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +#pragma once +#define HAVE_IRCD_CTX_CRITICAL_ASSERTION_H + +namespace ircd::ctx::this_ctx +{ + struct critical_assertion; // Assert no yielding for a section +} + +/// An instance of critical_assertion detects an attempt to context switch. +/// +/// For when the developer specifically does not want any yielding in a +/// section or anywhere up the stack from it. This device does not prevent +/// a switch and may carry no meaning outside of debug-mode compilation. It is +/// good practice to use this device even when it appears obvious the +/// section's callgraph has no chance of yielding: code changes, and everything +/// up the graph can change without taking notice of your section. +/// +class ircd::ctx::this_ctx::critical_assertion +{ + #ifndef NDEBUG + bool theirs; + + public: + critical_assertion(); + ~critical_assertion() noexcept; + #endif +}; diff --git a/include/ircd/ctx/critical_indicator.h b/include/ircd/ctx/critical_indicator.h new file mode 100644 index 000000000..d41ca968e --- /dev/null +++ b/include/ircd/ctx/critical_indicator.h @@ -0,0 +1,38 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +#pragma once +#define HAVE_IRCD_CTX_CRITICAL_INDICATOR_H + +namespace ircd::ctx::this_ctx +{ + struct critical_indicator; // Indicates if yielding happened for a section +} + +/// An instance of critical_indicator reports if context switching happened. +/// +/// A critical_indicator remains true after construction until a context switch +/// has occurred. It then becomes false. This is not an assertion and is +/// available in optimized builds for real use. For example, a context may +/// want to recompute some value after a context switch and opportunistically +/// skip this effort when this indicator shows no switch occurred. +/// +class ircd::ctx::this_ctx::critical_indicator +{ + uint64_t state; + + public: + uint64_t count() const { return yields(cur()) - state; } + operator bool() const { return yields(cur()) == state; } + + critical_indicator() + :state{yields(cur())} + {} +}; diff --git a/include/ircd/ctx/ctx.h b/include/ircd/ctx/ctx.h index cd33f778e..057e8653d 100644 --- a/include/ircd/ctx/ctx.h +++ b/include/ircd/ctx/ctx.h @@ -55,9 +55,9 @@ namespace ircd::ctx const int64_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 &); // Context can throw at interruption point - bool interruption(const ctx &); // Context was marked for interruption - bool termination(const ctx &); // Context was marked for termination + bool interruptible(const ctx &) noexcept; // Context can throw at interruption point + bool interruption(const ctx &) noexcept; // Context was marked for interruption + bool termination(const ctx &) noexcept; // Context was marked for termination bool finished(const ctx &); // Context function returned (or exception). bool started(const ctx &); // Context was ever entered. bool running(const ctx &); // Context is the currently running ctx. @@ -73,6 +73,11 @@ namespace ircd::ctx } #include "this_ctx.h" +#include "stack_usage_assertion.h" +#include "critical_assertion.h" +#include "critical_indicator.h" +#include "exception_handler.h" +#include "uninterruptible.h" #include "prof.h" #include "list.h" #include "dock.h" diff --git a/include/ircd/ctx/exception_handler.h b/include/ircd/ctx/exception_handler.h new file mode 100644 index 000000000..75245390b --- /dev/null +++ b/include/ircd/ctx/exception_handler.h @@ -0,0 +1,38 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +#pragma once +#define HAVE_IRCD_CTX_EXCEPTION_HANDLER_H + +namespace ircd::ctx::this_ctx +{ + struct exception_handler; +} + +/// An instance of exception_handler must be present to allow a context +/// switch inside a catch block. This is due to ABI limitations that stack +/// exceptions with thread-local assumptions and don't expect catch blocks +/// on the same thread to interleave when we switch the stack. +/// +/// We first increment the refcount for the caught exception so it remains +/// intuitively accessible for the rest of the catch block. Then the presence +/// of this object makes the ABI believe the catch block has ended. +/// +/// The exception cannot then be rethrown. DO NOT RETHROW THE EXCEPTION. +/// +struct ircd::ctx::this_ctx::exception_handler +:std::exception_ptr +{ + exception_handler() noexcept; + exception_handler(exception_handler &&) = delete; + exception_handler(const exception_handler &) = delete; + exception_handler &operator=(exception_handler &&) = delete; + exception_handler &operator=(const exception_handler &) = delete; +}; diff --git a/include/ircd/ctx/prof.h b/include/ircd/ctx/prof.h index 875677bda..5383f8edd 100644 --- a/include/ircd/ctx/prof.h +++ b/include/ircd/ctx/prof.h @@ -39,6 +39,9 @@ namespace ircd::ctx::prof void mark(const event &); } +/// Profiling events for marking. These are currently used internally at the +/// appropriate point to mark(): the user of ircd::ctx has no reason to mark() +/// these events; this interface is not quite developed for general use yet. enum class ircd::ctx::prof::event { SPAWN, // Context spawn requested diff --git a/include/ircd/ctx/stack_usage_assertion.h b/include/ircd/ctx/stack_usage_assertion.h new file mode 100644 index 000000000..d4692ac30 --- /dev/null +++ b/include/ircd/ctx/stack_usage_assertion.h @@ -0,0 +1,35 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +#pragma once +#define HAVE_IRCD_CTX_STACK_USAGE_ASSERTION_H + +namespace ircd::ctx::this_ctx +{ + struct stack_usage_assertion; + + size_t stack_at_here() __attribute__((noinline)); +} + +/// An instance of stack_usage_assertion is placed on a ctx stack where one +/// wants to test the stack usage at both construction and destruction points +/// to ensure it is less than the value set in ctx::prof::settings which is +/// generally some engineering safety factor of 2-3 etc. This should not be +/// entirely relied upon except during debug builds, however we may try to +/// provide an optimized build mode enabling these to account for any possible +/// differences in the stack between the environments. +/// +struct ircd::ctx::this_ctx::stack_usage_assertion +{ + #ifndef NDEBUG + stack_usage_assertion(); + ~stack_usage_assertion() noexcept; + #endif +}; diff --git a/include/ircd/ctx/this_ctx.h b/include/ircd/ctx/this_ctx.h index eb65fb148..a532ab07e 100644 --- a/include/ircd/ctx/this_ctx.h +++ b/include/ircd/ctx/this_ctx.h @@ -15,28 +15,18 @@ namespace ircd::ctx { /// Interface to the currently running context inline namespace this_ctx { - struct critical_indicator; // Indicates if yielding happened for a section - struct critical_assertion; // Assert no yielding for a section - struct exception_handler; // Must be present to yield in a handler - struct uninterruptible; // Scope convenience for interruptible() - struct ctx &cur(); ///< Assumptional reference to *current - const uint64_t &id(); // Unique ID for cur ctx string_view name(); // Optional label for cur ctx + ulong cycles_here(); // misc profiling related + + bool interruption_requested(); // interruption(cur()) + void interruption_point(); // throws if interruption_requested() + void wait(); // Returns when context is woken up. void yield(); // Allow other contexts to run before returning. - void interruption_point(); // throws if interruption_requested() - bool interruption_requested(); // interruption(cur()) - void interruptible(const bool &); // interruptible(cur(), bool) +INTERRUPTION POINT - void interruptible(const bool &, std::nothrow_t) noexcept; - - struct stack_usage_assertion; // Assert safety factor (see ctx/prof.h) - size_t stack_at_here() __attribute__((noinline)); - ulong cycles_here(); - // Return remaining time if notified; or <= 0 if not, and timeout thrown on throw overloads microseconds wait(const microseconds &, const std::nothrow_t &); template nothrow_overload wait(const duration &); @@ -59,116 +49,6 @@ namespace ircd::ctx extern __thread ctx *current; } -/// An instance of stack_usage_assertion is placed on a ctx stack where one -/// wants to test the stack usage at both construction and destruction points -/// to ensure it is less than the value set in ctx::prof::settings which is -/// generally some engineering safety factor of 2-3 etc. This should not be -/// entirely relied upon except during debug builds, however we may try to -/// provide an optimized build mode enabling these to account for any possible -/// differences in the stack between the environments. -/// -struct ircd::ctx::this_ctx::stack_usage_assertion -{ - #ifndef NDEBUG - stack_usage_assertion(); - ~stack_usage_assertion() noexcept; - #endif -}; - -/// An instance of critical_assertion detects an attempt to context switch. -/// -/// For when the developer specifically does not want any yielding in a -/// section or anywhere up the stack from it. This device does not prevent -/// a switch and may carry no meaning outside of debug-mode compilation. It is -/// good practice to use this device even when it appears obvious the -/// section's callgraph has no chance of yielding: code changes, and everything -/// up the graph can change without taking notice of your section. -/// -class ircd::ctx::this_ctx::critical_assertion -{ - #ifndef NDEBUG - bool theirs; - - public: - critical_assertion(); - ~critical_assertion() noexcept; - #endif -}; - -/// An instance of critical_indicator reports if context switching happened. -/// -/// A critical_indicator remains true after construction until a context switch -/// has occurred. It then becomes false. This is not an assertion and is -/// available in optimized builds for real use. For example, a context may -/// want to recompute some value after a context switch and opportunistically -/// skip this effort when this indicator shows no switch occurred. -/// -class ircd::ctx::this_ctx::critical_indicator -{ - uint64_t state; - - public: - uint64_t count() const { return yields(cur()) - state; } - operator bool() const { return yields(cur()) == state; } - - critical_indicator() - :state{yields(cur())} - {} -}; - -/// An instance of exception_handler must be present to allow a context -/// switch inside a catch block. This is due to ABI limitations that stack -/// exceptions with thread-local assumptions and don't expect catch blocks -/// on the same thread to interleave when we switch the stack. -/// -/// We first increment the refcount for the caught exception so it remains -/// intuitively accessible for the rest of the catch block. Then the presence -/// of this object makes the ABI believe the catch block has ended. -/// -/// The exception cannot then be rethrown. DO NOT RETHROW THE EXCEPTION. -/// -struct ircd::ctx::this_ctx::exception_handler -:std::exception_ptr -{ - exception_handler() noexcept; - exception_handler(exception_handler &&) = delete; - exception_handler(const exception_handler &) = delete; - exception_handler &operator=(exception_handler &&) = delete; - exception_handler &operator=(const exception_handler &) = delete; -}; - -/// An instance of uninterruptible will suppress interrupts sent to the -/// context for the scope. Suppression does not discard any interrupt, -/// it merely ignores it at all interruption points until the suppression -/// ends, after which it will be thrown. -/// -struct ircd::ctx::this_ctx::uninterruptible -{ - struct nothrow; - - bool theirs; - - uninterruptible(); - uninterruptible(uninterruptible &&) = delete; - uninterruptible(const uninterruptible &) = delete; - ~uninterruptible() noexcept(false); -}; - -/// A variant of uinterruptible for users that must guarantee the ending of -/// the suppression scope will not be an interruption point. The default -/// behavior for uninterruptible is to throw, even from its destructor, to -/// fulfill the interruption request without any more delay. -/// -struct ircd::ctx::this_ctx::uninterruptible::nothrow -{ - bool theirs; - - nothrow() noexcept; - nothrow(nothrow &&) = delete; - nothrow(const nothrow &) = delete; - ~nothrow() noexcept; -}; - /// This overload matches ::sleep() and acts as a drop-in for ircd contexts. /// interruption point. inline void diff --git a/include/ircd/ctx/uninterruptible.h b/include/ircd/ctx/uninterruptible.h new file mode 100644 index 000000000..d881a182f --- /dev/null +++ b/include/ircd/ctx/uninterruptible.h @@ -0,0 +1,53 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +#pragma once +#define HAVE_IRCD_CTX_UNINTERRUPTIBLE_H + +namespace ircd::ctx::this_ctx +{ + struct uninterruptible; + + bool interruptible() noexcept; + void interruptible(const bool &); + void interruptible(const bool &, std::nothrow_t) noexcept; +} + +/// An instance of uninterruptible will suppress interrupts sent to the +/// context for the scope. Suppression does not discard any interrupt, +/// it merely ignores it at all interruption points until the suppression +/// ends, after which it will be thrown. +/// +struct ircd::ctx::this_ctx::uninterruptible +{ + struct nothrow; + + bool theirs; + + uninterruptible(); + uninterruptible(uninterruptible &&) = delete; + uninterruptible(const uninterruptible &) = delete; + ~uninterruptible() noexcept(false); +}; + +/// A variant of uinterruptible for users that must guarantee the ending of +/// the suppression scope will not be an interruption point. The default +/// behavior for uninterruptible is to throw, even from its destructor, to +/// fulfill the interruption request without any more delay. +/// +struct ircd::ctx::this_ctx::uninterruptible::nothrow +{ + bool theirs; + + nothrow() noexcept; + nothrow(nothrow &&) = delete; + nothrow(const nothrow &) = delete; + ~nothrow() noexcept; +}; diff --git a/ircd/ctx.cc b/ircd/ctx.cc index 1e73c6a6e..9edf5ea82 100644 --- a/ircd/ctx.cc +++ b/ircd/ctx.cc @@ -423,6 +423,7 @@ ircd::ctx::finished(const ctx &ctx) /// Indicates if `ctx` was terminated; does not clear the flag bool ircd::ctx::termination(const ctx &c) +noexcept { return c.flags & context::TERMINATED; } @@ -430,6 +431,7 @@ ircd::ctx::termination(const ctx &c) /// Indicates if `ctx` was interrupted; does not clear the flag bool ircd::ctx::interruption(const ctx &c) +noexcept { return c.flags & context::INTERRUPTED; } @@ -437,6 +439,7 @@ ircd::ctx::interruption(const ctx &c) /// Indicates if `ctx` will suppress any interrupts. bool ircd::ctx::interruptible(const ctx &c) +noexcept { return c.flags & ~context::NOINTERRUPT; } @@ -607,8 +610,6 @@ ircd::ctx::this_ctx::interruptible(const bool &b) interruption_point(); } -/// Throws interrupted if the currently running context was interrupted -/// and clears the interrupt flag. void ircd::ctx::this_ctx::interruptible(const bool &b, std::nothrow_t) @@ -617,6 +618,13 @@ noexcept interruptible(cur(), b); } +bool +ircd::ctx::this_ctx::interruptible() +noexcept +{ + return interruptible(cur()); +} + /// Throws interrupted if the currently running context was interrupted /// and clears the interrupt flag. void