on master help sign in
Make logs work with regular wait/gather APIs too
commitf455a06d0a44b78c90fde1d90f0a7aeb48a19d00 authorHarendra Kumar <harendra.kumar@gmail.com> dateThu 29 Jun 2017, 03:59 (9 years ago) parenta9d6c29
2 files changed+29−24
src/Strands/Threads.hs+20−22 test/logged.hs+9−2
src/Strands/Threads.hs+20−22
@@ -559,31 +559,29 @@ wait m = waitAsync (const mzero) Nothing m
559559logged :: (Loggable a, MonadIO m) => AsyncT m a -> AsyncT m a
560560logged m = AsyncT $ do
561561 ctx <- get
562− case logsRef ctx of
563− Nothing -> runAsyncT m
564− Just _ -> runLogged ctx (runAndLogResult m)
562+ case journal ctx of
563+ -- no replay
564+ CtxLog ls [] ->
565+ case logsRef ctx of
566+ Nothing -> runAsyncT m
567+ Just _ -> do
568+ put $ ctx {journal = CtxLog (Executing : ls) []}
569+ runAndLogResult m
570+
571+ -- replaying the log
572+ CtxLog ls (r:rs) -> do
573+ -- dbg $ "Replay: j: " ++ show j
574+ case r of
575+ Executing -> do
576+ put $ ctx {journal = CtxLog (r : ls) rs}
577+ runAndLogResult m
578+ Result val -> do
579+ let x = fmap read val
580+ put $ ctx {journal = CtxLog (r : ls) rs}
581+ return x
565582
566583 where
567584
568− runLogged ctx action =
569− case journal ctx of
570− -- no replay
571− CtxLog ls [] -> do
572− put $ ctx {journal = CtxLog (Executing : ls) []}
573− action
574−
575− -- replaying the log
576− CtxLog ls (r:rs) -> do
577− -- dbg $ "Replay: j: " ++ show j
578− case r of
579− Executing -> do
580− put $ ctx {journal = CtxLog (r : ls) rs}
581− action
582− Result val -> do
583− let x = fmap read val
584− put $ ctx {journal = CtxLog (r : ls) rs}
585− return x
586−
587585 runAndLogResult action = runAsyncT $ do
588586 x <- action
589587 logResult x
test/logged.hs+9−2
@@ -3,12 +3,19 @@ import Control.Monad.IO.Class (liftIO)
33
44main = do
55 logs <- waitLogged comp
6− -- XXX even if we use wait replay should still work, though new logs will
7− -- not be generated.
6+
7+ putStrLn $ "\nResuming with logs:"
88 logs1 <- waitLogged $ eachWithLog comp logs
9+
10+ putStrLn $ "\nResuming with logs:"
911 logs2 <- waitLogged $ eachWithLog comp logs1
12+
13+ putStrLn $ "\nLogs at the end must be empty:"
1014 putStrLn $ show logs2
1115
16+ putStrLn $ "\nRunning the last log again using wait:"
17+ wait $ eachWithLog comp logs1
18+
1219 where
1320 comp = logged $ threads 5 $ do
1421 r <- logged $ each [1..3]