@@ -105,20 +105,17 @@ module Streamly.Internal.Data.SVar
108−import Control.Concurrent
109− (ThreadId, myThreadId, threadDelay, throwTo, forkIO, killThread)
108+import Control.Concurrent (ThreadId, myThreadId, threadDelay, throwTo)
110import Control.Concurrent.MVar
109import Control.Concurrent.MVar
111 (MVar, newEmptyMVar, tryPutMVar, takeMVar, tryTakeMVar, newMVar,
110 (MVar, newEmptyMVar, tryPutMVar, takeMVar, tryTakeMVar, newMVar,
112 tryReadMVar)
111 tryReadMVar)
113import Control.Exception
112import Control.Exception
114− (SomeException(..), catch, mask, assert, Exception, catches,
113+ (SomeException(..), assert, Exception, catches,
115 throwIO, Handler(..), BlockedIndefinitelyOnMVar(..),
114 throwIO, Handler(..), BlockedIndefinitelyOnMVar(..),
116 BlockedIndefinitelyOnSTM(..))
115 BlockedIndefinitelyOnSTM(..))
117import Control.Monad (when)
116import Control.Monad (when)
118−import Control.Monad.Catch (MonadThrow)
119import Control.Monad.IO.Class (MonadIO(..))
117import Control.Monad.IO.Class (MonadIO(..))
120−import Control.Monad.Trans.Control
118+import Control.Monad.Trans.Control (MonadBaseControl, control)
121− (MonadBaseControl, control, StM, liftBaseDiscard)
122import Streamly.Internal.Data.Atomics
119import Streamly.Internal.Data.Atomics
123 (atomicModifyIORefCAS, atomicModifyIORefCAS_, writeBarrier,
120 (atomicModifyIORefCAS, atomicModifyIORefCAS_, writeBarrier,
124 storeLoadBarrier)
121 storeLoadBarrier)
@@ -126,21 +123,19 @@ import Data.Concurrent.Queue.MichaelScott (LinkedQueue, pushL)
126import Data.Functor (void)
123import Data.Functor (void)
127import Data.Heap (Heap, Entry(..))
124import Data.Heap (Heap, Entry(..))
128import Data.Int (Int64)
125import Data.Int (Int64)
129−import Data.Kind (Type)
130import Data.IORef
126import Data.IORef
131 (IORef, modifyIORef, newIORef, readIORef, writeIORef, atomicModifyIORef)
127 (IORef, modifyIORef, newIORef, readIORef, writeIORef, atomicModifyIORef)
128+import Data.Kind (Type)
132import Data.Maybe (fromJust, fromMaybe)
129import Data.Maybe (fromJust, fromMaybe)
133#if __GLASGOW_HASKELL__ < 808
130#if __GLASGOW_HASKELL__ < 808
134import Data.Semigroup ((<>))
131import Data.Semigroup ((<>))
136import Data.Set (Set)
133import Data.Set (Set)
137−import GHC.Conc (ThreadId(..))
139−import GHC.IO (IO(..))
140import System.IO (hPutStrLn, stderr)
134import System.IO (hPutStrLn, stderr)
141−import System.Mem.Weak (addFinalizer)
143−import Streamly.Internal.Data.Time.Clock (Clock(..), getTime)
136+import Streamly.Internal.Control.Concurrent
137+ (MonadAsync, RunInIO(..), doFork, fork, forkManaged)
138+import Streamly.Internal.Data.Time.System (Clock(..), getTime)
144import Streamly.Internal.Data.Time.Units
139import Streamly.Internal.Data.Time.Units
145 (AbsTime, NanoSecond64(..), MicroSecond64(..), diffAbsTime64,
140 (AbsTime, NanoSecond64(..), MicroSecond64(..), diffAbsTime64,
146 fromRelTime64, toRelTime64, showNanoSecond64, showRelTime64)
141 fromRelTime64, toRelTime64, showNanoSecond64, showRelTime64)
@@ -898,66 +893,16 @@ withDiagMVar sv label action =
898-- Spawning threads
893-- Spawning threads
899------------------------------------------------------------------------------
894------------------------------------------------------------------------------
901−-- /Since: 0.8.0 ("Streamly.Prelude")/
903−-- | A monad that can perform concurrent or parallel IO operations. Streams
904−-- that can be composed concurrently require the underlying monad to be
907−-- /Since: 0.1.0 ("Streamly")/
910−type MonadAsync m = (MonadIO m, MonadBaseControl IO m, MonadThrow m)
912−-- When we run computations concurrently, we completely isolate the state of
896+-- | When we run computations concurrently, we completely isolate the state of
913-- the concurrent computations from the parent computation. The invariant is
897-- the concurrent computations from the parent computation. The invariant is
914-- that we should never be running two concurrent computations in the same
898-- that we should never be running two concurrent computations in the same
915-- thread without using the runInIO function. Also, we should never be running
899-- thread without using the runInIO function. Also, we should never be running
916-- a concurrent computation in the parent thread, otherwise it may affect the
900-- a concurrent computation in the parent thread, otherwise it may affect the
917-- state of the parent which is against the defined semantics of concurrent
901-- state of the parent which is against the defined semantics of concurrent
918-- execution.
902-- execution.
919−newtype RunInIO m = RunInIO { runInIO :: forall b. m b -> IO (StM m b) }
921captureMonadState :: MonadBaseControl IO m => m (RunInIO m)
903captureMonadState :: MonadBaseControl IO m => m (RunInIO m)
922captureMonadState = control $ \run -> run (return $ RunInIO run)
904captureMonadState = control $ \run -> run (return $ RunInIO run)
924−-- Stolen from the async package. The perf improvement is modest, 2% on a
925−-- thread heavy benchmark (parallel composition using noop computations).
926−-- A version of forkIO that does not include the outer exception
927−-- handler: saves a bit of time when we will be installing our own
928−-- exception handler.
929−{-# INLINE rawForkIO #-}
930−rawForkIO :: IO () -> IO ThreadId
931−rawForkIO action = IO $ \ s ->
932− case fork# action s of (# s1, tid #) -> (# s1, ThreadId tid #)
934−{-# INLINE doFork #-}
935−doFork :: MonadBaseControl IO m
938− -> (SomeException -> IO ())
940−doFork action (RunInIO mrun) exHandler =
942− mask $ \restore -> do
943− tid <- rawForkIO $ catch (restore $ void $ mrun action)
947−{-# INLINABLE fork #-}
948−fork :: MonadBaseControl IO m => m () -> m ThreadId
949−fork = liftBaseDiscard forkIO
951−-- | Fork a thread that is automatically killed as soon as the reference to the
952−-- returned threadId is garbage collected.
954−{-# INLINABLE forkManaged #-}
955−forkManaged :: (MonadIO m, MonadBaseControl IO m) => m () -> m ThreadId
956−forkManaged action = do
958− liftIO $ addFinalizer tid (killThread tid)
961------------------------------------------------------------------------------
906------------------------------------------------------------------------------
962-- Collecting results from child workers in a streamed fashion
907-- Collecting results from child workers in a streamed fashion
963------------------------------------------------------------------------------
908------------------------------------------------------------------------------