@@ -17,7 +17,7 @@ module Asyncly.RunAsync
19 , AsynclyT
19 , AsynclyT
20− , threads
20+-- , threads
@@ -56,7 +56,7 @@ deriving instance MonadAsync m => Alternative (AsynclyT m)
56deriving instance MonadAsync m => Monad (AsynclyT m)
56deriving instance MonadAsync m => Monad (AsynclyT m)
57deriving instance MonadAsync m => MonadIO (AsynclyT m)
57deriving instance MonadAsync m => MonadIO (AsynclyT m)
58instance MonadTrans (AsynclyT) where
58instance MonadTrans (AsynclyT) where
59− lift mx = AsynclyT $ AsyncT $ lift mx >>= return . (\a -> (Yield a stop))
59+ lift mx = AsynclyT $ AsyncT $ \s k -> lift mx >>= (\a -> (k a Nothing))
61getContext :: Maybe (IORef [Recording]) -> IO Context
61getContext :: Maybe (IORef [Recording]) -> IO Context
62getContext lref = do
62getContext lref = do
@@ -69,31 +69,33 @@ getContext lref = do
69-- results.
69-- results.
70{-# SPECIALIZE wait_ :: AsynclyT IO a -> IO () #-}
70{-# SPECIALIZE wait_ :: AsynclyT IO a -> IO () #-}
71wait_ :: forall m a. (MonadAsync m, MonadCatch m) => AsynclyT m a -> m ()
71wait_ :: forall m a. (MonadAsync m, MonadCatch m) => AsynclyT m a -> m ()
72−wait_ (AsynclyT m) = do
72+wait_ (AsynclyT m) = do
73 ctx <- liftIO $ getContext Nothing
73 ctx <- liftIO $ getContext Nothing
74 _ <- runStateT (run m) ctx
74 _ <- runStateT (run m) ctx
79− run m = runAsyncT m >>= \x ->
79+ run m = (runAsyncT m) (return ())
80− case x of
80+ (\a r -> maybe (return ()) (\x -> run x) r)
82− -- XXX pull from children here
82+data Step a r = Stop | Done a | Yield a r
84+{-# SPECIALIZE wait :: AsynclyT IO a -> IO [a] #-}
85wait :: forall m a. (MonadAsync m, MonadCatch m) => AsynclyT m a -> m [a]
86wait :: forall m a. (MonadAsync m, MonadCatch m) => AsynclyT m a -> m [a]
86wait (AsynclyT m) = liftIO (getContext Nothing) >>= run m
87wait (AsynclyT m) = liftIO (getContext Nothing) >>= run m
90 run ma ctx = do
91 run ma ctx = do
91− (a, ctx') <- runStateT (runAsyncT ma) ctx
92+ (res, ctx') <- runStateT ((runAsyncT ma) (return Stop)
93+ (\a r -> maybe (return (Done a)) (\x -> return $ Yield a x) r)) ctx
92− case a of
94+ case res of
93 Yield x mb -> liftM (x :) (run mb ctx')
95 Yield x mb -> liftM (x :) (run mb ctx')
94 -- XXX pull from children here
96 -- XXX pull from children here
95 Stop -> return []
97 Stop -> return []
96−
98+ Done x -> return (x : [])
98-- scatter
100-- scatter
99{-# SPECIALIZE each :: [a] -> AsynclyT IO a #-}
101{-# SPECIALIZE each :: [a] -> AsynclyT IO a #-}
@@ -108,8 +110,8 @@ each xs = foldr (<|>) empty $ map return xs
108-- | Runs a computation under a given thread limit. A limit of 0 means all new
110-- | Runs a computation under a given thread limit. A limit of 0 means all new
109-- tasks start synchronously in the current thread unless overridden by
111-- tasks start synchronously in the current thread unless overridden by
110-- 'async'.
112-- 'async'.
111−threads :: MonadAsync m => Int -> AsynclyT m a -> AsynclyT m a
113+-- threads :: MonadAsync m => Int -> AsynclyT m a -> AsynclyT m a
112−threads n action = AsynclyT $ AsyncT $ threadCtl n (runAsyncT $ runAsynclyT action)
114+-- threads n action = AsynclyT $ AsyncT $ threadCtl n (runAsyncT $ runAsynclyT action)
115wait_ :: AsyncT IO a -> IO ()
117wait_ :: AsyncT IO a -> IO ()