Asynchronous exceptions and "resume"
Wed, 26 Mar 2003 14:35:33 -0000
> Hi all, is there a way, or is it planned to, or has anyone published
> articles on... resuming from asynchronous exceptions?
> I mean: it would be useful there was a=20
> suspend :: ThreadID -> IO ()
> where the result is the remaining computation of the other=20
> thread, wich
> one could forkIO again, or simply run in the current thread.=20
This is fairly easy to implement in GHC. Simple call
Schedule.c:raiseAsync() with NULL for the exception, and make it return
the final closure after it has stripped the thread's stack. Entering
that closure should restart the thread's computation exactly where it
left off. If the thread was blocked on something (eg. takeMVar), then
it should restart the blocking operation.
The types aren't quite right: the closure you get back will behave like
an application of unsafePerformIO, so you will need to wrap it using eg.
Control.Exception.evaluate to get an IO action.
I'm sure there are plenty of cool uses for this...