2{-# LANGUAGE ConstraintKinds #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE GeneralizedNewtypeDeriving#-}
6{-# LANGUAGE InstanceSigs #-}
7{-# LANGUAGE LambdaCase #-}
8{-# LANGUAGE MultiParamTypeClasses #-}
9{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE UndecidableInstances #-} -- XXX
13-- Module : Streamly.Internal.Data.Stream.Async
14-- Copyright : (c) 2017 Harendra Kumar
17-- Maintainer : streamly@composewell.com
18-- Stability : experimental
22module Streamly.Internal.Data.Stream.Async
39import Control.Concurrent (myThreadId)
40import Control.Monad.Base (MonadBase(..), liftBaseDefault)
41import Control.Monad.Catch (MonadThrow, throwM)
42import Control.Concurrent.MVar (newEmptyMVar)
43-- import Control.Monad.Error.Class (MonadError(..))
44import Control.Monad.IO.Class (MonadIO(..))
45import Control.Monad.Reader.Class (MonadReader(..))
46import Control.Monad.State.Class (MonadState(..))
47import Control.Monad.Trans.Class (MonadTrans(lift))
48import Data.Concurrent.Queue.MichaelScott (LinkedQueue, newQ, nullQ, tryPopR)
49import Data.IORef (IORef, newIORef, readIORef)
50import Data.Maybe (fromJust)
51#if __GLASGOW_HASKELL__ < 808
52import Data.Semigroup (Semigroup(..))
55import Prelude hiding (map)
56import qualified Data.Set as S
58import Streamly.Internal.Data.Atomics (atomicModifyIORefCAS)
59import Streamly.Internal.Data.Stream.SVar (fromSVar)
60import Streamly.Internal.Data.SVar
61import Streamly.Internal.Data.Stream.StreamK
62 (IsStream(..), Stream, mkStream, foldStream, adapt, foldStreamShared,
64import qualified Streamly.Internal.Data.Stream.StreamK as K
65import qualified Streamly.Internal.Data.Stream.StreamD as D
67#include "Instances.hs"
69-------------------------------------------------------------------------------
71-------------------------------------------------------------------------------
73{-# INLINE workLoopLIFO #-}
81workLoopLIFO q st sv winfo = run
88 Nothing -> liftIO $ sendStop sv winfo
89 Just m -> foldStreamSVar sv st yieldk single run m
92 res <- liftIO $ sendYield sv winfo (ChildYield a)
93 if res then run else liftIO $ sendStop sv winfo
96 res <- liftIO $ sendYield sv winfo (ChildYield a)
98 then foldStreamShared st yieldk single run r
103 dequeue = liftIO $ atomicModifyIORefCAS q $ \case
105 x : xs -> (xs, Just x)
107-- We duplicate workLoop for yield limit and no limit cases because it has
108-- around 40% performance overhead in the worst case.
110-- XXX we can pass yinfo directly as an argument here so that we do not have to
111-- make a check every time.
112{-# INLINE workLoopLIFOLimited #-}
115 => IORef [Stream m a]
120workLoopLIFOLimited q st sv winfo = run
127 Nothing -> liftIO $ sendStop sv winfo
129 -- XXX This is just a best effort minimization of concurrency
130 -- to the yield limit. If the stream is made of concurrent
131 -- streams we do not reserve the yield limit in the constituent
132 -- streams before executing the action. This can be done
133 -- though, by sharing the yield limit ref with downstream
134 -- actions via state passing. Just a todo.
135 yieldLimitOk <- liftIO $ decrementYieldLimit sv
138 let stop = liftIO (incrementYieldLimit sv) >> run
139 foldStreamSVar sv st yieldk single stop m
140 -- Avoid any side effects, undo the yield limit decrement if we
141 -- never yielded anything.
144 incrementYieldLimit sv
148 res <- liftIO $ sendYield sv winfo (ChildYield a)
149 if res then run else liftIO $ sendStop sv winfo
151 -- XXX can we pass on the yield limit downstream to limit the concurrency
152 -- of constituent streams.
154 res <- liftIO $ sendYield sv winfo (ChildYield a)
155 yieldLimitOk <- liftIO $ decrementYieldLimit sv
156 let stop = liftIO (incrementYieldLimit sv) >> run
157 if res && yieldLimitOk
158 then foldStreamShared st yieldk single stop r
160 incrementYieldLimit sv
164 dequeue = liftIO $ atomicModifyIORefCAS q $ \case
166 x : xs -> (xs, Just x)
168-------------------------------------------------------------------------------
170-------------------------------------------------------------------------------
172-- XXX we can remove sv as it is derivable from st
174{-# INLINE workLoopFIFO #-}
177 => LinkedQueue (Stream m a)
182workLoopFIFO q st sv winfo = run
187 work <- liftIO $ tryPopR q
189 Nothing -> liftIO $ sendStop sv winfo
190 Just m -> foldStreamSVar sv st yieldk single run m
193 res <- liftIO $ sendYield sv winfo (ChildYield a)
194 if res then run else liftIO $ sendStop sv winfo
197 res <- liftIO $ sendYield sv winfo (ChildYield a)
199 then foldStreamShared st yieldk single run r
204{-# INLINE workLoopFIFOLimited #-}
207 => LinkedQueue (Stream m a)
212workLoopFIFOLimited q st sv winfo = run
217 work <- liftIO $ tryPopR q
219 Nothing -> liftIO $ sendStop sv winfo
221 yieldLimitOk <- liftIO $ decrementYieldLimit sv
224 let stop = liftIO (incrementYieldLimit sv) >> run
225 foldStreamSVar sv st yieldk single stop m
228 incrementYieldLimit sv
232 res <- liftIO $ sendYield sv winfo (ChildYield a)
233 if res then run else liftIO $ sendStop sv winfo
236 res <- liftIO $ sendYield sv winfo (ChildYield a)
237 yieldLimitOk <- liftIO $ decrementYieldLimit sv
238 let stop = liftIO (incrementYieldLimit sv) >> run
239 if res && yieldLimitOk
240 then foldStreamShared st yieldk single stop r
242 incrementYieldLimit sv
246-------------------------------------------------------------------------------
248-- This code belongs in SVar.hs but is kept here for perf reasons
249-------------------------------------------------------------------------------
251-- XXX we have this function in this file because passing runStreamLIFO as a
252-- function argument to this function results in a perf degradation of more
253-- than 10%. Need to investigate what the root cause is.
254-- Interestingly, the same thing does not make any difference for Ahead.
255getLifoSVar :: forall m a. MonadAsync m
256 => State Stream m a -> RunInIO m -> IO (SVar Stream m a)
257getLifoSVar st mrun = do
258 outQ <- newIORef ([], 0)
259 outQMv <- newEmptyMVar
261 wfw <- newIORef False
262 running <- newIORef S.empty
264 yl <- case getYieldLimit st of
265 Nothing -> return Nothing
266 Just x -> Just <$> newIORef x
267 rateInfo <- getYieldRateInfo st
269 stats <- newSVarStats
272 let isWorkFinished _ = null <$> readIORef q
274 let isWorkFinishedLimited sv = do
276 case remainingWork sv of
280 Nothing -> return False
281 qEmpty <- null <$> readIORef q
282 return $ qEmpty || yieldsDone
284 let getSVar :: SVar Stream m a
285 -> (SVar Stream m a -> m [ChildEvent a])
286 -> (SVar Stream m a -> m Bool)
287 -> (SVar Stream m a -> IO Bool)
288 -> (IORef [Stream m a]
294 getSVar sv readOutput postProc workDone wloop = SVar
296 , outputQueueFromConsumer = undefined
298 , maxBufferLimit = getMaxBuffer st
299 , pushBufferSpace = undefined
300 , pushBufferPolicy = undefined
301 , pushBufferMVar = undefined
302 , maxWorkerLimit = min (getMaxThreads st) (getMaxBuffer st)
303 , yieldRateInfo = rateInfo
304 , outputDoorBell = outQMv
305 , outputDoorBellFromConsumer = undefined
306 , readOutputQ = readOutput sv
307 , postProcess = postProc sv
308 , workerThreads = running
309 , workLoop = wloop q st{streamVar = Just sv} sv
310 , enqueue = enqueueLIFO sv q
311 , isWorkDone = workDone sv
312 , isQueueDone = workDone sv
314 , svarStyle = AsyncVar
315 , svarStopStyle = StopNone
316 , svarStopBy = undefined
318 , workerCount = active
319 , accountThread = delThread sv
320 , workerStopMVar = undefined
322 , svarInspectMode = getInspectMode st
324 , aheadWorkQueue = undefined
325 , outputHeap = undefined
330 case getStreamRate st of
332 case getYieldLimit st of
333 Nothing -> getSVar sv readOutputQBounded
337 Just _ -> getSVar sv readOutputQBounded
339 isWorkFinishedLimited
342 case getYieldLimit st of
343 Nothing -> getSVar sv readOutputQPaced
347 Just _ -> getSVar sv readOutputQPaced
349 isWorkFinishedLimited
353getFifoSVar :: forall m a. MonadAsync m
354 => State Stream m a -> RunInIO m -> IO (SVar Stream m a)
355getFifoSVar st mrun = do
356 outQ <- newIORef ([], 0)
357 outQMv <- newEmptyMVar
359 wfw <- newIORef False
360 running <- newIORef S.empty
362 yl <- case getYieldLimit st of
363 Nothing -> return Nothing
364 Just x -> Just <$> newIORef x
365 rateInfo <- getYieldRateInfo st
367 stats <- newSVarStats
370 let isWorkFinished _ = nullQ q
371 let isWorkFinishedLimited sv = do
373 case remainingWork sv of
377 Nothing -> return False
379 return $ qEmpty || yieldsDone
381 let getSVar :: SVar Stream m a
382 -> (SVar Stream m a -> m [ChildEvent a])
383 -> (SVar Stream m a -> m Bool)
384 -> (SVar Stream m a -> IO Bool)
385 -> (LinkedQueue (Stream m a)
391 getSVar sv readOutput postProc workDone wloop = SVar
393 , outputQueueFromConsumer = undefined
395 , maxBufferLimit = getMaxBuffer st
396 , pushBufferSpace = undefined
397 , pushBufferPolicy = undefined
398 , pushBufferMVar = undefined
399 , maxWorkerLimit = min (getMaxThreads st) (getMaxBuffer st)
400 , yieldRateInfo = rateInfo
401 , outputDoorBell = outQMv
402 , outputDoorBellFromConsumer = undefined
403 , readOutputQ = readOutput sv
404 , postProcess = postProc sv
405 , workerThreads = running
406 , workLoop = wloop q st{streamVar = Just sv} sv
407 , enqueue = enqueueFIFO sv q
408 , isWorkDone = workDone sv
409 , isQueueDone = workDone sv
411 , svarStyle = WAsyncVar
412 , svarStopStyle = StopNone
413 , svarStopBy = undefined
415 , workerCount = active
416 , accountThread = delThread sv
417 , workerStopMVar = undefined
419 , svarInspectMode = getInspectMode st
421 , aheadWorkQueue = undefined
422 , outputHeap = undefined
427 case getStreamRate st of
429 case getYieldLimit st of
430 Nothing -> getSVar sv readOutputQBounded
434 Just _ -> getSVar sv readOutputQBounded
436 isWorkFinishedLimited
439 case getYieldLimit st of
440 Nothing -> getSVar sv readOutputQPaced
444 Just _ -> getSVar sv readOutputQPaced
446 isWorkFinishedLimited
450{-# INLINABLE newAsyncVar #-}
451newAsyncVar :: MonadAsync m
452 => State Stream m a -> Stream m a -> m (SVar Stream m a)
454 mrun <- captureMonadState
455 sv <- liftIO $ getLifoSVar st mrun
458-- XXX Get rid of this?
459-- | Make a stream asynchronous, triggers the computation and returns a stream
460-- in the underlying monad representing the output generated by the original
461-- computation. The returned action is exhaustible and must be drained once. If
462-- not drained fully we may have a thread blocked forever and once exhausted it
463-- will always return 'empty'.
466{-# INLINABLE mkAsync #-}
467mkAsync :: (IsStream t, MonadAsync m) => t m a -> m (t m a)
468mkAsync = mkAsync' defState
470{-# INLINABLE mkAsync' #-}
471mkAsync' :: (IsStream t, MonadAsync m) => State Stream m a -> t m a -> m (t m a)
472mkAsync' st m = fmap fromSVar (newAsyncVar st (toStream m))
474-- | Create a new SVar and enqueue one stream computation on it.
475{-# INLINABLE newWAsyncVar #-}
476newWAsyncVar :: MonadAsync m
477 => State Stream m a -> Stream m a -> m (SVar Stream m a)
478newWAsyncVar st m = do
479 mrun <- captureMonadState
480 sv <- liftIO $ getFifoSVar st mrun
483------------------------------------------------------------------------------
484-- Running streams concurrently
485------------------------------------------------------------------------------
487-- Concurrency rate control.
489-- Our objective is to create more threads on demand if the consumer is running
490-- faster than us. As soon as we encounter a concurrent composition we create a
491-- push pull pair of threads. We use an SVar for communication between the
492-- consumer, pulling from the SVar and the producer who is pushing to the SVar.
493-- The producer creates more threads if the SVar drains and becomes empty, that
494-- is the consumer is running faster.
496-- XXX Note 1: This mechanism can be problematic if the initial production
497-- latency is high, we may end up creating too many threads. So we need some
498-- way to monitor and use the latency as well. Having a limit on the dispatches
499-- (programmer controlled) may also help.
501-- TBD Note 2: We may want to run computations at the lower level of the
502-- composition tree serially even when they are composed using a parallel
503-- combinator. We can use 'serial' in place of 'async' and 'wSerial' in
504-- place of 'wAsync'. If we find that an SVar immediately above a computation
505-- gets drained empty we can switch to parallelizing the computation. For that
506-- we can use a state flag to fork the rest of the computation at any point of
507-- time inside the Monad bind operation if the consumer is running at a faster
510-- TBD Note 3: the binary operation ('parallel') composition allows us to
511-- dispatch a chunkSize of only 1. If we have to dispatch in arbitrary
512-- chunksizes we will need to compose the parallel actions using a data
513-- constructor (A Free container) instead so that we can divide it in chunks of
514-- arbitrary size before dispatching. If the stream is composed of
515-- hierarchically composed grains of different sizes then we can always switch
516-- to a desired granularity depending on the consumer speed.
518-- TBD Note 4: for pure work (when we are not in the IO monad) we can divide it
519-- into just the number of CPUs.
521-- | Join two computations on the currently running 'SVar' queue for concurrent
522-- execution. When we are using parallel composition, an SVar is passed around
523-- as a state variable. We try to schedule a new parallel computation on the
524-- SVar passed to us. The first time, when no SVar exists, a new SVar is
525-- created. Subsequently, 'joinStreamVarAsync' may get called when a computation
526-- already scheduled on the SVar is further evaluated. For example, when (a
527-- `parallel` b) is evaluated it calls a 'joinStreamVarAsync' to put 'a' and 'b' on
528-- the current scheduler queue.
530-- The 'SVarStyle' required by the current composition context is passed as one
531-- of the parameters. If the scheduling and composition style of the new
532-- computation being scheduled is different than the style of the current SVar,
533-- then we create a new SVar and schedule it on that. The newly created SVar
534-- joins as one of the computations on the current SVar queue.
536-- Cases when we need to switch to a new SVar:
538-- * (x `parallel` y) `parallel` (t `parallel` u) -- all of them get scheduled on the same SVar
539-- * (x `parallel` y) `parallel` (t `async` u) -- @t@ and @u@ get scheduled on a new child SVar
540-- because of the scheduling policy change.
541-- * if we 'adapt' a stream of type 'async' to a stream of type
542-- 'Parallel', we create a new SVar at the transitioning bind.
543-- * When the stream is switching from disjunctive composition to conjunctive
544-- composition and vice-versa we create a new SVar to isolate the scheduling
547forkSVarAsync :: (IsStream t, MonadAsync m)
548 => SVarStyle -> t m a -> t m a -> t m a
549forkSVarAsync style m1 m2 = mkStream $ \st stp sng yld -> do
551 AsyncVar -> newAsyncVar st (concurrently (toStream m1) (toStream m2))
552 WAsyncVar -> newWAsyncVar st (concurrently (toStream m1) (toStream m2))
553 _ -> error "illegal svar type"
554 foldStream st stp sng yld $ fromSVar sv
556 concurrently ma mb = mkStream $ \st stp sng yld -> do
557 liftIO $ enqueue (fromJust $ streamVar st) mb
558 foldStreamShared st stp sng yld ma
560{-# INLINE joinStreamVarAsync #-}
561joinStreamVarAsync :: (IsStream t, MonadAsync m)
562 => SVarStyle -> t m a -> t m a -> t m a
563joinStreamVarAsync style m1 m2 = mkStream $ \st stp sng yld ->
565 Just sv | svarStyle sv == style -> do
566 liftIO $ enqueue sv (toStream m2)
567 foldStreamShared st stp sng yld m1
568 _ -> foldStreamShared st stp sng yld (forkSVarAsync style m1 m2)
570------------------------------------------------------------------------------
571-- Semigroup and Monoid style compositions for parallel actions
572------------------------------------------------------------------------------
574-- | Polymorphic version of the 'Semigroup' operation '<>' of 'AsyncT'.
575-- Merges two streams possibly concurrently, preferring the
576-- elements from the left one when available.
580async :: (IsStream t, MonadAsync m) => t m a -> t m a -> t m a
581async = joinStreamVarAsync AsyncVar
586{-# DEPRECATED (<|) "Please use 'async' instead." #-}
588(<|) :: (IsStream t, MonadAsync m) => t m a -> t m a -> t m a
591-- IMPORTANT: using a monomorphically typed and SPECIALIZED consMAsync makes a
592-- huge difference in the performance of consM in IsStream instance even we
593-- have a SPECIALIZE in the instance.
595-- | XXX we can implement it more efficienty by directly implementing instead
596-- of combining streams using async.
597{-# INLINE consMAsync #-}
598{-# SPECIALIZE consMAsync :: IO a -> AsyncT IO a -> AsyncT IO a #-}
599consMAsync :: MonadAsync m => m a -> AsyncT m a -> AsyncT m a
600consMAsync m r = fromStream $ K.yieldM m `async` (toStream r)
602------------------------------------------------------------------------------
604------------------------------------------------------------------------------
606-- | The 'Semigroup' operation for 'AsyncT' appends two streams. The combined
607-- stream behaves like a single stream with the actions from the second stream
608-- appended to the first stream. The combined stream is evaluated in the
609-- asynchronous style. This operation can be used to fold an infinite lazy
610-- container of streams.
614-- import qualified "Streamly.Prelude" as S
615-- import Control.Concurrent
617-- main = (S.toList . 'asyncly' $ (S.fromList [1,2]) \<> (S.fromList [3,4])) >>= print
623-- Any exceptions generated by a constituent stream are propagated to the
624-- output stream. The output and exceptions from a single stream are guaranteed
625-- to arrive in the same order in the resulting stream as they were generated
626-- in the input stream. However, the relative ordering of elements from
627-- different streams in the resulting stream can vary depending on scheduling
628-- and generation delays.
630-- Similarly, the monad instance of 'AsyncT' /may/ run each iteration
631-- concurrently based on demand. More concurrent iterations are started only
632-- if the previous iterations are not able to produce enough output for the
636-- main = 'drain' . 'asyncly' $ do
637-- n <- return 3 \<\> return 2 \<\> return 1
639-- threadDelay (n * 1000000)
640-- myThreadId >>= \\tid -> putStrLn (show tid ++ ": Delay " ++ show n)
643-- ThreadId 40: Delay 1
644-- ThreadId 39: Delay 2
645-- ThreadId 38: Delay 3
649newtype AsyncT m a = AsyncT {getAsyncT :: Stream m a}
650 deriving (MonadTrans)
652-- | A demand driven left biased parallely composing IO stream of elements of
653-- type @a@. See 'AsyncT' documentation for more details.
656type Async = AsyncT IO
658-- | Fix the type of a polymorphic stream as 'AsyncT'.
661asyncly :: IsStream t => AsyncT m a -> t m a
664instance IsStream AsyncT where
670------------------------------------------------------------------------------
672------------------------------------------------------------------------------
674-- Monomorphically typed version of "async" for better performance of Semigroup
676{-# INLINE mappendAsync #-}
677{-# SPECIALIZE mappendAsync :: AsyncT IO a -> AsyncT IO a -> AsyncT IO a #-}
678mappendAsync :: MonadAsync m => AsyncT m a -> AsyncT m a -> AsyncT m a
679mappendAsync m1 m2 = fromStream $ async (toStream m1) (toStream m2)
681instance MonadAsync m => Semigroup (AsyncT m a) where
684------------------------------------------------------------------------------
686------------------------------------------------------------------------------
688instance MonadAsync m => Monoid (AsyncT m a) where
692------------------------------------------------------------------------------
694------------------------------------------------------------------------------
696{-# INLINE apAsync #-}
697{-# SPECIALIZE apAsync :: AsyncT IO (a -> b) -> AsyncT IO a -> AsyncT IO b #-}
698apAsync :: MonadAsync m => AsyncT m (a -> b) -> AsyncT m a -> AsyncT m b
699apAsync (AsyncT m1) (AsyncT m2) =
700 let f x1 = K.concatMapBy async (pure . x1) m2
701 in AsyncT $ K.concatMapBy async f m1
703instance (Monad m, MonadAsync m) => Applicative (AsyncT m) where
705 pure = AsyncT . K.yield
709------------------------------------------------------------------------------
711------------------------------------------------------------------------------
713-- GHC: if we change the implementation of bindWith with arguments in a
714-- different order we see a significant performance degradation (~2x).
715{-# INLINE bindAsync #-}
716{-# SPECIALIZE bindAsync :: AsyncT IO a -> (a -> AsyncT IO b) -> AsyncT IO b #-}
717bindAsync :: MonadAsync m => AsyncT m a -> (a -> AsyncT m b) -> AsyncT m b
718bindAsync m f = fromStream $ K.bindWith async (adapt m) (\a -> adapt $ f a)
720-- GHC: if we specify arguments in the definition of (>>=) we see a significant
721-- performance degradation (~2x).
722instance MonadAsync m => Monad (AsyncT m) where
726------------------------------------------------------------------------------
728------------------------------------------------------------------------------
730MONAD_COMMON_INSTANCES(AsyncT, MONADPARALLEL)
732------------------------------------------------------------------------------
734------------------------------------------------------------------------------
736-- | XXX we can implement it more efficienty by directly implementing instead
737-- of combining streams using wAsync.
738{-# INLINE consMWAsync #-}
739{-# SPECIALIZE consMWAsync :: IO a -> WAsyncT IO a -> WAsyncT IO a #-}
740consMWAsync :: MonadAsync m => m a -> WAsyncT m a -> WAsyncT m a
741consMWAsync m r = fromStream $ K.yieldM m `wAsync` (toStream r)
743-- | Polymorphic version of the 'Semigroup' operation '<>' of 'WAsyncT'.
744-- Merges two streams concurrently choosing elements from both fairly.
748wAsync :: (IsStream t, MonadAsync m) => t m a -> t m a -> t m a
749wAsync = joinStreamVarAsync WAsyncVar
751-- | The 'Semigroup' operation for 'WAsyncT' interleaves the elements from the
752-- two streams. Therefore, when @a <> b@ is evaluated, one action is picked
753-- from stream @a@ for evaluation and then the next action is picked from
754-- stream @b@ and then the next action is again picked from stream @a@, going
755-- around in a round-robin fashion. Many such actions are executed concurrently
756-- depending on 'maxThreads' and 'maxBuffer' limits. Results are served to the
757-- consumer in the order completion of the actions.
759-- Note that when multiple actions are combined like @a <> b <> c ... <> z@ we
760-- go in a round-robin fasion across all of them picking one action from each
761-- up to @z@ and then come back to @a@. Note that this operation cannot be
762-- used to fold a container of infinite streams as the state that it needs to
763-- maintain is proportional to the number of streams.
767-- import qualified "Streamly.Prelude" as S
768-- import Control.Concurrent
770-- main = (S.toList . 'wAsyncly' $ (S.fromList [1,2]) \<> (S.fromList [3,4])) >>= print
776-- Any exceptions generated by a constituent stream are propagated to the
777-- output stream. The output and exceptions from a single stream are guaranteed
778-- to arrive in the same order in the resulting stream as they were generated
779-- in the input stream. However, the relative ordering of elements from
780-- different streams in the resulting stream can vary depending on scheduling
781-- and generation delays.
783-- Similarly, the 'Monad' instance of 'WAsyncT' runs /all/ iterations fairly
784-- concurrently using a round robin scheduling.
787-- main = 'drain' . 'wAsyncly' $ do
788-- n <- return 3 \<\> return 2 \<\> return 1
790-- threadDelay (n * 1000000)
791-- myThreadId >>= \\tid -> putStrLn (show tid ++ ": Delay " ++ show n)
794-- ThreadId 40: Delay 1
795-- ThreadId 39: Delay 2
796-- ThreadId 38: Delay 3
800newtype WAsyncT m a = WAsyncT {getWAsyncT :: Stream m a}
801 deriving (MonadTrans)
803-- | A round robin parallely composing IO stream of elements of type @a@.
804-- See 'WAsyncT' documentation for more details.
807type WAsync = WAsyncT IO
809-- | Fix the type of a polymorphic stream as 'WAsyncT'.
812wAsyncly :: IsStream t => WAsyncT m a -> t m a
815instance IsStream WAsyncT where
816 toStream = getWAsyncT
821------------------------------------------------------------------------------
823------------------------------------------------------------------------------
825{-# INLINE mappendWAsync #-}
826{-# SPECIALIZE mappendWAsync :: WAsyncT IO a -> WAsyncT IO a -> WAsyncT IO a #-}
827mappendWAsync :: MonadAsync m => WAsyncT m a -> WAsyncT m a -> WAsyncT m a
828mappendWAsync m1 m2 = fromStream $ wAsync (toStream m1) (toStream m2)
830instance MonadAsync m => Semigroup (WAsyncT m a) where
833------------------------------------------------------------------------------
835------------------------------------------------------------------------------
837instance MonadAsync m => Monoid (WAsyncT m a) where
841------------------------------------------------------------------------------
843------------------------------------------------------------------------------
845{-# INLINE apWAsync #-}
846{-# SPECIALIZE apWAsync :: WAsyncT IO (a -> b) -> WAsyncT IO a -> WAsyncT IO b #-}
847apWAsync :: MonadAsync m => WAsyncT m (a -> b) -> WAsyncT m a -> WAsyncT m b
848apWAsync (WAsyncT m1) (WAsyncT m2) =
849 let f x1 = K.concatMapBy wAsync (pure . x1) m2
850 in WAsyncT $ K.concatMapBy wAsync f m1
852-- GHC: if we specify arguments in the definition of (<*>) we see a significant
853-- performance degradation (~2x).
854instance (Monad m, MonadAsync m) => Applicative (WAsyncT m) where
855 pure = WAsyncT . K.yield
858------------------------------------------------------------------------------
860------------------------------------------------------------------------------
862-- GHC: if we change the implementation of bindWith with arguments in a
863-- different order we see a significant performance degradation (~2x).
864{-# INLINE bindWAsync #-}
865{-# SPECIALIZE bindWAsync :: WAsyncT IO a -> (a -> WAsyncT IO b) -> WAsyncT IO b #-}
866bindWAsync :: MonadAsync m => WAsyncT m a -> (a -> WAsyncT m b) -> WAsyncT m b
867bindWAsync m f = fromStream $ K.bindWith wAsync (adapt m) (\a -> adapt $ f a)
869-- GHC: if we specify arguments in the definition of (>>=) we see a significant
870-- performance degradation (~2x).
871instance MonadAsync m => Monad (WAsyncT m) where
875------------------------------------------------------------------------------
877------------------------------------------------------------------------------
879MONAD_COMMON_INSTANCES(WAsyncT, MONADPARALLEL)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list