32 return Context::init();
59 }
catch(std::bad_alloc
const&) {
68 int res = context->create();
77 if(
likely(attr.stackSize > 0)) {
78#ifdef ZTH_CONTEXT_WINFIBER
81 Stack const& stack = context->stackUsable();
83 context, stack.
p, stack.
p + stack.
size - 1U);
118 from->context_switch(*to);
130 if(!Config::EnableStackWaterMark || !context)
134 void* guard =
nullptr;
136 if(f && f->
context() == context) {
139 guard = context->stackGuard(
nullptr);
146 context->stackGuard(guard);
170 context->stackGuard();
174 context->attr().entry(context->attr().arg);
184 zth_abort(
"Returned to finished context");
194#define ZTH_STACK_WATERMARKER ((uint64_t)0x5a7468574de2889eULL)
203static void stack_watermark_align(
void*& stack,
size_t*& sizeptr,
size_t* size =
nullptr) noexcept
208 uintptr_t stack_ =
reinterpret_cast<uintptr_t
>(stack);
211 stack_ = (stack_ +
sizeof(size_t) - 1U) & ~(uintptr_t)(
sizeof(
size_t) - 1U);
213 sizeptr =
reinterpret_cast<size_t*
>(stack_);
216 stack_ +=
sizeof(size_t);
219 *size -= stack_ - (uintptr_t)stack;
221 stack =
reinterpret_cast<void*
>(stack_);
230 if(!Config::EnableStackWaterMark || !stack || !size)
236 size_t* sizeptr =
nullptr;
237 stack_watermark_align(stack, sizeptr, &size);
240 for(
size_t i = 0; i *
sizeof(uint64_t) <= size -
sizeof(uint64_t); i++)
252 if(!Config::EnableStackWaterMark || !stack)
255 size_t* sizeptr =
nullptr;
256 stack_watermark_align(stack, sizeptr);
268 if(!Config::EnableStackWaterMark || !stack)
271 size_t* sizeptr =
nullptr;
272 stack_watermark_align(stack, sizeptr);
273 size_t size = *sizeptr;
277#ifdef ZTH_USE_VALGRIND
278 VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE(stack, size);
281 for(uint64_t* s =
static_cast<uint64_t*
>(stack);
285#ifdef ZTH_USE_VALGRIND
286 VALGRIND_ENABLE_ADDR_ERROR_REPORTING_IN_RANGE(stack, size);
289 return size - unused *
sizeof(uint64_t);
305#ifdef ZTH_STACK_SWITCH
306void*
zth_stack_switch(
void* stack,
size_t size,
void* (*f)(
void*)
noexcept,
void* arg)
noexcept
Context * context() const noexcept
static safe_ptr< singleton_type >::type instance() noexcept
Return the only instance of T within this thread.
The class that manages the fibers within this thread.
Fiber * currentFiber() const noexcept
void * stack_switch(void *stack, size_t size, void *(*f)(void *) noexcept, void *arg) noexcept
Context switch mechanism implementation.
#define ZTH_STACK_WATERMARKER
void context_entry(zth::Context *context) noexcept
Entry point of the fiber.
#define zth_stack_switch(stack, size, f, arg)
Call the function f using the new stack pointer.
void zth_abort(char const *fmt,...)
Aborts the process after printing the given printf() formatted message.
Worker & currentWorker() noexcept
Return the (thread-local) singleton Worker instance.
fiber_type< F >::fiber fiber(F &&f, Args &&... args)
Create and start a new fiber.
void yield(Fiber *preferFiber=nullptr, bool alwaysYield=false, Timestamp const &now=Timestamp::now())
Allow a context switch.
size_t stack_watermark_remaining(void *stack) noexcept
Return the remaining stack size that was never touched.
void stack_watermark_init(void *stack, size_t size) noexcept
Initialize the memory region for stack high water marking.
size_t stack_watermark_maxused(void *stack) noexcept
Return the high water mark of the stack.
size_t stack_watermark_size(void *stack) noexcept
Return the size of the given stack region.
#define zth_dbg(group, fmt, a...)
Debug printf()-like function.
void context_switch(Context *from, Context *to) noexcept
Perform context switch.
void context_destroy(Context *context) noexcept
Destroy and cleanup a context.
int context_init() noexcept
One-time context mechanism initialization.
string err(int e)
Return a string like strerror() does, but as a zth::string.
void context_deinit() noexcept
Final cleanup.
int context_create(Context *&context, ContextAttr const &attr) noexcept
Create a context.
size_t context_stack_usage(Context *context) noexcept
Return the high water mark of the stack of the given context.
#define zth_assert(expr)
assert(), but better integrated in Zth.
#define likely(expr)
Marks the given expression to likely be evaluated to true.