31 #include <valgrind/valgrind.h>
38 #if defined(__APPLE__) && defined(__MACH__)
39 #define _DARWIN_C_SOURCE
61 class PrimaryFiber :
public Fiber
64 PrimaryFiber() :
Fiber(nullptr, 0) { setStarted(); }
65 void main() {
panic(
"PrimaryFiber main executed.\n"); }
68 PrimaryFiber _primaryFiber;
71 Fiber *_currentFiber = &_primaryFiber;
74 Fiber *startingFiber =
nullptr;
81 startingFiber->start();
88 link(link),
stack(nullptr), stackSize(stack_size), guardPage(nullptr),
89 guardPageSize(sysconf(_SC_PAGE_SIZE)), _started(false), _finished(false)
93 PROT_READ | PROT_WRITE,
94 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
97 fatal(
"Could not mmap %d byte fiber stack.\n", stack_size);
102 fatal(
"Could not forbid access to fiber stack guard page.");
106 valgrindStackId = VALGRIND_STACK_REGISTER(
113 panic_if(
stack && _currentFiber ==
this,
"Fiber stack is in use.");
115 VALGRIND_STACK_DEREGISTER(valgrindStackId);
128 ctx.uc_link =
nullptr;
133 startingFiber =
this;
134 panic_if(!_currentFiber,
"No active Fiber object.");
135 swapcontext(&_currentFiber->ctx, &
ctx);
144 startingFiber =
nullptr;
150 int ret M5_VAR_USED = swapcontext(&
ctx, &_currentFiber->ctx);
151 panic_if(ret == -1, strerror(errno));
168 if (_currentFiber ==
this)
175 Fiber *prev = _currentFiber;
177 _currentFiber = next;
178 swapcontext(&prev->
ctx, &next->
ctx);