@@ -43,6 +43,7 @@ import Asyncly.AsyncT
43-- results in 2x performance degradation. At some point if that performance is
43-- results in 2x performance degradation. At some point if that performance is
44-- really needed we can combine the two.
44-- really needed we can combine the two.
46newtype AsynclyT m a = AsynclyT { runAsynclyT :: AsyncT (StateT Context m) a }
47newtype AsynclyT m a = AsynclyT { runAsynclyT :: AsyncT (StateT Context m) a }
48deriving instance Monad m => Functor (AsynclyT m)
49deriving instance Monad m => Functor (AsynclyT m)
@@ -52,7 +53,7 @@ deriving instance Monad m => Monad (AsynclyT m)
52deriving instance MonadIO m => MonadIO (AsynclyT m)
53deriving instance MonadIO m => MonadIO (AsynclyT m)
53deriving instance MonadThrow m => MonadThrow (AsynclyT m)
54deriving instance MonadThrow m => MonadThrow (AsynclyT m)
54instance MonadTrans (AsynclyT) where
55instance 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))
57-- XXX orphan instance, use a newtype instead?
58-- XXX orphan instance, use a newtype instead?
58instance (Monad m, MonadRecorder m)
59instance (Monad m, MonadRecorder m)
@@ -64,6 +65,10 @@ instance (Monad m, MonadRecorder m)
64deriving instance (Monad m, MonadRecorder m)
65deriving instance (Monad m, MonadRecorder m)
65 => MonadRecorder (AsynclyT m)
66 => MonadRecorder (AsynclyT m)
70+type AsynclyT = AsyncT
67------------------------------------------------------------------------------
72------------------------------------------------------------------------------
68-- Running the monad
73-- Running the monad
69------------------------------------------------------------------------------
74------------------------------------------------------------------------------
@@ -80,39 +85,40 @@ getContext lref = do
80{-# INLINABLE runAsynclyLogged #-}
85{-# INLINABLE runAsynclyLogged #-}
81runAsynclyLogged :: MonadAsync m
86runAsynclyLogged :: MonadAsync m
82 => Maybe (IORef [Recording]) -> AsynclyT m a -> m ()
87 => Maybe (IORef [Recording]) -> AsynclyT m a -> m ()
83−runAsynclyLogged lref (AsynclyT m) = do
88+runAsynclyLogged lref m = do
84 ctx <- liftIO $ getContext lref
89 ctx <- liftIO $ getContext lref
85− _ <- runStateT (run m) ctx
90+ run ctx m
90 stop = return ()
94 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)
93runAsyncly :: MonadAsync m => AsynclyT m a -> m ()
98runAsyncly :: MonadAsync m => AsynclyT m a -> m ()
94runAsyncly m = runAsynclyLogged Nothing m
99runAsyncly m = runAsynclyLogged Nothing m
96−data Step a r = Stop | Done a | Yield a r
101+data Step a r = Stop | Done a | Yield a Context r
98-- | Run an 'AsynclyT m' computation and collect the results generated by each
103-- | Run an 'AsynclyT m' computation and collect the results generated by each
99-- thread of the computation in a list.
104-- thread of the computation in a list.
100{-# INLINABLE toList #-}
105{-# INLINABLE toList #-}
101toList :: MonadAsync m => AsynclyT m a -> m [a]
106toList :: MonadAsync m => AsynclyT m a -> m [a]
102−toList (AsynclyT m) = liftIO (getContext Nothing) >>= run m
108+ ctx <- liftIO (getContext Nothing)
106 stop = return Stop
113 stop = return Stop
107 done a = return (Done a)
114 done a = return (Done a)
108− yield a x = return $ Yield a x
115+ yield a c x = return $ Yield a c x
110− run ma ctx = do
117+ run ctx ma = do
111− (res, ctx') <- runStateT
118+ res <- (runAsyncT ma) ctx stop (\a c r ->
112− ((runAsyncT ma) stop (\a r -> maybe (done a) (\x -> yield a x) r))
119+ maybe (done a) (\x -> yield a c x) r)
114 case res of
120 case res of
115− Yield x mb -> liftM (x :) (run mb ctx')
121+ Yield x c mb -> liftM (x :) (run c mb)
116 Stop -> return []
122 Stop -> return []
117 Done x -> return (x : [])
123 Done x -> return (x : [])
@@ -128,8 +134,8 @@ each xs = foldr (<|>) empty $ map return xs
128-- tasks start synchronously in the current thread unless overridden by
134-- tasks start synchronously in the current thread unless overridden by
129-- 'async'.
135-- 'async'.
130threads :: MonadAsync m => Int -> AsynclyT m a -> AsynclyT m a
136threads :: MonadAsync m => Int -> AsynclyT m a -> AsynclyT m a
131−threads n action = AsynclyT $ AsyncT $ \stp yld ->
137+threads n action = AsyncT $ \ctx stp yld ->
132− threadCtl n ((runAsyncT $ runAsynclyT action) stp yld)
138+ (runAsyncT action) ctx stp yld
134------------------------------------------------------------------------------
140------------------------------------------------------------------------------
135-- Logging
141-- Logging