January 1998

ZFiber: Threads At Interrupt Time

Fibers, at least in this context, are threads implemented using setjmp/longjmp to transfer control in a manner reminiscent of co-routines. They are a generalization of the stack-swapping used by NetPhone to run in the constrained environment it experienced when its deferred tasks were scheduled when an application with a tiny stack was current (yes Print Monitor, I'm talking about you).

Whereas NetPhone allocated some space for a stack to be used when invoking a stack-hungry function (the GSM codec generally), ZFiber calls setjmp to record the current stack and registers, then calls longjmp on the setjmp buffer previously initialized by the next fiber to be executed. Because we have complete control over the entire process this makes it very easy to turn async callback-based functions (like most of the Mac's IO subsystem) into blocking function calls. And the whole thing can run at deferred task time, simply by having the callback for an async function schedule a deferred task which calls longjmp to the first fiber that's marked as being both runnable and safe to be run at deferred task time. A fiber can change its deferred task time eligibility on the fly, so sections handling IO can be run at deferred task time, and thus not be subject to the latency imposed by having to wait till GetNextEvent, but sections that have to touch non interrupt-safe sections of the ToolBox can be run exclusively at normal time.

Projects by Category

Recent Projects

Project Archives