Going Beyond the Standard: Continuations in Mono
While Mono usually strives to follow the C# and Common Language Infrastructure specifications, it does occasionally go beyond them. While some features such as SIMD support are backwards-compatible with .NET, runtime supported continuations are exclusive to Mono.
A continuation is like a thread frozen in time, a snap-shot if you will. It includes stack frames and local variables, but unlike a true thread it is not executable. A simple form of continuation can be seen in C#’s yield operator. But yield is just a compiler trick that creates a state machine; it isn’t a general purpose continuation. For that you need almost need support from the runtime.
The result of Tomi Valkeinen's research into runtime support is the Mono.Tasklet API. This allows for general purpose continuations that actually capture the stack. The stack can be restored as often as necessary. It should be noted that this form of continuation can capture references but not the heap allocated objects those references point to. On top of this he built his microthreading library. This consists of lightweight threads that use continuations and cooperative multitasking to share a single physical thread.
Earlier we said “almost” because there is another way to build a general purpose continuation. Jim Purbrick of LindenLabs built a continuation system that uses CIL rewriting instead of runtime suppport. Compared to Valkeinen's continuations, this is a rather heavy weight way to build continuations. But it does have the ability to serialize not only the stack but also objects on the heap. This allows the continuation to be restored on another server, which is very important to Second Life’s scalable architecture. Long term LindenLabs would like to combine the two techniques, using Mono Tasklets for fast continuations and their technique when continuations need to be serialized.
Anatole Tresch Mar 03, 2015