[Mono-dev] Mono Continuations and MicroThreads

Paolo Molaro lupus at ximian.com
Tue Apr 4 06:10:35 EDT 2006

On 04/04/06 Tomi Valkeinen wrote:
> Attached is a small patch to mono runtime that implements continuations 
> for amd64 and x86 linux, two C# files that implement basic 
> MicroThread-stuff, and some C# test programs. You can also get a full 

Wow! Nice:)

> About the overall implementation: IT'S A HACK! =). It works, but I know 
> many ways to break it. It lacks sanity checks. It probably misses a few 
> arguments every now and then. It kinda ignores GC, which probably will 
> also break it. The new GC will probably break it even more. I haven't even 

If you allocate the area where you store the stack with with
mono_gc_alloc_fixed (size, NULL), both the old and the new GC will
conservatively scan the area for pointers (but make sure that when you
copy it the values are properly aligned (start and top of the copied
stack must be pointer-sized aligned)). You currently use malloc/g_new,
so even the current GC won't see the objects and collect them.
The effect on the new GC will be mostly to slow it down, since it won't
be able to move the objects that may be referenced by the saved stack
(this of course depends on how many objects are referenced and how large
they are).

> thought about CAS. Not forgetting the millions of things that I don't know 
> about that will also break it. But still, the tests I've included work, 
> and it's been great fun playing with them.

You should likely have the size of the saved stack as a param and of
course you'd need to check for overflows on it.
Also, for continuations that include managed->unmanaged->managed
transitions, you need to save and restore the LMF chain (I haven't
checked all your code, so maybe you handle that already).

> My first question to the list is: is anyone else interested in this, using 
> continuations and/or developing them further? Shall I keep quiet from this 
> on or do you want to hear more?

I think every interesting and clever development with mono is on topic
for this list:) I would love if people would contribute to and enhance your
version. We always envisioned mono as a virtual machine that enables
such projects and support for continuations would enable also more
languages to be targeted at running on the mono runtime...

> My second question is: how to make the native code better? I don't know 
> much about Mono internals, this was my first mono runtime project so all 
> feedback is appreciated. Also I'd very much like to hear if someone can 
> point out why this way of implementing continuations to Monois 
> fundamentally flawed. This requires reading the code, but if some of you 
> Mono-gurus have the time and can educate me...

See above for a way to deal with GC issues.
Eventually also security info in the stack frames would need to be
saved and you'd have to deal with cross-appdomain transitions.
This mechanism can't work in some scenarios, but they are not easily
accessed in C# code anyway (passing the address of a local var to a real
subthread that is supposed to store or read from it: the stack is saved
and restored bu the subthread doesn't know it ...).


lupus at debian.org                                     debian/rules
lupus at ximian.com                             Monkeys do it better

More information about the Mono-devel-list mailing list