darcs patch: forkChild, waitForChild, parIO, timeout

Taral taralx at gmail.com
Tue Nov 7 11:04:24 EST 2006


On 11/7/06, Simon Marlow <simonmarhaskell at gmail.com> wrote:
> So 'timeout N E' should behave exactly the same as E as long as E doesn't time
> out.  Three difficulties with this:
>
>   1. if E raises an exception, you want the exception propagated to the parent.
>
>   2. if another thread throws an exception to this thread, E should receive the
>      exception.
>
>   3. E should get the same result from myThreadId.

I think 2 and 3 go together, and require an exception. Here's my try:

data Timeout = Timeout deriving Typeable

timeout n f
    | n < 0 = fmap Just f
    | n == 0 = return Nothing
    | otherwise = do
        pt <- myThreadId
        bracket (forkIO (threadDelay n >> throwDynTo pt Timeout))
                (killThread)
                (fmap Just f) `catchDyn` \Timeout -> return Nothing

-- 
Taral <taralx at gmail.com>
"You can't prove anything."
    -- Gödel's Incompetence Theorem


More information about the Libraries mailing list