31#include <valgrind/valgrind.h>
38#if defined(__APPLE__) && defined(__MACH__)
39#define _DARWIN_C_SOURCE
62class PrimaryFiber :
public Fiber
65 PrimaryFiber() : Fiber(nullptr, 0) { setStarted(); }
66 void main() {
panic(
"PrimaryFiber main executed.\n"); }
69PrimaryFiber _primaryFiber;
72Fiber *_currentFiber = &_primaryFiber;
75Fiber *startingFiber =
nullptr;
82 startingFiber->start();
89 link(link),
stack(nullptr), stackSize(stack_size), guardPage(nullptr),
90 guardPageSize(sysconf(_SC_PAGE_SIZE)), _started(false), _finished(false)
94 PROT_READ | PROT_WRITE,
95 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
98 fatal(
"Could not mmap %d byte fiber stack.\n", stack_size);
103 fatal(
"Could not forbid access to fiber stack guard page.");
107 valgrindStackId = VALGRIND_STACK_REGISTER(
114 panic_if(
stack && _currentFiber ==
this,
"Fiber stack is in use.");
116 VALGRIND_STACK_DEREGISTER(valgrindStackId);
129 ctx.uc_link =
nullptr;
134 startingFiber =
this;
135 panic_if(!_currentFiber,
"No active Fiber object.");
136 swapcontext(&_currentFiber->ctx, &
ctx);
145 startingFiber =
nullptr;
149 if (_setjmp(
jmp) == 0) {
152 int ret = swapcontext(&
ctx, &_currentFiber->ctx);
153 panic_if(ret == -1, strerror(errno));
171 if (_currentFiber ==
this)
178 Fiber *prev = _currentFiber;
180 _currentFiber = next;
181 if (_setjmp(prev->
jmp) == 0)
182 _longjmp(next->
jmp, 1);
This class represents a fiber, which is a light weight sort of thread which is cooperatively schedule...
virtual void main()=0
This method is called when this fiber is first run.
static void entryTrampoline()
static Fiber * currentFiber()
Get a pointer to the current running Fiber.
static Fiber * primaryFiber()
Get a pointer to the primary Fiber.
Fiber(size_t stack_size=DefaultStackSize)
void run()
Start executing the fiber represented by this object.
#define panic(...)
This implements a cprintf based panic() function.
#define fatal(...)
This implements a cprintf based fatal() function.
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.