Skip to content

Commit 2fe3867

Browse files
author
Alberto
committed
Cleaning. fix bug onFinish wth exceptions
1 parent 53a0a0d commit 2fe3867

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

transient/src/Transient/Internals.hs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ rindent= unsafePerformIO $ newIORef 0
7676
-- tr x= return () !> unsafePerformIO (printColor x)
7777
-- tr x= trace (show(unsafePerformIO myThreadId, unsafePerformIO $ printColor x)) $ return()
7878

79+
-- {-# NOINLINE tr #-}
7980
tr x= trace (printColor x) $ return ()
8081

82+
-- {-# NOINLINE printColor #-}
8183
printColor :: Show a => a -> String
8284
printColor x= unsafePerformIO $ do
8385
th <- myThreadId
@@ -96,7 +98,7 @@ printColor x= unsafePerformIO $ do
9698
-- in toHex q ++ (show $ (if r < 9 then toEnum( fromEnum '0' + r) else toEnum(fromEnum 'A'+ r -10):: Int))
9799

98100
ttr ::(Show a, MonadIO m) => a -> m()
99-
ttr x= liftIO $ putStrLn $ printColor x
101+
ttr x= liftIO $ do putStr "=======>" ; putStrLn $ printColor x
100102

101103
type StateIO = StateT EventF IO
102104

@@ -171,16 +173,7 @@ instance MonadState EventF TransIO where
171173
noTrans :: StateIO x -> TransIO x
172174
noTrans x = Transient $ x >>= return . Just
173175

174-
-- | filters away the Nothing responses of the State monad.
175-
-- in principle the state monad should return a single response, but, for performance reasons,
176-
-- it can run inside elements of transient monad (using `runTrans`) which may produce
177-
-- many results
178-
liftTrans :: StateIO (Maybe b) -> TransIO b
179-
liftTrans mx= do
180-
r <- noTrans mx
181-
case r of
182-
Nothing -> empty
183-
Just x -> return x
176+
184177

185178
-- emptyEventF :: ThreadId -> IORef (LifeCycle, BS.ByteString) -> MVar [EventF] -> EventF
186179
emptyEventF th label par childs =
@@ -1236,8 +1229,8 @@ sandbox mx = do
12361229
-- | executes a computation and restores the concrete state data. Default state is assigned if there isn't any before execution
12371230
sandboxData :: Typeable a => a -> TransIO b -> TransIO b
12381231
sandboxData def w= do
1239-
d <- getData `onNothing` return def
1240-
w <*** setData d
1232+
d <- getState <|> return def
1233+
w <*** setState d
12411234

12421235

12431236

@@ -1453,7 +1446,7 @@ loop parentc rec = forkMaybe False parentc $ \cont -> do
14531446
if dofork
14541447
then forkIt rparent proc
14551448
else ( proc parent >>= \(_,cont) -> exceptBackg cont $ Finish $ show (unsafePerformIO myThreadId,"loop thread ended"))
1456-
`catch` \e ->do exceptBack parent e
1449+
`catch` \e -> exceptBack parent e
14571450

14581451

14591452
forkIt rparentState proc= do
@@ -1781,8 +1774,9 @@ undoCut = backCut ()
17811774
{-# NOINLINE onBack #-}
17821775
onBack :: (Typeable b, Show b) => TransientIO a -> ( b -> TransientIO a) -> TransientIO a
17831776
onBack ac bac = registerBack (typeof bac) $ Transient $ do
1784-
-- tr "onBack"
1777+
-- ttr "onBack"
17851778
Backtrack mreason stack <- getData `onNothing` (return $ backStateOf (typeof bac))
1779+
-- ttr ("onBackstack",mreason, length stack)
17861780
runTrans $ case mreason of
17871781
Nothing -> ac -- !> "ONBACK NOTHING"
17881782
Just reason -> bac reason -- !> ("ONBACK JUST",reason)
@@ -1826,8 +1820,6 @@ registerBack witness f = Transient $ do
18261820
--addr x = liftIO $ return . hashStableName =<< (makeStableName $! x)
18271821

18281822

1829-
registerUndo :: TransientIO a -> TransientIO a
1830-
registerUndo f= registerBack () f
18311823

18321824
-- XXX Should we enforce retry of the same track which is being undone? If the
18331825
-- user specifies a different track would it make sense?
@@ -1859,7 +1851,7 @@ retry= forward ()
18591851
back :: (Typeable b, Show b) => b -> TransIO a
18601852
back reason = do
18611853
-- tr "back"
1862-
bs <- getData `onNothing` return (backStateOf reason)
1854+
bs@(Backtrack mreason stack) <- getData `onNothing` return (backStateOf reason)
18631855
goBackt bs -- !>"GOBACK"
18641856

18651857
where
@@ -1872,8 +1864,9 @@ back reason = do
18721864
goBackt (Backtrack _ [] )= empty
18731865
goBackt (Backtrack b (stack@(first : bs)) )= do
18741866
setData $ Backtrack (Just reason) bs --stack
1875-
x <- runClosure first -- !> ("RUNCLOSURE",length stack)
1867+
x <- runClosure first -- <|> ttr "EMPTY" -- !> ("RUNCLOSURE",length stack)
18761868
Backtrack back bs' <- getData `onNothing` return (backStateOf reason)
1869+
-- ttr ("goBackt",back, length bs')
18771870

18781871
case back of
18791872
Nothing -> do
@@ -1933,6 +1926,7 @@ onFinish exc= do
19331926
cont <- getCont
19341927
onFinishCont cont (return()) exc
19351928

1929+
19361930
-- | A binary onFinish which will return a result when all the threads of the first operand terminates so it can return a result.
19371931
-- the first parameter is the action and the second the action to do when all is done.
19381932
--
@@ -1969,7 +1963,8 @@ onFinishCont cont proc mx= do
19691963
if f then backtrack else
19701964
mx reason
19711965
-- anyThreads abduce -- necessary for controlling threads 0
1972-
1966+
-- to protect the stack of finish. The default exeception mechanism don't know about state
1967+
onException $ \(e::SomeException) -> throwt e
19731968
return r
19741969
where
19751970
isDead st= liftIO $ do
@@ -2106,7 +2101,7 @@ tmask proc= Transient $ do
21062101
-- If you want to manage exceptions only in the first argument and have the semantics of `catch` in transient, use `catcht`
21072102
onException' :: Exception e => TransIO a -> (e -> TransIO a) -> TransIO a
21082103
onException' mx f= onAnyException mx $ \e -> do
2109-
--return () !> "EXCEPTION HANDLER EXEC"
2104+
-- tr "EXCEPTION HANDLER EXEC"
21102105
case fromException e of
21112106
Nothing -> do
21122107
-- Backtrack r stack <- getData `onNothing` return (backStateOf e)

0 commit comments

Comments
 (0)