31 #include <valgrind/valgrind.h>
38 #if defined(__APPLE__) && defined(__MACH__)
39 #define _DARWIN_C_SOURCE
59 class PrimaryFiber :
public Fiber
63 void main() {
panic(
"PrimaryFiber main executed.\n"); }
66 PrimaryFiber _primaryFiber;
69 Fiber *_currentFiber = &_primaryFiber;
72 Fiber *startingFiber =
nullptr;
79 startingFiber->start();
86 link(link),
stack(nullptr), stackSize(stack_size), guardPage(nullptr),
87 guardPageSize(sysconf(_SC_PAGE_SIZE)), _started(false), _finished(false)
91 PROT_READ | PROT_WRITE,
92 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
95 fatal(
"Could not mmap %d byte fiber stack.\n", stack_size);
100 fatal(
"Could not forbid access to fiber stack guard page.");
104 valgrindStackId = VALGRIND_STACK_REGISTER(
111 panic_if(
stack && _currentFiber ==
this,
"Fiber stack is in use.");
113 VALGRIND_STACK_DEREGISTER(valgrindStackId);
126 ctx.uc_link =
nullptr;
131 startingFiber =
this;
132 panic_if(!_currentFiber,
"No active Fiber object.");
133 swapcontext(&_currentFiber->ctx, &
ctx);
142 startingFiber =
nullptr;
146 if (_setjmp(
jmp) == 0) {
149 int ret = swapcontext(&
ctx, &_currentFiber->ctx);
150 panic_if(ret == -1, strerror(errno));
168 if (_currentFiber ==
this)
175 Fiber *prev = _currentFiber;
177 _currentFiber = next;
178 if (_setjmp(prev->
jmp) == 0)
179 _longjmp(next->
jmp, 1);