@@ -149,15 +149,17 @@ import Control.Monad ((>=>), when)
149149import Control.Monad.Catch (MonadCatch)
150150import Control.Monad.IO.Class (MonadIO(..))
151151import Control.Monad.Trans.Control (MonadBaseControl, liftBaseOp_)
152+import Data.IORef (newIORef, readIORef, mkWeakIORef, writeIORef)
152153import Data.Maybe (isNothing)
153154import Data.Void (Void)
154−import Data.IORef (newIORef, readIORef, mkWeakIORef, writeIORef)
155−import GHC.Types (SPEC(..))
156155import Fusion.Plugin.Types (Fuse(..))
157−import Streamly.Internal.Data.Stream.StreamD.Type (Stream(..), Step(..))
158−import Streamly.Internal.Data.Unfold.Types (Unfold(..))
156+import GHC.Types (SPEC(..))
159157import Streamly.Internal.Data.Fold.Types (Fold(..))
158+import Streamly.Internal.Data.IOFinalizer
159+ (newIOFinalizer, runIOFinalizer, clearingIOFinalizer)
160+import Streamly.Internal.Data.Stream.StreamD.Type (Stream(..), Step(..))
160161import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime)
162+import Streamly.Internal.Data.Unfold.Types (Unfold(..))
161163import System.Mem (performMajorGC)
162164
163165import qualified Prelude
@@ -166,8 +168,7 @@ import qualified Data.Tuple as Tuple
166168import qualified Streamly.Internal.Data.Fold.Types as FL
167169import qualified Streamly.Internal.Data.Stream.StreamK as K (uncons)
168170import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
169−import qualified Streamly.Internal.Data.Stream.StreamD as D
170− (unfold, newFinalizedIORef, runIORefFinalizer, withIORefFinalizer)
171+import qualified Streamly.Internal.Data.Stream.StreamD as D (unfold)
171172
172173import Streamly.Internal.Data.SVar
173174import Prelude
@@ -1154,7 +1155,7 @@ gbracket bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
11541155 -- the registration of 'aft' atomic. See comment in 'D.gbracketIO'.
11551156 (r, ref) <- liftBaseOp_ mask_ $ do
11561157 r <- bef x
1157− ref <- D.newFinalizedIORef (aft r)
1158+ ref <- newIOFinalizer (aft r)
11581159 return (r, ref)
11591160 s <- inject1 r
11601161 return $ Right (s, r, ref)
@@ -1167,7 +1168,7 @@ gbracket bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
11671168 Yield x s -> return $ Yield x (Right (s, v, ref))
11681169 Skip s -> return $ Skip (Right (s, v, ref))
11691170 Stop -> do
1170− D.runIORefFinalizer ref
1171+ runIOFinalizer ref
11711172 return Stop
11721173 -- XXX Do not handle async exceptions, just rethrow them.
11731174 Left e -> do
@@ -1175,7 +1176,7 @@ gbracket bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
11751176 -- be atomic wrt async exceptions. Otherwise if we have cleared
11761177 -- the finalizer and have not run the exception handler then we
11771178 -- may leak the resource.
1178− r <- D.withIORefFinalizer ref (einject (v, e))
1179+ r <- clearingIOFinalizer ref (einject (v, e))
11791180 return $ Skip (Left r)
11801181 step (Left st) = do
11811182 res <- estep st
@@ -1263,7 +1264,7 @@ after action (Unfold step1 inject1) = Unfold step inject
12631264
12641265 inject x = do
12651266 s <- inject1 x
1266− ref <- D.newFinalizedIORef (action x)
1267+ ref <- newIOFinalizer (action x)
12671268 return (s, ref)
12681269
12691270 {-# INLINE_LATE step #-}
@@ -1273,7 +1274,7 @@ after action (Unfold step1 inject1) = Unfold step inject
12731274 Yield x s -> return $ Yield x (s, ref)
12741275 Skip s -> return $ Skip (s, ref)
12751276 Stop -> do
1276− D.runIORefFinalizer ref
1277+ runIOFinalizer ref
12771278 return Stop
12781279
12791280{-# INLINE_NORMAL _onException #-}
@@ -1365,17 +1366,17 @@ finally action (Unfold step1 inject1) = Unfold step inject
13651366
13661367 inject x = do
13671368 s <- inject1 x
1368− ref <- D.newFinalizedIORef (action x)
1369+ ref <- newIOFinalizer (action x)
13691370 return (s, ref)
13701371
13711372 {-# INLINE_LATE step #-}
13721373 step (st, ref) = do
1373− res <- step1 st `MC.onException` D.runIORefFinalizer ref
1374+ res <- step1 st `MC.onException` runIOFinalizer ref
13741375 case res of
13751376 Yield x s -> return $ Yield x (s, ref)
13761377 Skip s -> return $ Skip (s, ref)
13771378 Stop -> do
1378− D.runIORefFinalizer ref
1379+ runIOFinalizer ref
13791380 return Stop
13801381
13811382{-# INLINE_NORMAL _bracket #-}
@@ -1449,19 +1450,19 @@ bracket bef aft (Unfold step1 inject1) = Unfold step inject
14491450 -- the registration of 'aft' atomic. See comment in 'D.gbracketIO'.
14501451 (r, ref) <- liftBaseOp_ mask_ $ do
14511452 r <- bef x
1452− ref <- D.newFinalizedIORef (aft r)
1453+ ref <- newIOFinalizer (aft r)
14531454 return (r, ref)
14541455 s <- inject1 r
14551456 return (s, ref)
14561457
14571458 {-# INLINE_LATE step #-}
14581459 step (st, ref) = do
1459− res <- step1 st `MC.onException` D.runIORefFinalizer ref
1460+ res <- step1 st `MC.onException` runIOFinalizer ref
14601461 case res of
14611462 Yield x s -> return $ Yield x (s, ref)
14621463 Skip s -> return $ Skip (s, ref)
14631464 Stop -> do
1464− D.runIORefFinalizer ref
1465+ runIOFinalizer ref
14651466 return Stop
14661467
14671468-- | When unfolding @Unfold m a b@ if an exception @e@ occurs, unfold @e@ using