@@ -43,6 +43,7 @@ import Asyncly.AsyncT
4343-- results in 2x performance degradation. At some point if that performance is
4444-- really needed we can combine the two.
4545
46+{-
4647newtype AsynclyT m a = AsynclyT { runAsynclyT :: AsyncT (StateT Context m) a }
4748
4849deriving instance Monad m => Functor (AsynclyT m)
@@ -52,7 +53,7 @@ deriving instance Monad m => Monad (AsynclyT m)
5253deriving instance MonadIO m => MonadIO (AsynclyT m)
5354deriving instance MonadThrow m => MonadThrow (AsynclyT m)
5455instance MonadTrans (AsynclyT) where
55− lift mx = AsynclyT $ AsyncT $ \_ k -> lift mx >>= (\a -> (k a Nothing))
56+ lift mx = AsynclyT $ AsyncT $ \c _ k -> lift mx >>= (\a -> (k a c Nothing))
5657
5758-- XXX orphan instance, use a newtype instead?
5859instance (Monad m, MonadRecorder m)
@@ -64,6 +65,10 @@ instance (Monad m, MonadRecorder m)
6465deriving instance (Monad m, MonadRecorder m)
6566 => MonadRecorder (AsynclyT m)
6667
68+-}
69+
70+type AsynclyT = AsyncT
71+
6772------------------------------------------------------------------------------
6873-- Running the monad
6974------------------------------------------------------------------------------
@@ -80,39 +85,40 @@ getContext lref = do
8085{-# INLINABLE runAsynclyLogged #-}
8186runAsynclyLogged :: MonadAsync m
8287 => Maybe (IORef [Recording]) -> AsynclyT m a -> m ()
83−runAsynclyLogged lref (AsynclyT m) = do
88+runAsynclyLogged lref m = do
8489 ctx <- liftIO $ getContext lref
85− _ <- runStateT (run m) ctx
86− return ()
90+ run ctx m
8791
8892 where
8993
9094 stop = return ()
91− run mx = (runAsyncT mx) stop (\_ r -> maybe stop run r)
95+ run ct mx = (runAsyncT mx) ct stop (\_ ct r ->
96+ maybe stop (\x -> (run ct x)) r)
9297
9398runAsyncly :: MonadAsync m => AsynclyT m a -> m ()
9499runAsyncly m = runAsynclyLogged Nothing m
95100
96−data Step a r = Stop | Done a | Yield a r
101+data Step a r = Stop | Done a | Yield a Context r
97102
98103-- | Run an 'AsynclyT m' computation and collect the results generated by each
99104-- thread of the computation in a list.
100105{-# INLINABLE toList #-}
101106toList :: MonadAsync m => AsynclyT m a -> m [a]
102−toList (AsynclyT m) = liftIO (getContext Nothing) >>= run m
107+toList m = do
108+ ctx <- liftIO (getContext Nothing)
109+ run ctx m
103110
104111 where
105112
106113 stop = return Stop
107114 done a = return (Done a)
108− yield a x = return $ Yield a x
115+ yield a c x = return $ Yield a c x
109116
110− run ma ctx = do
111− (res, ctx') <- runStateT
112− ((runAsyncT ma) stop (\a r -> maybe (done a) (\x -> yield a x) r))
113− ctx
117+ run ctx ma = do
118+ res <- (runAsyncT ma) ctx stop (\a c r ->
119+ maybe (done a) (\x -> yield a c x) r)
114120 case res of
115− Yield x mb -> liftM (x :) (run mb ctx')
121+ Yield x c mb -> liftM (x :) (run c mb)
116122 Stop -> return []
117123 Done x -> return (x : [])
118124
@@ -128,8 +134,8 @@ each xs = foldr (<|>) empty $ map return xs
128134-- tasks start synchronously in the current thread unless overridden by
129135-- 'async'.
130136threads :: MonadAsync m => Int -> AsynclyT m a -> AsynclyT m a
131−threads n action = AsynclyT $ AsyncT $ \stp yld ->
132− threadCtl n ((runAsyncT $ runAsynclyT action) stp yld)
137+threads n action = AsyncT $ \ctx stp yld ->
138+ (runAsyncT action) ctx stp yld
133139
134140------------------------------------------------------------------------------
135141-- Logging