Io, Io, it’s off to work we go…

Wednesday, February 15th, 2006

First of all, sorry for the title of this post, I just could not resist!

For the last few days, I have been working on the coroutine code in Io. Coroutines are implemented in a system dependent way.

For Windows, Io uses the fiber API. This is nice and simple.

On Unix platforms where it is available, Io uses ucontext.h which is also nice and simple (although the makecontext function call has a bit of an odd signature, understandably).

On other Unix platforms things get very hairy. Io uses setjmp to save the current stack context, modifies the stack context and eventually, when the coroutine gets called, uses it with longjmp. The modification of the stack context cannot be done in a portable way, for the very least because different systems have different registers and stack frame formats.

For linux x86, for example, this involves:

  1. changing the instruction pointer register (EIP for a 32bit machine) to point to our coroutine start function
  2. allocate a stack frame for the coroutine start function call
  3. push the start function parameters on top of the stack
  4. have the stack pointer register (ESP for a 32 bit machine) point to this made up frame

Note that for linux, ucontext.h is probably a better choice (is it guaranteed to be there on recent kernels, I’d need to check).

Things are entirely different on OS X, where stack frames are more complex to setup. Good luck Steve!

On a much lighter note, I setup a frappr map of the Io community.

%d bloggers like this: