{-# LANGUAGE CPP, NoImplicitPrelude, RankNTypes, ImpredicativeTypes #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif
module Control.Concurrent.Thread
(
forkIO
, forkOS
, forkOn
, forkIOWithUnmask
, forkOnWithUnmask
, Result
, result
) where
import qualified Control.Concurrent ( forkOS
, forkIOWithUnmask
, forkOnWithUnmask
)
import Control.Concurrent ( ThreadId )
import Control.Concurrent.MVar ( newEmptyMVar, putMVar, readMVar )
import Control.Exception ( SomeException, try, throwIO, mask )
import Control.Monad ( return, (>>=) )
import Data.Either ( Either(..), either )
import Data.Function ( (.), ($) )
import Data.Int ( Int )
import System.IO ( IO )
import Control.Concurrent.Raw ( rawForkIO, rawForkOn )
forkIO :: IO a -> IO (ThreadId, IO (Result a))
forkIO :: forall a. IO a -> IO (ThreadId, IO (Result a))
forkIO = forall a.
(IO () -> IO ThreadId) -> IO a -> IO (ThreadId, IO (Result a))
fork IO () -> IO ThreadId
rawForkIO
forkOS :: IO a -> IO (ThreadId, IO (Result a))
forkOS :: forall a. IO a -> IO (ThreadId, IO (Result a))
forkOS = forall a.
(IO () -> IO ThreadId) -> IO a -> IO (ThreadId, IO (Result a))
fork IO () -> IO ThreadId
Control.Concurrent.forkOS
forkOn :: Int -> IO a -> IO (ThreadId, IO (Result a))
forkOn :: forall a. Int -> IO a -> IO (ThreadId, IO (Result a))
forkOn = forall a.
(IO () -> IO ThreadId) -> IO a -> IO (ThreadId, IO (Result a))
fork forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> IO () -> IO ThreadId
rawForkOn
forkIOWithUnmask
:: ((forall b. IO b -> IO b) -> IO a) -> IO (ThreadId, IO (Result a))
forkIOWithUnmask :: forall a.
((forall b. IO b -> IO b) -> IO a) -> IO (ThreadId, IO (Result a))
forkIOWithUnmask = forall a.
(((forall b. IO b -> IO b) -> IO ()) -> IO ThreadId)
-> ((forall b. IO b -> IO b) -> IO a)
-> IO (ThreadId, IO (Result a))
forkWithUnmask ((forall b. IO b -> IO b) -> IO ()) -> IO ThreadId
Control.Concurrent.forkIOWithUnmask
forkOnWithUnmask
:: Int -> ((forall b. IO b -> IO b) -> IO a) -> IO (ThreadId, IO (Result a))
forkOnWithUnmask :: forall a.
Int
-> ((forall b. IO b -> IO b) -> IO a)
-> IO (ThreadId, IO (Result a))
forkOnWithUnmask = forall a.
(((forall b. IO b -> IO b) -> IO ()) -> IO ThreadId)
-> ((forall b. IO b -> IO b) -> IO a)
-> IO (ThreadId, IO (Result a))
forkWithUnmask forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ((forall b. IO b -> IO b) -> IO ()) -> IO ThreadId
Control.Concurrent.forkOnWithUnmask
fork :: (IO () -> IO ThreadId) -> (IO a -> IO (ThreadId, IO (Result a)))
fork :: forall a.
(IO () -> IO ThreadId) -> IO a -> IO (ThreadId, IO (Result a))
fork IO () -> IO ThreadId
doFork = \IO a
a -> do
MVar (Result a)
res <- forall a. IO (MVar a)
newEmptyMVar
ThreadId
tid <- forall b. ((forall b. IO b -> IO b) -> IO b) -> IO b
mask forall a b. (a -> b) -> a -> b
$ \forall b. IO b -> IO b
restore -> IO () -> IO ThreadId
doFork forall a b. (a -> b) -> a -> b
$ forall e a. Exception e => IO a -> IO (Either e a)
try (forall b. IO b -> IO b
restore IO a
a) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. MVar a -> a -> IO ()
putMVar MVar (Result a)
res
forall (m :: * -> *) a. Monad m => a -> m a
return (ThreadId
tid, forall a. MVar a -> IO a
readMVar MVar (Result a)
res)
forkWithUnmask
:: (((forall b. IO b -> IO b) -> IO ()) -> IO ThreadId)
-> ((forall b. IO b -> IO b) -> IO a) -> IO (ThreadId, IO (Result a))
forkWithUnmask :: forall a.
(((forall b. IO b -> IO b) -> IO ()) -> IO ThreadId)
-> ((forall b. IO b -> IO b) -> IO a)
-> IO (ThreadId, IO (Result a))
forkWithUnmask ((forall b. IO b -> IO b) -> IO ()) -> IO ThreadId
doForkWithUnmask = \(forall b. IO b -> IO b) -> IO a
f -> do
MVar (Result a)
res <- forall a. IO (MVar a)
newEmptyMVar
ThreadId
tid <- forall b. ((forall b. IO b -> IO b) -> IO b) -> IO b
mask forall a b. (a -> b) -> a -> b
$ \forall b. IO b -> IO b
restore ->
((forall b. IO b -> IO b) -> IO ()) -> IO ThreadId
doForkWithUnmask forall a b. (a -> b) -> a -> b
$ \forall b. IO b -> IO b
unmask ->
forall e a. Exception e => IO a -> IO (Either e a)
try (forall b. IO b -> IO b
restore forall a b. (a -> b) -> a -> b
$ (forall b. IO b -> IO b) -> IO a
f forall b. IO b -> IO b
unmask) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. MVar a -> a -> IO ()
putMVar MVar (Result a)
res
forall (m :: * -> *) a. Monad m => a -> m a
return (ThreadId
tid, forall a. MVar a -> IO a
readMVar MVar (Result a)
res)
type Result a = Either SomeException a
result :: Result a -> IO a
result :: forall a. Result a -> IO a
result = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall e a. Exception e => e -> IO a
throwIO forall (m :: * -> *) a. Monad m => a -> m a
return