0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-01 01:28:54 +02:00
construct/include/ircd/ctx
2023-02-20 20:15:44 -08:00
..
async.h ircd::ctx::async: Fix template default param using runtime extern. 2019-06-21 03:41:13 -07:00
concurrent.h Add noexcept to various lambdas (gcc-11). 2022-06-29 14:42:42 -07:00
concurrent_for_each.h ircd::ctx: Minor fix inconsistent header HAVE_ defines. 2019-09-30 14:23:28 -07:00
condition_variable.h ircd::ctx: Fix extra semicolons on scope. 2022-06-17 21:11:54 -07:00
context.h ircd::ctx: Support user-supplied stacks. 2020-10-14 02:22:45 -07:00
continuation.h ircd: Various symbol surface reductions and sorting. 2022-06-17 21:11:53 -07:00
critical_assertion.h ircd::ctx: Ensure assert_critical() callsite DCE in non-debugmode. 2019-10-09 16:30:08 -07:00
critical_indicator.h ircd::ctx::this_ctx: Fix inline namespace reopening for clang. 2019-06-23 07:37:23 -06:00
ctx.h ircd: Correct/abridge various comments. 2022-06-17 21:11:52 -07:00
dock.h ircd::ctx::dock: Add options for waiters; support queuing strategies. 2023-02-20 20:15:44 -08:00
exception_handler.h ircd::ctx: Split exception_handler w/ cxxabi header requirement. 2019-07-21 15:29:16 -07:00
fault.h Update Copyrastafaris. 2018-02-05 21:24:34 -08:00
future.h Revert "ircd: ISO C++ requires template on destructor names out-of-line." (gcc-11/12) 2022-06-20 17:26:39 -07:00
latch.h ircd::ctx::latch: Replace closure w/ simple bind statement. 2020-07-11 14:07:08 -07:00
list.h ircd::ctx::list: Add mid-list insertion suite to interface. 2023-02-20 20:15:44 -08:00
mutex.h ircd: Specify explicit noexcept for gcc-10 issue. 2020-07-11 17:57:33 -07:00
ole.h ircd: Various symbol surface reductions and sorting. 2022-06-17 21:11:53 -07:00
pool.h ircd::ctx::pool: Add option for dispatch strategy; behavior changed by default. 2023-02-20 20:15:44 -08:00
posix.h ircd::ctx::posix: Trapdoor complex allowing real pthreads to work again. 2021-01-04 01:12:49 -08:00
prof.h ircd::ctx: Integrate slice cycle counters with ios. 2020-08-21 10:43:04 -07:00
promise.h ircd::ctx: Specify inline linkage for various templates to prevent any dynsyms. 2020-07-11 15:41:57 -07:00
queue.h ircd::ctx::queue: Add options for FIFO and LIFO strategies to interface. 2023-02-20 20:15:44 -08:00
README.md ircd::ctx: Use vg::stack registration in stack allocator. 2020-10-16 14:32:06 -07:00
scope_notify.h ircd::ctx: Add scope_notify device. 2019-03-21 16:46:04 -07:00
shared_mutex.h ircd::buffer: Fix template name conflicts for clang-11; apply inline linkages. 2020-10-29 04:06:59 -07:00
shared_state.h ircd::ctx: Add missing shared_state already_t w/ value ctor. 2020-10-15 04:55:38 -07:00
sleep.h ircd::ctx: Split sleep()/wait() suites into headers. 2020-02-28 09:11:06 -08:00
slice_usage_warning.h ircd::ctx: Measure and report epoch span for slice_usage_warning. 2020-05-24 17:14:02 -07:00
stack.h ircd::ctx: Update stack allocator for official support. (boost-1.80) 2023-01-19 19:58:22 -08:00
stack_usage_assertion.h ircd::ctx::this_ctx: Fix inline namespace reopening for clang. 2019-06-23 07:37:23 -06:00
this_ctx.h ircd::ios: Consolidate dispatch/post/defer interfaces; minor fixes. 2020-12-20 06:02:50 -08:00
trit.h ircd::ctx: Preliminary trilean logic concept (WIP). 2019-09-30 14:23:28 -07:00
uninterruptible.h ircd::ctx: Inline various codepaths leading to flags(ctx). 2020-06-18 03:56:09 -07:00
unlock_guard.h Revert "ircd: ISO C++ requires template on destructor names out-of-line." (gcc-11/12) 2022-06-20 17:26:39 -07:00
upgrade_lock.h Revert "ircd: ISO C++ requires template on destructor names out-of-line." (gcc-11/12) 2022-06-20 17:26:39 -07:00
view.h Revert "ircd: ISO C++ requires template on destructor names out-of-line." (gcc-11/12) 2022-06-20 17:26:39 -07:00
wait.h ircd::ctx: Fix erroneous comment. [ci skip] 2023-02-04 13:20:44 -08:00
when.h ircd::ctx: Fix inconsistent refcount() behavior. 2019-09-08 18:22:05 -07:00

Userspace Context Switching

The ircd::ctx subsystem is a userspace threading library meant to regress the asynchronous callback pattern back to synchronous suspensions. These are stackful coroutines which provide developers with more intuitive control in environments which conduct frequent I/O which would otherwise break up a single asynchronous stack into callback-hell.

Motivation

Userspace threads are an alternative to using posix kernel threads as a way to develop intuitively-stackful programs in applications which are primarily I/O-bound rather than CPU-bound. This is born out of a recognition that a single CPU core has enough capacity to compute the entirety of all requests for an efficiently-written network daemon if I/O were instantaneous; if one could use a single thread it is advantageous to do so right up until the compute-bound is reached, rather than introducing more threads for any other reason. The limits to single-threading and scaling beyond a single CPU is then pushed to higher-level application logic: either message-passing between multiple processes (or machines in a cluster), and/or threads which have extremely low interference.

ircd::ctx allows for a very large number of contexts to exist, on the order of thousands or more, and still efficiently make progress without the overhead of kernel context switches. As an anecdotal example, a kernel context switch from a contended mutex could realistically be five to ten times more costly than a userspace context switch if not significantly more, and with effects that are less predictable. Contexts will accomplish as much work as possible in a "straight line" before yielding to the kernel to wait for the completion of any I/O event.

There is no preemptive interleaving of contexts. This makes every sequence of instructions executed a natural transaction requiring no other method of exclusion. It also allows for introspective conditions, i.e: check if context switch occurred: if so, refresh value; if not, the old value is good. This is impossible in a preemptive environment as the result may have changed at instruction boundaries rather than at cooperative boundaries.

Foundation

This library is embedded around boost::coroutine / boost::context.

boost::asio has then added its own comprehensive integration with the above libraries eliminating the need for us to worry about a lot of boilerplate to de-async the asio networking calls. See: boost::asio::spawn.

This is a nice boost, but that's as far as it goes. The rest is on us here to actually make a threading library.

Interface

We mimic the standard library std::thread suite as much as possible (which mimics the boost::thread library) and offer alternative threading primitives for these userspace contexts rather than those for operating system threads in std:: such as ctx::mutex and ctx::condition_variable and ctx::future among others.

  • The primary user object is ircd::context (or ircd::ctx::context) which has an std::thread-like interface. By default the lifetime of this instance has significance, and when context goes out of scope, a termination is sent to the child context and the parent yields until it has joined (see DETACH and WAIT_JOIN flags to change this behavior). Take note that this means by default a child context may never be able to complete (or even be entered at all!) if the parent constructs and then desctructs context without either any flags or any strategy by the parent to wait for completion.

Context Switching

A context switch has the overhead of a heavy function call -- a function with a bunch of arguments (i.e the registers being saved and restored). We consider this fast and our philosophy is to not think about the context switch itself as a bad thing to be avoided for its own sake.

This system is also fully integrated both with the IRCd core boost::asio::io_service event loop and networking systems. There are actually several types of context switches going on here built on two primitives:

  • Direct jump: This is the fastest switch. Context A can yield to context B directly if A knows about B and if it knows that B is in a state ready to resume from a direct jump and that A will also be further resumed somehow. This is not always suitable in practice so other techniques may be used instead.

  • Queued wakeup: This is the common default and safe switch. This is where the context system integrates with the boost::asio::io_service event loop. The execution of a "slice" as we'll call a yield-to-yield run of non-stop computation is analogous to a function posted to the io_service in the asynchronous pattern. Context A can enqueue context B if it knows about B and then choose whether to yield or not to yield. In any case the io_service queue will simply continue to the next task which isn't guaranteed to be B.

The most common context switching encountered and wielded by developers is the ctx::dock, a non-locking condition variable. The power of the dock is in its:

  1. Lightweight. It's just a ctx::list; two pointers for a list head, where the nodes are the contexts themselves participating in the list.

  2. Cooperative-condition optimized. When a context is waiting on a condition it provides, the ctx system can run the function itself to test the condition without waking up the context.

When does Context Switching (yielding) occur?

Bottom line is that this is simply not javascript. There are no stack-clairvoyant keywords like await which explicitly indicate to everyone everywhere that the overall state of the program before and after any totally benign-looking function call will be different. This is indeed multi-threaded programming but in a very PG-13 rated way. You have to assume that if you aren't sure some function has a "deep yield" somewhere way up the stack that there is a potential for yielding in that function. Unlike real concurrent threading everything beyond this is much easier.

  • Anything directly on your stack is safe (same with real MT anyway).

  • static and global assets are safe if you can assert no yielding. Such an assertion can be made with an instance of ircd::ctx::critical_assertion. Note that we try to use thread_local rather than static to still respect any real multi-threading that may occur now or in the future.

  • Calls which may yield and do IO may be marked with [GET] and [SET] conventional labels but they may not be. Some reasoning about obvious yields and a zen-like awareness is always recommended.