2 issues and 1 proposal for runInUnboundThread

Bas van Dijk v.dijk.bas at gmail.com
Thu Oct 14 18:00:24 EDT 2010


I found two small issues in Control.Concurrent:

* Both runInBoundThread and runInUnboundThread use throw instead of
throwIO. It's nicer to use throwIO in an IO context instead of throw
because it guarantees ordering with respect to other IO actions.

* runInUnboundThread explicitly checks the blocked status of the
current thread which is redundant because mask also performs this

I also have a new proposal:

When you throw an asynchronous exception to a thread which is
executing: `runInUnboundThread m`, m will keep executing and there's
no way to kill it.

I propose to catch asynchronous exceptions in runInUnboundThread and
throw them to the thread which is executing m. m in turn can decide to
catch or ignore them. In case m decides to ignore them or to rethrow
them, the exception will be rethrown in the current thread:

runInUnboundThread :: IO a -> IO a
runInUnboundThread action = do
  bound <- isCurrentThreadBound
  if bound
    then do
      mv <- newEmptyMVar
      mask $ \restore -> do
        tid <- forkIO $ Exception.try (restore action) >>= putMVar mv
        let wait = takeMVar mv `Exception.catch` \(e :: SomeException) ->
                     Exception.throwTo tid e >> wait
        wait >>= unsafeResult
    else action

unsafeResult :: Either SomeException a -> IO a
unsafeResult = either Exception.throwIO return

The patch bundle attached to the ticket fixes the two issues and
implements the above behavior.

Discussion deadline:

I don't think the fixes for the issues need discussion. They only need
a review from one of the GHC devs.

With regard to the proposal I propose a discussion deadline of 2 weeks
from now: Thursday 28 October.

Ticket: http://hackage.haskell.org/trac/ghc/ticket/4400



More information about the Libraries mailing list