开发者

Crash using Mono.Tasklets.Continuation

I'm attempting to work on a microthread message passing library in C# using mono. Since Mono 2.4 or so, apparently continuations (not yield) have been available under 'Mono.Tasklets'. However, these lack any documentation that I can find, and while my general use of them works, I get an occasional (but reproducable) crash that the debugger won't attach to.

My specific questions are:

Does anyone know what the Mark() function is, and where/when I need to call it? It seems to be a one-time initialization, but I don't know why that wouldn't be in the constructor then.

Are there limitations 开发者_JS百科on using multiple continuations? I've discovered that you cannot pass a continuation to another thread, but it seems I can store multiple continuations and switch back and forth?


LE: Regarding the crash problem see these open bugs: Bug 566324 , Bug 580791 , Bug 602502

I'm fairly new to this myself, I present here the information I gathered so far. Maybe it will be useful.

1) The Mono.Tasklets library (described by Miguel de Icaza [READ: here]) is a continuation library that can be used to construct various forms of continuation systems and light weight(LW) threads.

A simple way of picturing this is the Mono-version of C's longjmp/setjmp, they can only be used to unwind the stack.

The library was initially developed by a guy [READ: here] and now it's included in Mono and documented [READ: here] (follow those links and you'll find more information)

That guy implemented a Microthreading library on top of this abstraction. and now that has been ported to Mono.Tasklets framework

2) A continuation is an object that can be used to store the current execution state and it can then be used to restore the stored state later.

"Execution state" here means the stack, which includes call stack and local variables, and the processor's registers.

When the stored state is restored the program execution looks like it jumps back to the position where the state was saved, with all the local variables restored.

An example in C. and more info on Wikipedia/Continuations

3) The API is :

public class Continuation {
      public Continuation ();
      public void Mark ();
      public int Store (int state);
      public void Restore (int state);
 }

Continuations can be used to implement microthreads. You can look at the code from Mono.MicroThreads available at Github [READ: here]

    public Continuation()
    {
        m_handle = alloc_continuation();
        Print("Continuation()");
    }

    public void Mark()
    {
        Print("Mark()");
        // skip 1 frame, ie. this function
        mark_continuation_frame(m_handle, 1);
    }

    public int Store(int data)
    {
        Print("Store({0})", data);
        int res = store_continuation(m_handle, data);
        Print("Store({0}) = {1}", data, res);
        return res;
    }

    public void Restore(int data)
    {
        Print("Restore({0})", data);
        restore_continuation(m_handle, data);
        Print("Restore() exit (NEVER REACHED)");
    }

From what is presented here:

Mark() it is used to mark the topmost frame to be stored

Store(x) stores the current state to the continuation, and returns the given integer x.

Restore(y) restores the stored state, and returns the given integer y. (Note that the integer y given to Restore is actually returned from the Store() method, because that's where we are after the state has been restored.)

static void Main()
{
 Continuation c = new Continuation();
 c.Mark();
 int foo = 123;
 int val = c.Store(0);
 Console.WriteLine("{0} {1}", val, foo);
 foo = 321;
 if (val < 5)
     c.Restore(val + 1);
}

When you call Store() the current state of execution is recorded and it is possible to go back to this state by invoking Restore().

The caller to Store() tells whether it is the initial store or a restore point based on the result from Store:

 var c = new Continuation ();
 ...

 switch (c.Store (0)){
 case 0:
      // First invocation
 case 1:
      // Restored from the point ahead.
 }
 ...
 // Jump back to the switch statement.
 c.Restore (1); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜