1{-# LANGUAGE UnboxedTuples #-}
4-- Module : Streamly.Internal.Data.IORef.Prim
5-- Copyright : (c) 2019 Composewell Technologies
8-- Maintainer : streamly@composewell.com
9-- Stability : experimental
12-- A mutable variable in a mutation capable monad (IO) holding a 'Prim'
13-- value. This allows fast modification because of unboxed storage.
15-- = Multithread Consistency Notes
17-- In general, any value that straddles a machine word cannot be guaranteed to
18-- be consistently read from another thread without a lock. GHC heap objects
19-- are always machine word aligned, therefore, a 'IORef' is also word aligned.
20-- On a 64-bit platform, writing a 64-bit aligned type from one thread and
21-- reading it from another thread should give consistent old or new value. The
22-- same holds true for 32-bit values on a 32-bit platform.
24module Streamly.Internal.Data.IORef.Prim
45import Control.Monad.IO.Class (MonadIO(..))
46import Control.Monad.Primitive (primitive_)
47import Data.Primitive.Types (Prim, sizeOf#, readByteArray#, writeByteArray#)
48import GHC.Exts (MutableByteArray#, newByteArray#, RealWorld)
51import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
52import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
54-- | An 'IORef' holds a single 'Prim' value.
55data IORef a = IORef (MutableByteArray# RealWorld)
57-- | Create a new 'IORef'.
60{-# INLINE newIORef #-}
61newIORef :: forall a. Prim a => a -> IO (IORef a)
62newIORef x = IO (\s# ->
63 case newByteArray# (sizeOf# (undefined :: a)) s# of
65 case writeByteArray# arr# 0# x s1# of
66 s2# -> (# s2#, IORef arr# #)
69-- | Write a value to an 'IORef'.
72{-# INLINE writeIORef #-}
73writeIORef :: Prim a => IORef a -> a -> IO ()
74writeIORef (IORef arr#) x = primitive_ (writeByteArray# arr# 0# x)
76-- | Read a value from an 'IORef'.
79{-# INLINE readIORef #-}
80readIORef :: Prim a => IORef a -> IO a
81readIORef (IORef arr#) = IO (readByteArray# arr# 0#)
83-- | Modify the value of an 'IORef' using a function with strict application.
86{-# INLINE modifyIORef' #-}
87modifyIORef' :: Prim a => IORef a -> (a -> a) -> IO ()
88modifyIORef' (IORef arr#) g = primitive_ $ \s# ->
89 case readByteArray# arr# 0# s# of
90 (# s'#, a #) -> let a' = g a in a' `seq` writeByteArray# arr# 0# a' s'#
92-- | Generate a stream by continuously reading the IORef.
95{-# INLINE_NORMAL toStreamD #-}
96toStreamD :: (MonadIO m, Prim a) => IORef a -> D.Stream m a
97toStreamD var = D.Stream step ()
101 {-# INLINE_LATE step #-}
102 step _ () = liftIO (readIORef var) >>= \x -> return $ D.Yield x ()
104-- | Construct a stream by reading a 'Prim' 'IORef' repeatedly.
108{-# INLINE toStream #-}
109toStream :: (K.IsStream t, MonadIO m, Prim a) => IORef a -> t m a
110toStream = D.fromStreamD . toStreamD
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