Skip to content

Commit a0156f7

Browse files
iohk-bors[bot]coot
andauthored
Merge #3077
3077: peekTQueue and friends r=coot a=coot This PR contains two patches: - io-sim-classes: added peekTQueue and firends - io-sim-classes: derive Generic instance of Time Co-authored-by: Marcin Szamotulski <[email protected]>
2 parents b5a910b + 45050fe commit a0156f7

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

io-sim-classes/src/Control/Monad/Class/MonadSTM.hs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ module Control.Monad.Class.MonadSTM
3737
, newTQueueDefault
3838
, readTQueueDefault
3939
, tryReadTQueueDefault
40+
, peekTQueueDefault
41+
, tryPeekTQueueDefault
4042
, writeTQueueDefault
4143
, isEmptyTQueueDefault
4244

@@ -46,6 +48,8 @@ module Control.Monad.Class.MonadSTM
4648
, newTBQueueDefault
4749
, readTBQueueDefault
4850
, tryReadTBQueueDefault
51+
, peekTBQueueDefault
52+
, tryPeekTBQueueDefault
4953
, writeTBQueueDefault
5054
, isEmptyTBQueueDefault
5155
, isFullTBQueueDefault
@@ -131,13 +135,17 @@ class ( Monad stm
131135
newTQueue :: stm (TQueue_ stm a)
132136
readTQueue :: TQueue_ stm a -> stm a
133137
tryReadTQueue :: TQueue_ stm a -> stm (Maybe a)
138+
peekTQueue :: TQueue_ stm a -> stm a
139+
tryPeekTQueue :: TQueue_ stm a -> stm (Maybe a)
134140
writeTQueue :: TQueue_ stm a -> a -> stm ()
135141
isEmptyTQueue :: TQueue_ stm a -> stm Bool
136142

137143
type TBQueue_ stm :: Type -> Type
138144
newTBQueue :: Natural -> stm (TBQueue_ stm a)
139145
readTBQueue :: TBQueue_ stm a -> stm a
140146
tryReadTBQueue :: TBQueue_ stm a -> stm (Maybe a)
147+
peekTBQueue :: TBQueue_ stm a -> stm a
148+
tryPeekTBQueue :: TBQueue_ stm a -> stm (Maybe a)
141149
flushTBQueue :: TBQueue_ stm a -> stm [a]
142150
writeTBQueue :: TBQueue_ stm a -> a -> stm ()
143151
-- | @since 0.2.0.0
@@ -253,12 +261,16 @@ instance MonadSTMTx STM.STM where
253261
newTQueue = STM.newTQueue
254262
readTQueue = STM.readTQueue
255263
tryReadTQueue = STM.tryReadTQueue
264+
peekTQueue = STM.peekTQueue
265+
tryPeekTQueue = STM.tryPeekTQueue
256266
flushTBQueue = STM.flushTBQueue
257267
writeTQueue = STM.writeTQueue
258268
isEmptyTQueue = STM.isEmptyTQueue
259269
newTBQueue = STM.newTBQueue
260270
readTBQueue = STM.readTBQueue
261271
tryReadTBQueue = STM.tryReadTBQueue
272+
peekTBQueue = STM.peekTBQueue
273+
tryPeekTBQueue = STM.tryPeekTBQueue
262274
writeTBQueue = STM.writeTBQueue
263275
lengthTBQueue = STM.lengthTBQueue
264276
isEmptyTBQueue = STM.isEmptyTBQueue
@@ -462,6 +474,20 @@ isEmptyTQueueDefault (TQueue read write) = do
462474
[] -> return True
463475
_ -> return False
464476

477+
peekTQueueDefault :: MonadSTM m => TQueueDefault m a -> STM m a
478+
peekTQueueDefault (TQueue read _write) = do
479+
xs <- readTVar read
480+
case xs of
481+
(x:_) -> return x
482+
_ -> retry
483+
484+
tryPeekTQueueDefault :: MonadSTM m => TQueueDefault m a -> STM m (Maybe a)
485+
tryPeekTQueueDefault (TQueue read _write) = do
486+
xs <- readTVar read
487+
case xs of
488+
(x:_) -> return (Just x)
489+
_ -> return Nothing
490+
465491
--
466492
-- Default TBQueue implementation in terms of TVars (used by sim)
467493
--
@@ -514,6 +540,20 @@ tryReadTBQueueDefault (TBQueue rsize read _wsize write _size) = do
514540
writeTVar read zs
515541
return (Just z)
516542

543+
peekTBQueueDefault :: MonadSTM m => TBQueueDefault m a -> STM m a
544+
peekTBQueueDefault (TBQueue _rsize read _wsize _write _size) = do
545+
xs <- readTVar read
546+
case xs of
547+
(x:_) -> return x
548+
_ -> retry
549+
550+
tryPeekTBQueueDefault :: MonadSTM m => TBQueueDefault m a -> STM m (Maybe a)
551+
tryPeekTBQueueDefault (TBQueue _rsize read _wsize _write _size) = do
552+
xs <- readTVar read
553+
case xs of
554+
(x:_) -> return (Just x)
555+
_ -> return Nothing
556+
517557
writeTBQueueDefault :: MonadSTM m => TBQueueDefault m a -> a -> STM m ()
518558
writeTBQueueDefault (TBQueue rsize _read wsize write _size) a = do
519559
w <- readTVar wsize

io-sim-classes/src/Control/Monad/Class/MonadTime.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE DeriveGeneric #-}
12

23
module Control.Monad.Class.MonadTime (
34
MonadTime(..)
@@ -21,6 +22,7 @@ import Data.Time.Clock (DiffTime, NominalDiffTime, UTCTime,
2122
addUTCTime, diffUTCTime)
2223
import qualified Data.Time.Clock as Time
2324
import Data.Word (Word64)
25+
import GHC.Generics (Generic (..))
2426

2527
-- | A point in time in a monotonic clock.
2628
--
@@ -29,7 +31,7 @@ import Data.Word (Word64)
2931
-- program runs. It is represented as the 'DiffTime' from this arbitrary epoch.
3032
--
3133
newtype Time = Time DiffTime
32-
deriving (Eq, Ord, Show)
34+
deriving (Eq, Ord, Show, Generic)
3335

3436
-- | The time duration between two points in time (positive or negative).
3537
diffTime :: Time -> Time -> DiffTime

io-sim/src/Control/Monad/IOSim/Internal.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,16 @@ instance MonadSTMTx (STM s) where
360360
newTQueue = newTQueueDefault
361361
readTQueue = readTQueueDefault
362362
tryReadTQueue = tryReadTQueueDefault
363+
peekTQueue = peekTQueueDefault
364+
tryPeekTQueue = tryPeekTQueueDefault
363365
writeTQueue = writeTQueueDefault
364366
isEmptyTQueue = isEmptyTQueueDefault
365367

366368
newTBQueue = newTBQueueDefault
367369
readTBQueue = readTBQueueDefault
368370
tryReadTBQueue = tryReadTBQueueDefault
371+
peekTBQueue = peekTBQueueDefault
372+
tryPeekTBQueue = tryPeekTBQueueDefault
369373
flushTBQueue = flushTBQueueDefault
370374
writeTBQueue = writeTBQueueDefault
371375
lengthTBQueue = lengthTBQueueDefault

0 commit comments

Comments
 (0)