@@ -81,7 +81,7 @@ module Streamly.Internal.Data.Stream.StreamD
81 , fromListM
81 , fromListM
82 , fromStreamK
82 , fromStreamK
83 , fromStreamD
83 , fromStreamD
84− , fromPrimVar
84+ , fromPrimIORef
85 , fromSVar
85 , fromSVar
87 -- * Elimination
87 -- * Elimination
@@ -325,8 +325,7 @@ import Foreign.Storable (Storable(..))
325import GHC.Types (SPEC(..))
325import GHC.Types (SPEC(..))
326import System.Mem (performMajorGC)
326import System.Mem (performMajorGC)
327import Fusion.Plugin.Types (Fuse(..))
327import Fusion.Plugin.Types (Fuse(..))
328−import Streamly.Internal.Mutable.Prim.Var
328+import Streamly.Internal.Data.IORef.Prim (Prim)
329− (Prim, Var, readVar, newVar, modifyVar')
330import Streamly.Internal.Data.Time.Units
329import Streamly.Internal.Data.Time.Units
331 (TimeUnit64, toRelTime64, diffAbsTime64, RelTime64)
330 (TimeUnit64, toRelTime64, diffAbsTime64, RelTime64)
332import Streamly.Internal.Data.Atomics (atomicModifyIORefCAS_)
331import Streamly.Internal.Data.Atomics (atomicModifyIORefCAS_)
@@ -341,6 +340,7 @@ import Streamly.Internal.Data.Unfold.Types (Unfold(..))
341import Streamly.Internal.Data.Tuple.Strict (Tuple3'(..))
340import Streamly.Internal.Data.Tuple.Strict (Tuple3'(..))
342import Streamly.Internal.Data.Stream.SVar (fromConsumer, pushToFold)
341import Streamly.Internal.Data.Stream.SVar (fromConsumer, pushToFold)
343+import qualified Streamly.Internal.Data.IORef.Prim as Prim
344import qualified Streamly.Internal.Data.Pipe.Types as Pipe
344import qualified Streamly.Internal.Data.Pipe.Types as Pipe
345import qualified Streamly.Internal.Memory.Array.Types as A
345import qualified Streamly.Internal.Memory.Array.Types as A
346import qualified Streamly.Internal.Memory.Mutable.Array.Types as MA
346import qualified Streamly.Internal.Memory.Mutable.Array.Types as MA
@@ -677,12 +677,12 @@ fromListM = Stream step
677toStreamD :: (K.IsStream t, Monad m) => t m a -> Stream m a
677toStreamD :: (K.IsStream t, Monad m) => t m a -> Stream m a
678toStreamD = fromStreamK . K.toStream
678toStreamD = fromStreamK . K.toStream
680−{-# INLINE_NORMAL fromPrimVar #-}
680+{-# INLINE_NORMAL fromPrimIORef #-}
681−fromPrimVar :: (MonadIO m, Prim a) => Var IO a -> Stream m a
681+fromPrimIORef :: (MonadIO m, Prim a) => Prim.IORef a -> Stream m a
682−fromPrimVar var = Stream step ()
682+fromPrimIORef var = Stream step ()
684 {-# INLINE_LATE step #-}
684 {-# INLINE_LATE step #-}
685− step _ () = liftIO (readVar var) >>= \x -> return $ Yield x ()
685+ step _ () = liftIO (Prim.readIORef var) >>= \x -> return $ Yield x ()
687-------------------------------------------------------------------------------
687-------------------------------------------------------------------------------
688-- Generation from SVar
688-- Generation from SVar
@@ -3811,17 +3811,17 @@ pollCounts predicate transf fld (Stream step state) = Stream step' Nothing
3811 -- As long as we are using an "Int" for counts lockfree reads from
3811 -- As long as we are using an "Int" for counts lockfree reads from
3812 -- Var should work correctly on both 32-bit and 64-bit machines.
3812 -- Var should work correctly on both 32-bit and 64-bit machines.
3813 -- However, an Int on a 32-bit machine may overflow quickly.
3813 -- However, an Int on a 32-bit machine may overflow quickly.
3814− countVar <- liftIO $ newVar (0 :: Int)
3814+ countVar <- liftIO $ Prim.newIORef (0 :: Int)
3815 tid <- forkManaged
3815 tid <- forkManaged
3816 $ void $ runFold fld
3816 $ void $ runFold fld
3817− $ transf $ fromPrimVar countVar
3817+ $ transf $ fromPrimIORef countVar
3818 return $ Skip (Just (countVar, tid, state))
3818 return $ Skip (Just (countVar, tid, state))
3820 step' gst (Just (countVar, tid, st)) = do
3820 step' gst (Just (countVar, tid, st)) = do
3821 r <- step gst st
3821 r <- step gst st
3822 case r of
3822 case r of
3823 Yield x s -> do
3823 Yield x s -> do
3824− when (predicate x) $ liftIO $ modifyVar' countVar (+ 1)
3824+ when (predicate x) $ liftIO $ Prim.modifyIORef' countVar (+ 1)
3825 return $ Yield x (Just (countVar, tid, s))
3825 return $ Yield x (Just (countVar, tid, s))
3826 Skip s -> return $ Skip (Just (countVar, tid, s))
3826 Skip s -> return $ Skip (Just (countVar, tid, s))
3827 Stop -> do
3827 Stop -> do
@@ -3842,12 +3842,12 @@ tapRate samplingRate action (Stream step state) = Stream step' Nothing
3843 MC.catch
3843 MC.catch
3844 (do liftIO $ threadDelay (round $ samplingRate * 1000000)
3844 (do liftIO $ threadDelay (round $ samplingRate * 1000000)
3845− i <- liftIO $ readVar countVar
3845+ i <- liftIO $ Prim.readIORef countVar
3846 let !diff = i - prev
3846 let !diff = i - prev
3847 void $ action diff
3847 void $ action diff
3848 return i)
3848 return i)
3849 (\(e :: AsyncException) -> do
3849 (\(e :: AsyncException) -> do
3850− i <- liftIO $ readVar countVar
3850+ i <- liftIO $ Prim.readIORef countVar
3851 let !diff = i - prev
3851 let !diff = i - prev
3852 void $ action diff
3852 void $ action diff
3853 throwM (MC.toException e))
3853 throwM (MC.toException e))
@@ -3855,7 +3855,7 @@ tapRate samplingRate action (Stream step state) = Stream step' Nothing
3856 {-# INLINE_LATE step' #-}
3856 {-# INLINE_LATE step' #-}
3857 step' _ Nothing = do
3857 step' _ Nothing = do
3858− countVar <- liftIO $ newVar 0
3858+ countVar <- liftIO $ Prim.newIORef 0
3859 tid <- fork $ loop countVar 0
3859 tid <- fork $ loop countVar 0
3860 ref <- liftIO $ newIORef ()
3860 ref <- liftIO $ newIORef ()
3861 _ <- liftIO $ mkWeakIORef ref (killThread tid)
3861 _ <- liftIO $ mkWeakIORef ref (killThread tid)
@@ -3865,7 +3865,7 @@ tapRate samplingRate action (Stream step state) = Stream step' Nothing
3865 r <- step gst st
3865 r <- step gst st
3866 case r of
3866 case r of
3867 Yield x s -> do
3867 Yield x s -> do
3868− liftIO $ modifyVar' countVar (+ 1)
3868+ liftIO $ Prim.modifyIORef' countVar (+ 1)
3869 return $ Yield x (Just (countVar, tid, s, ref))
3869 return $ Yield x (Just (countVar, tid, s, ref))
3870 Skip s -> return $ Skip (Just (countVar, tid, s, ref))
3870 Skip s -> return $ Skip (Just (countVar, tid, s, ref))
3871 Stop -> do
3871 Stop -> do
@@ -4628,13 +4628,13 @@ dropByTime duration (Stream step1 state1) = Stream step (DropByTimeInit state1)
4628 Stop -> Stop
4628 Stop -> Stop
4630{-# INLINE updateTimeVar #-}
4630{-# INLINE updateTimeVar #-}
4631−updateTimeVar :: Var IO Int64 -> IO ()
4631+updateTimeVar :: Prim.IORef Int64 -> IO ()
4632updateTimeVar timeVar = do
4632updateTimeVar timeVar = do
4633 MicroSecond64 t <- fromAbsTime <$> getTime Monotonic
4633 MicroSecond64 t <- fromAbsTime <$> getTime Monotonic
4634− modifyVar' timeVar (const t)
4634+ Prim.modifyIORef' timeVar (const t)
4636{-# INLINE updateWithDelay #-}
4636{-# INLINE updateWithDelay #-}
4637−updateWithDelay :: RealFrac a => a -> Var IO Int64 -> IO ()
4637+updateWithDelay :: RealFrac a => a -> Prim.IORef Int64 -> IO ()
4638updateWithDelay precision timeVar = do
4638updateWithDelay precision timeVar = do
4639 threadDelay (delayTime precision)
4639 threadDelay (delayTime precision)
4640 updateTimeVar timeVar
4640 updateTimeVar timeVar
@@ -4666,14 +4666,14 @@ times g = Stream step Nothing
4666 -- XXX note that this is safe only on a 64-bit machine. On a 32-bit
4666 -- XXX note that this is safe only on a 64-bit machine. On a 32-bit
4667 -- machine a 64-bit 'Var' cannot be read consistently without a lock
4667 -- machine a 64-bit 'Var' cannot be read consistently without a lock
4668 -- while another thread is writing to it.
4668 -- while another thread is writing to it.
4669− timeVar <- liftIO $ newVar (0 :: Int64)
4669+ timeVar <- liftIO $ Prim.newIORef (0 :: Int64)
4670 liftIO $ updateTimeVar timeVar
4670 liftIO $ updateTimeVar timeVar
4671 tid <- forkManaged $ liftIO $ forever (updateWithDelay g timeVar)
4671 tid <- forkManaged $ liftIO $ forever (updateWithDelay g timeVar)
4672− a <- liftIO $ readVar timeVar
4672+ a <- liftIO $ Prim.readIORef timeVar
4673 return $ Skip $ Just (timeVar, tid, a)
4673 return $ Skip $ Just (timeVar, tid, a)
4675 step _ s@(Just (timeVar, _, t0)) = do
4675 step _ s@(Just (timeVar, _, t0)) = do
4676− a <- liftIO $ readVar timeVar
4676+ a <- liftIO $ Prim.readIORef timeVar
4677 -- XXX we can perhaps use an AbsTime64 using a 64 bit Int for
4677 -- XXX we can perhaps use an AbsTime64 using a 64 bit Int for
4678 -- efficiency. or maybe we can use a representation using Double for
4678 -- efficiency. or maybe we can use a representation using Double for
4679 -- floating precision time
4679 -- floating precision time