2-- Module : Streamly.Internal.Data.Unfold.Exception
3-- Copyright : (c) 2019 Composewell Technologies
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
9-- Lifted resource management primitives.
11module Streamly.Internal.Data.Unfold.Exception
22import Control.Exception (mask_)
23import Control.Monad.Catch (MonadCatch)
24import Streamly.Internal.Control.Concurrent
25 (MonadRunInIO, MonadAsync, withRunInIO)
26import Streamly.Internal.Data.IOFinalizer.Lifted
27 (newIOFinalizer, runIOFinalizer, clearingIOFinalizer)
28import Streamly.Internal.Data.Stream.StreamD.Type (Step(..))
30import qualified Control.Monad.Catch as MC
32import Streamly.Internal.Data.Unfold.Type
34-- | Run the alloc action @a -> m c@ with async exceptions disabled but keeping
35-- blocking operations interruptible (see 'Control.Exception.mask'). Use the
36-- output @c@ as input to @Unfold m c b@ to generate an output stream. When
37-- unfolding use the supplied @try@ operation @forall s. m s -> m (Either e s)@
38-- to catch synchronous exceptions. If an exception occurs run the exception
39-- handling unfold @Unfold m (c, e) b@.
41-- The cleanup action @c -> m d@, runs whenever the stream ends normally, due
42-- to a sync or async exception or if it gets garbage collected after a partial
43-- lazy evaluation. See 'bracket' for the semantics of the cleanup action.
45-- 'gbracket' can express all other exception handling combinators.
47-- /Inhibits stream fusion/
50{-# INLINE_NORMAL gbracket #-}
53 => (a -> m c) -- ^ before
54 -> (c -> m d) -- ^ after, on normal stop, or GC
55 -> Unfold m (c, e) b -- ^ on exception
56 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
57 -> Unfold m c b -- ^ unfold to run
59gbracket bef aft (Unfold estep einject) ftry (Unfold step1 inject1) =
65 -- Mask asynchronous exceptions to make the execution of 'bef' and
66 -- the registration of 'aft' atomic. See comment in 'D.gbracketIO'.
67 (r, ref) <- withRunInIO $ \run -> mask_ $ run $ do
69 ref <- newIOFinalizer (aft r)
72 return $ Right (s, r, ref)
74 {-# INLINE_LATE step #-}
75 step (Right (st, v, ref)) = do
76 res <- ftry $ step1 st
79 Yield x s -> return $ Yield x (Right (s, v, ref))
80 Skip s -> return $ Skip (Right (s, v, ref))
84 -- XXX Do not handle async exceptions, just rethrow them.
86 -- Clearing of finalizer and running of exception handler must
87 -- be atomic wrt async exceptions. Otherwise if we have cleared
88 -- the finalizer and have not run the exception handler then we
89 -- may leak the resource.
90 r <- clearingIOFinalizer ref (einject (v, e))
91 return $ Skip (Left r)
95 Yield x s -> Yield x (Left s)
96 Skip s -> Skip (Left s)
99-- | Unfold the input @a@ using @Unfold m a b@, run an action on @a@ whenever
100-- the unfold stops normally, or if it is garbage collected after a partial
103-- The semantics of the action @a -> m c@ are similar to the cleanup action
104-- semantics in 'bracket'.
106-- /See also 'after_'/
109{-# INLINE_NORMAL after #-}
110after :: MonadRunInIO m
111 => (a -> m c) -> Unfold m a b -> Unfold m a b
112after action (Unfold step1 inject1) = Unfold step inject
118 ref <- newIOFinalizer (action x)
121 {-# INLINE_LATE step #-}
125 Yield x s -> return $ Yield x (s, ref)
126 Skip s -> return $ Skip (s, ref)
131-- | Unfold the input @a@ using @Unfold m a b@, run an action on @a@ whenever
132-- the unfold stops normally, aborts due to an exception or if it is garbage
133-- collected after a partial lazy evaluation.
135-- The semantics of the action @a -> m c@ are similar to the cleanup action
136-- semantics in 'bracket'.
139-- finally release = bracket return release
142-- /See also 'finally_'/
144-- /Inhibits stream fusion/
147{-# INLINE_NORMAL finally #-}
148finally :: (MonadAsync m, MonadCatch m)
149 => (a -> m c) -> Unfold m a b -> Unfold m a b
150finally action (Unfold step1 inject1) = Unfold step inject
156 ref <- newIOFinalizer (action x)
159 {-# INLINE_LATE step #-}
161 res <- step1 st `MC.onException` runIOFinalizer ref
163 Yield x s -> return $ Yield x (s, ref)
164 Skip s -> return $ Skip (s, ref)
169-- | Run the alloc action @a -> m c@ with async exceptions disabled but keeping
170-- blocking operations interruptible (see 'Control.Exception.mask'). Use the
171-- output @c@ as input to @Unfold m c b@ to generate an output stream.
173-- @c@ is usually a resource under the state of monad @m@, e.g. a file
174-- handle, that requires a cleanup after use. The cleanup action @c -> m d@,
175-- runs whenever the stream ends normally, due to a sync or async exception or
176-- if it gets garbage collected after a partial lazy evaluation.
178-- 'bracket' only guarantees that the cleanup action runs, and it runs with
179-- async exceptions enabled. The action must ensure that it can successfully
180-- cleanup the resource in the face of sync or async exceptions.
182-- When the stream ends normally or on a sync exception, cleanup action runs
183-- immediately in the current thread context, whereas in other cases it runs in
184-- the GC context, therefore, cleanup may be delayed until the GC gets to run.
186-- /See also: 'bracket_', 'gbracket'/
188-- /Inhibits stream fusion/
191{-# INLINE_NORMAL bracket #-}
192bracket :: (MonadAsync m, MonadCatch m)
193 => (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
194bracket bef aft (Unfold step1 inject1) = Unfold step inject
199 -- Mask asynchronous exceptions to make the execution of 'bef' and
200 -- the registration of 'aft' atomic. See comment in 'D.gbracketIO'.
201 (r, ref) <- withRunInIO $ \run -> mask_ $ run $ do
203 ref <- newIOFinalizer (aft r)
208 {-# INLINE_LATE step #-}
210 res <- step1 st `MC.onException` runIOFinalizer ref
212 Yield x s -> return $ Yield x (s, ref)
213 Skip s -> return $ Skip (s, ref)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list