Coco is a small extension to get True C Coroutine semantics for Lua 5.1.
Coco is both available as a stand-alone release and integrated into LuaJIT 1.x.
The stand-alone release is a patchset against the » standard Lua 5.1.4 distribution. There are no dependencies on LuaJIT. However LuaJIT 1.x depends on Coco to allow yielding for JIT compiled functions.
Coco is Copyright © 2004-2011 Mike Pall. Coco is free software, released under the » MIT/X license (same license as the Lua core).
Features
True C coroutine semantics mean you can yield from a coroutine across a C call boundary and resume back to it.
Coco allows you to use a dedicated C stack for each coroutine. Resuming a coroutine and yielding from a coroutine automatically switches C stacks, too.
In particular you can now:
- Yield across all metamethods (not advised for __gc).
- Yield across iterator functions (for x in func do).
- Yield across callbacks (table.foreach(), dofile(), ...).
- Yield across protected callbacks (pcall(), xpcall(), ...).
- Yield from C functions and resume back to them.
Best of all, you don't need to change your Lua or C sources and still get the benefits. It's fully integrated into the Lua core, but tries to minimize the required changes.
More ...
Please visit the » Download page to fetch the current version of the stand-alone package.
Coco needs some machine-specific features — please have a look at the Portability Requirements.
Coco also provides some upwards-compatible API Extensions for Lua.