Coroutines and exceptions

Daniel Keep daniel.keep.lists at gmail.com
Fri Apr 28 17:13:12 PDT 2006


Hi.

I've been playing around with an implementation of coroutines in D the
last day or two, and I've got it to the point where it works pretty well
under gdc, and under dmd with a few bugs to track down.  But I've hit a
brick will with exceptions.

See, I was kinda hoping that D found exception handlers by going back up
the stack frames... although after pouring over the disassembly in gdb,
it looks like D registers the exception handlers as it meets them.

The problem with this is that the coroutine library allows you to jump
from one part of the code to another, which ends up screwing up the
order of exception handlers.  For example (simplified, mind you):

void coro_a()
{
    try
    {
    	co_call(main); // Jump back into main just after co_call
    }
    except( Exception e )
    {
        writefln("Exception in coro_a: %s", e.toString());
    }
}

void main()
{
    try
    {
        co_call(coro_a); // Jump into coro_a
        throw new Exception("Oh noes!");
    }
    catch( Exception e )
    {
        writefln("Exception in main: %s", e.toString());
    }
}

If you run that, you end up getting "Exception in coro_a: Oh noes!"
instead of "Exception in main: Oh noes!" as you would expect.

This is pretty much a show-stopper for coroutines; if exceptions don't
work with them, then the library won't be much good.

SO, the question is: assuming that you register exception handlers on to
some kind of internal stack of addresses to jump to in the event of an
exception, is there any possibility of being able to, say... swap out
that stack for a different one at runtime?

The CPU doesn't seem to mind when I do that to the program stack :P

Please and thankyou,

	-- Daniel Keep

-- 

v1sw5+8Yhw5ln4+5pr6OFma8u6+7Lw4Tm6+7l6+7D
a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP    http://hackerkey.com/



More information about the Digitalmars-d mailing list