mono/mono — pull request #387 by mrieker (2012-07-10 15:34:14+00:00)
Title: microthread implmentation using CreateThread model instead of setjmp/longjmp model

Using the existing Continuation.cs seems hard to define what is switched and what isn't.

SomeMainMethod()
{
some-local-vars-1
Continuation c = new Continuation();
some-local-vars-2
c.Mark();
some-local-vars-3
CallSomethingThatCallsSomethingThatCallsStore(c);
what in local-vars-1,2,3 gets saved and restored?
}

It's hard to say what of local-vars-1-2-3 gets saved and restored because it's not defined where the local vars are relative to top_sp as collected by c.Mark(). Maybe the JIT compilation optimized one of them into say R13 or something like that, and so R13 doesn't get saved on the stack until inside CallSomethingThatCallsSomethingThatCallsStore(c). So R13 will get saved and restored, which is probably not what is intended, and of course might change when seemingly unrelated parts of the code are changed. It might not even get saved until inside continuation_store() and even then there is no guarantee.

Using the CreateThread model, there is a distinct barrier between what is saved and what is not:

SomeMainMethod()
{
some-local-vars-1
MyUThread c = new MyUThread();
some-local-vars-2
CallSomethingThatCallsSomethingThatCallsStart(c);
all of local-vars-1,2 preserved
}

CallSomethingThatCallsSomethingThatCallsStart(MMRUThread c)
{
some-local-vars-3
CallSomethingThatCallsStart(c);
all of local-vars-3 preserved
}

CallSomethingThatCallsStart(MMRUThread c)
{
some-local-vars-4
c.Start();
all of local-vars-4 preserved
}

No matter what the compiler optimizes, all local vars in the 'macro' thread are preserved because there really isn't any restore mechanism as such. The top_sp that Continuation.Mark() had is embedded within the MMRUThread.Start() call and so all local vars created by the callers of MMRUThread.Start() are above its equivalent of top_sp, and MMRUThread.Start() is smart enough to force all caller register contents to be above its equivalent of top_sp.