This functions prints a format-less, ZERO-TERMINATED string. Hence, the @system interface. Also, cannot fail. \n must flush.
This is not _exactly_ the same as C's realloc function!
UFCS Getter for context allocator.
UFCS Setter for context allocator.
Return current context object. An ImplicitContext is safely copyable, but only the top-most context per-thread can be modified (like in _the_ stack). There is no reason to store it though?
UFCS Setter for context allocator.
UFCS Getter for context allocator.
Restore formerly pushed context from thread-local context stack.
Saves context on the thread-local context stack. The current context() becomes a copy of that. Needs to be paired with pop.
UFCS Getter for user pointer.
UFCS Setter for user pointer.
A "context" implements per-thread scoped globals. It is a frame in the secundary stack.
- allocators - loggers - the UI "context" in UIs ...
Note: internal stack is handled with malloc/realloc/free from the C stdlib. Performance of internal stack relies on a malloc implementation that is itself lockfree, else the realloc could stall other threads from time to time. The context are also not designed to be very large.
Important difference: context.push() and context.pop() are explicit here. Leaving a D scope {} doesn't restore parent context (there is no language support), you need to call push/pop manually. On the plus side, no ABI change are needed and context stack is not meant to be touched very frequently.
context.set!int("userStuff", 4); assert(context.get!int("userStuff") == 4);
void subProc() { context.push;
assert(context.get!int("userStuff") == 4); // follows chain of contexts
context.set!int("userStuff", 3); assert(context.get!int("userStuff") == 3); // stack-like organization of contexts
context.pop;
assert(context.get!int("userStuff") == 4); // stack-like organization of contexts, with hierarchic namespacing } subProc();
This is an implicit context system like in Odin, itself probably inspired by Scala "implicit parameters". In other words, a system to have scoped globals.
"In each scope, there is an implicit value named context. This CONTEXT VARIABLE is local to each scope and is implicitly passed by pointer to any procedure call in that scope (if the procedure has the Odin calling convention)."
Without language support, we don't have the ABI and the scope-specific context, however with TLS and manual scopes we can emulate that to pass parameters in an implicit way (scoped globals, in a way).
Note: this module uses TLS, and C runtime.
Copyright Guillaume Piolat 2021.
Contextual parameter system for D.