31 #include <valgrind/valgrind.h>
38 #if defined(__APPLE__) && defined(__MACH__)
39 #define _DARWIN_C_SOURCE
62 class PrimaryFiber :
public Fiber
65 PrimaryFiber() : Fiber(nullptr, 0) { setStarted(); }
66 void main() {
panic(
"PrimaryFiber main executed.\n"); }
69 PrimaryFiber _primaryFiber;
72 Fiber *_currentFiber = &_primaryFiber;
75 Fiber *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);