Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions core/src/Streamly/Internal/Data/IORef.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ module Streamly.Internal.Data.IORef

-- Read
, readIORef
, pollGenericIORef
, pollIORefInt

-- Deprecated
, pollIntIORef
)
where

#include "inline.hs"
#include "deprecation.h"

import Control.Monad.IO.Class (MonadIO(..))
#if __GLASGOW_HASKELL__ >= 810
Expand Down Expand Up @@ -88,20 +93,30 @@ modifyIORef' var g = do
x <- readIORef var
writeIORef var (g x)

-- | Generate a stream by continuously reading the IORef.
--
-- This operation reads the IORef without any synchronization. It can be
-- assumed to be atomic because the IORef (MutableByteArray) is always aligned
-- to Int boundaries, we are assuming that compiler uses single instructions to
-- access the memory. It may read stale values though until caches are
-- synchronised in a multiprocessor architecture.
--
-- /Pre-release/
{-# INLINE_NORMAL pollIntIORef #-}
pollIntIORef :: (MonadIO m, Unbox a) => IORef a -> D.Stream m a
pollIntIORef var = D.Stream step ()
-- | Internal, do not use.
{-# INLINE_NORMAL pollGenericIORef #-}
pollGenericIORef :: (MonadIO m, Unbox a) => IORef a -> D.Stream m a
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a generic version? Just the Int version should be enough. Any acceptable generic value should be coerced to Int.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To suppress the deprecation warning in the MutArray module. This definition is being used there.

pollGenericIORef var = D.Stream step ()

where

{-# INLINE_LATE step #-}
step _ () = liftIO (readIORef var) >>= \x -> return $ D.Yield x ()

{-# DEPRECATED pollIntIORef "Use pollIORefInt instead." #-}
{-# INLINE_NORMAL pollIntIORef #-}
pollIntIORef :: (MonadIO m, Unbox a) => IORef a -> D.Stream m a
pollIntIORef = pollGenericIORef

-- | Generate a stream by continuously reading the IORef.
--
-- This operation reads the IORef without any synchronization. It can be
-- assumed to be atomic because the size fits into machine register size. We
-- are assuming that compiler uses single instructions to access the memory. It
-- may read stale values though until caches are synchronised in a
-- multiprocessor architecture.
--
-- /Pre-release/
{-# INLINE_NORMAL pollIORefInt #-}
pollIORefInt :: MonadIO m => IORef Int -> D.Stream m Int
pollIORefInt = pollGenericIORef
2 changes: 1 addition & 1 deletion core/src/Streamly/Internal/Data/MutArray.hs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ type IORef = IORef.IORef

{-# DEPRECATED pollIntIORef "Use pollIntIORef from MutByteArray module." #-}
pollIntIORef :: (MonadIO m, Unbox a) => IORef a -> Stream m a
pollIntIORef = IORef.pollIntIORef
pollIntIORef = IORef.pollGenericIORef

{-# DEPRECATED newIORef "Use newIORef from MutByteArray module." #-}
newIORef :: forall a. Unbox a => a -> IO (IORef a)
Expand Down
2 changes: 1 addition & 1 deletion src/Streamly/Internal/Data/Stream/Concurrent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ parTapCount predicate fld (D.Stream step state) = D.Stream step' Nothing
countVar <- liftIO $ Unboxed.newIORef (0 :: Int)
tid <- forkManaged
$ void $ fld
$ Unboxed.pollIntIORef countVar
$ Unboxed.pollIORefInt countVar
return $ Skip (Just (countVar, tid, state))

step' gst (Just (countVar, tid, st)) = do
Expand Down
Loading