@@ -81,7 +81,7 @@ module Streamly.Internal.Data.Stream.StreamD
8181 , fromListM
8282 , fromStreamK
8383 , fromStreamD
84− , fromPrimVar
84+ , fromPrimIORef
8585 , fromSVar
8686
8787 -- * Elimination
@@ -325,8 +325,7 @@ import Foreign.Storable (Storable(..))
325325import GHC.Types (SPEC(..))
326326import System.Mem (performMajorGC)
327327import Fusion.Plugin.Types (Fuse(..))
328−import Streamly.Internal.Mutable.Prim.Var
329− (Prim, Var, readVar, newVar, modifyVar')
328+import Streamly.Internal.Data.IORef.Prim (Prim)
330329import Streamly.Internal.Data.Time.Units
331330 (TimeUnit64, toRelTime64, diffAbsTime64, RelTime64)
332331import Streamly.Internal.Data.Atomics (atomicModifyIORefCAS_)
@@ -341,6 +340,7 @@ import Streamly.Internal.Data.Unfold.Types (Unfold(..))
341340import Streamly.Internal.Data.Tuple.Strict (Tuple3'(..))
342341import Streamly.Internal.Data.Stream.SVar (fromConsumer, pushToFold)
343342
343+import qualified Streamly.Internal.Data.IORef.Prim as Prim
344344import qualified Streamly.Internal.Data.Pipe.Types as Pipe
345345import qualified Streamly.Internal.Memory.Array.Types as A
346346import qualified Streamly.Internal.Memory.Mutable.Array.Types as MA
@@ -677,12 +677,12 @@ fromListM = Stream step
677677toStreamD :: (K.IsStream t, Monad m) => t m a -> Stream m a
678678toStreamD = fromStreamK . K.toStream
679679
680−{-# INLINE_NORMAL fromPrimVar #-}
681−fromPrimVar :: (MonadIO m, Prim a) => Var IO a -> Stream m a
682−fromPrimVar var = Stream step ()
680+{-# INLINE_NORMAL fromPrimIORef #-}
681+fromPrimIORef :: (MonadIO m, Prim a) => Prim.IORef a -> Stream m a
682+fromPrimIORef var = Stream step ()
683683 where
684684 {-# INLINE_LATE step #-}
685− step _ () = liftIO (readVar var) >>= \x -> return $ Yield x ()
685+ step _ () = liftIO (Prim.readIORef var) >>= \x -> return $ Yield x ()
686686
687687-------------------------------------------------------------------------------
688688-- Generation from SVar
@@ -3811,17 +3811,17 @@ pollCounts predicate transf fld (Stream step state) = Stream step' Nothing
38113811 -- As long as we are using an "Int" for counts lockfree reads from
38123812 -- Var should work correctly on both 32-bit and 64-bit machines.
38133813 -- However, an Int on a 32-bit machine may overflow quickly.
3814− countVar <- liftIO $ newVar (0 :: Int)
3814+ countVar <- liftIO $ Prim.newIORef (0 :: Int)
38153815 tid <- forkManaged
38163816 $ void $ runFold fld
3817− $ transf $ fromPrimVar countVar
3817+ $ transf $ fromPrimIORef countVar
38183818 return $ Skip (Just (countVar, tid, state))
38193819
38203820 step' gst (Just (countVar, tid, st)) = do
38213821 r <- step gst st
38223822 case r of
38233823 Yield x s -> do
3824− when (predicate x) $ liftIO $ modifyVar' countVar (+ 1)
3824+ when (predicate x) $ liftIO $ Prim.modifyIORef' countVar (+ 1)
38253825 return $ Yield x (Just (countVar, tid, s))
38263826 Skip s -> return $ Skip (Just (countVar, tid, s))
38273827 Stop -> do
@@ -3842,12 +3842,12 @@ tapRate samplingRate action (Stream step state) = Stream step' Nothing
38423842 i <-
38433843 MC.catch
38443844 (do liftIO $ threadDelay (round $ samplingRate * 1000000)
3845− i <- liftIO $ readVar countVar
3845+ i <- liftIO $ Prim.readIORef countVar
38463846 let !diff = i - prev
38473847 void $ action diff
38483848 return i)
38493849 (\(e :: AsyncException) -> do
3850− i <- liftIO $ readVar countVar
3850+ i <- liftIO $ Prim.readIORef countVar
38513851 let !diff = i - prev
38523852 void $ action diff
38533853 throwM (MC.toException e))
@@ -3855,7 +3855,7 @@ tapRate samplingRate action (Stream step state) = Stream step' Nothing
38553855
38563856 {-# INLINE_LATE step' #-}
38573857 step' _ Nothing = do
3858− countVar <- liftIO $ newVar 0
3858+ countVar <- liftIO $ Prim.newIORef 0
38593859 tid <- fork $ loop countVar 0
38603860 ref <- liftIO $ newIORef ()
38613861 _ <- liftIO $ mkWeakIORef ref (killThread tid)
@@ -3865,7 +3865,7 @@ tapRate samplingRate action (Stream step state) = Stream step' Nothing
38653865 r <- step gst st
38663866 case r of
38673867 Yield x s -> do
3868− liftIO $ modifyVar' countVar (+ 1)
3868+ liftIO $ Prim.modifyIORef' countVar (+ 1)
38693869 return $ Yield x (Just (countVar, tid, s, ref))
38703870 Skip s -> return $ Skip (Just (countVar, tid, s, ref))
38713871 Stop -> do
@@ -4628,13 +4628,13 @@ dropByTime duration (Stream step1 state1) = Stream step (DropByTimeInit state1)
46284628 Stop -> Stop
46294629
46304630{-# INLINE updateTimeVar #-}
4631−updateTimeVar :: Var IO Int64 -> IO ()
4631+updateTimeVar :: Prim.IORef Int64 -> IO ()
46324632updateTimeVar timeVar = do
46334633 MicroSecond64 t <- fromAbsTime <$> getTime Monotonic
4634− modifyVar' timeVar (const t)
4634+ Prim.modifyIORef' timeVar (const t)
46354635
46364636{-# INLINE updateWithDelay #-}
4637−updateWithDelay :: RealFrac a => a -> Var IO Int64 -> IO ()
4637+updateWithDelay :: RealFrac a => a -> Prim.IORef Int64 -> IO ()
46384638updateWithDelay precision timeVar = do
46394639 threadDelay (delayTime precision)
46404640 updateTimeVar timeVar
@@ -4666,14 +4666,14 @@ times g = Stream step Nothing
46664666 -- XXX note that this is safe only on a 64-bit machine. On a 32-bit
46674667 -- machine a 64-bit 'Var' cannot be read consistently without a lock
46684668 -- while another thread is writing to it.
4669− timeVar <- liftIO $ newVar (0 :: Int64)
4669+ timeVar <- liftIO $ Prim.newIORef (0 :: Int64)
46704670 liftIO $ updateTimeVar timeVar
46714671 tid <- forkManaged $ liftIO $ forever (updateWithDelay g timeVar)
4672− a <- liftIO $ readVar timeVar
4672+ a <- liftIO $ Prim.readIORef timeVar
46734673 return $ Skip $ Just (timeVar, tid, a)
46744674
46754675 step _ s@(Just (timeVar, _, t0)) = do
4676− a <- liftIO $ readVar timeVar
4676+ a <- liftIO $ Prim.readIORef timeVar
46774677 -- XXX we can perhaps use an AbsTime64 using a 64 bit Int for
46784678 -- efficiency. or maybe we can use a representation using Double for
46794679 -- floating precision time