In C fibers can be coded using setjmp()
and longjmp()
to implement context switches at user level.
As described in
evanjones.ca and Portable Multithreading(pdf) it is also required that each fiber has a newly allocated stack.
Since a fiber lives in a thread context, when it is invoked will have automatically a stack frame associated to it, so why is it required this new allocated stack? : when a fiber wants to switch to another one, the following approach could be used :
cpu_context[N] :global array where the i-th entry is the cpu context(jmp_buffer) of the i-th fiber
fiber_ith :
[...]
if ( setjmp(cpu_context[i]) == 0 ){
longjmp(cpu_context[j])
}
[...]
The necessity of a new stack is due to the fact that as written here, it is not possible to use longjmp()
to go back to a fiber execution whose stack frame is not anymore valid from the instant that fiber calls longjmp()
?
EDIT : these fibers must be non preemtive and can voluntary switch from one fiber to another one