Skip to content

Commit

Permalink
Add a tryReadTBQueue to MonadSTM
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoutts committed Feb 11, 2019
1 parent 4bb3720 commit c153eb7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
16 changes: 12 additions & 4 deletions io-sim-classes/src/Control/Monad/Class/MonadSTM.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Control.Monad.Class.MonadSTM
, TBQueueDefault (..)
, newTBQueueDefault
, readTBQueueDefault
, tryReadTBQueueDefault
, writeTBQueueDefault
) where

Expand Down Expand Up @@ -87,6 +88,7 @@ class (MonadFork m, Monad (Tr m)) => MonadSTM m where
type TBQueue m :: * -> *
newTBQueue :: Natural -> Tr m (TBQueue m a)
readTBQueue :: TBQueue m a -> Tr m a
tryReadTBQueue :: TBQueue m a -> Tr m (Maybe a)
writeTBQueue :: TBQueue m a -> a -> Tr m ()

instance MonadSTM m => MonadSTM (ReaderT e m) where
Expand Down Expand Up @@ -116,6 +118,7 @@ instance MonadSTM m => MonadSTM (ReaderT e m) where

newTBQueue = lift . newTBQueue
readTBQueue = lift . readTBQueue
tryReadTBQueue = lift . tryReadTBQueue
writeTBQueue q a = lift $ writeTBQueue q a

instance (Show e, MonadSTM m) => MonadSTM (ExceptT e m) where
Expand Down Expand Up @@ -145,6 +148,7 @@ instance (Show e, MonadSTM m) => MonadSTM (ExceptT e m) where

newTBQueue = lift . newTBQueue
readTBQueue = lift . readTBQueue
tryReadTBQueue = lift . tryReadTBQueue
writeTBQueue q a = lift $ writeTBQueue q a

-- | Wrapper around 'BlockedIndefinitelyOnSTM' that stores a call stack
Expand Down Expand Up @@ -206,6 +210,7 @@ instance MonadSTM IO where
newTBQueue = STM.newTBQueue . fromEnum
#endif
readTBQueue = STM.readTBQueue
tryReadTBQueue = STM.tryReadTBQueue
writeTBQueue = STM.writeTBQueue

--
Expand Down Expand Up @@ -302,24 +307,27 @@ newTBQueueDefault size = do
return (TBQueue rsize read wsize write size)

readTBQueueDefault :: MonadSTM m => TBQueueDefault m a -> Tr m a
readTBQueueDefault (TBQueue rsize read _wsize write _size) = do
readTBQueueDefault queue = maybe retry return =<< tryReadTBQueueDefault queue

tryReadTBQueueDefault :: MonadSTM m => TBQueueDefault m a -> Tr m (Maybe a)
tryReadTBQueueDefault (TBQueue rsize read _wsize write _size) = do
xs <- readTVar read
r <- readTVar rsize
writeTVar rsize $! r + 1
case xs of
(x:xs') -> do
writeTVar read xs'
return x
return (Just x)
[] -> do
ys <- readTVar write
case ys of
[] -> retry
[] -> return Nothing
_ -> do
let (z:zs) = reverse ys -- NB. lazy: we want the transaction to be
-- short, otherwise it will conflict
writeTVar write []
writeTVar read zs
return z
return (Just z)

writeTBQueueDefault :: MonadSTM m => TBQueueDefault m a -> a -> Tr m ()
writeTBQueueDefault (TBQueue rsize _read wsize write _size) a = do
Expand Down
1 change: 1 addition & 0 deletions io-sim/src/Control/Monad/IOSim.hs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ instance MonadSTM (SimM s) where

newTBQueue = newTBQueueDefault
readTBQueue = readTBQueueDefault
tryReadTBQueue = tryReadTBQueueDefault
writeTBQueue = writeTBQueueDefault

instance MonadST (SimM s) where
Expand Down

0 comments on commit c153eb7

Please sign in to comment.