Faster timeout but is it correct?  Bas van Dijk v.dijk.bas at
Wed Feb 16 20:26:04 CET 2011  I realized that the previous
timeout had problems when called in a masked thread. What
happens is that the call to killThread will block because it
can't throw the KillThread exception to the timeout thread
because that thread is masked. I have to use unsafeUnmask to
always unmask the timeout thread. Note that, for some reason,
using forkIOUnmasked ... is much slower than using unsafeUnmask
$ forkIO .... Any idea why?  import GHC.IO (unsafeUnmask)
imeout :: Int -> IO a -> IO (Maybe a) timeout n f     | n <  0
= fmap Just f     | n == 0    = return Nothing     | otherwise =
do         myTid <- myThreadId         timeoutEx  <- fmap
Timeout newUnique         uninterruptibleMask $ \restore -> do
tid <- unsafeUnmask $ forkIO $                    threadDelay n
>> throwTo myTid timeoutEx           (restore (fmap Just f) >>=
\mb -> killThread tid >> return mb)             `catch` \e ->
case fromEx