Skip to content

Commit 1a3f3bb

Browse files
committed
MonadSTM.Strict: added TQueue & TBQueue
Fixes IntersectMBO/ouroboros-network#2797.
1 parent 24a2ff1 commit 1a3f3bb

File tree

1 file changed

+166
-10
lines changed
  • strict-stm/src/Control/Monad/Class/MonadSTM

1 file changed

+166
-10
lines changed

strict-stm/src/Control/Monad/Class/MonadSTM/Strict.hs

Lines changed: 166 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,40 @@ module Control.Monad.Class.MonadSTM.Strict
5353
, tryReadTMVar
5454
, swapTMVar
5555
, isEmptyTMVar
56+
-- * 'StrictTQueue'
57+
, StrictTQueue
58+
, labelTQueue
59+
, labelTQueueIO
60+
, traceTQueue
61+
, traceTQueueIO
62+
, toLazyTQueue
63+
, fromLazyTQueue
64+
, newTQueue
65+
, newTQueueIO
66+
, readTQueue
67+
, tryReadTQueue
68+
, peekTQueue
69+
, tryPeekTQueue
70+
, writeTQueue
71+
, isEmptyTQueue
72+
-- * 'StrictTBQueue'
73+
, StrictTBQueue
74+
, labelTBQueue
75+
, labelTBQueueIO
76+
, traceTBQueue
77+
, traceTBQueueIO
78+
, toLazyTBQueue
79+
, fromLazyTBQueue
80+
, newTBQueue
81+
, newTBQueueIO
82+
, readTBQueue
83+
, tryReadTBQueue
84+
, peekTBQueue
85+
, tryPeekTBQueue
86+
, writeTBQueue
87+
, lengthTBQueue
88+
, isEmptyTBQueue
89+
, isFullTBQueue
5690
-- ** Low-level API
5791
, checkInvariant
5892
-- * Deprecated API
@@ -64,23 +98,32 @@ module Control.Monad.Class.MonadSTM.Strict
6498
) where
6599

66100
import Control.Monad.Class.MonadSTM as X hiding (LazyTMVar, LazyTVar,
67-
TMVar, TVar, isEmptyTMVar, labelTMVar, labelTMVarIO,
68-
labelTVar, labelTVarIO, modifyTVar, newEmptyTMVar,
69-
newEmptyTMVarIO, newEmptyTMVarM, newTMVar, newTMVarIO,
70-
newTMVarM, newTVar, newTVarIO, newTVarM, putTMVar,
71-
readTMVar, readTVar, readTVarIO, stateTVar, swapTMVar,
72-
swapTVar, takeTMVar, traceTMVar, traceTMVarIO, traceTVar,
73-
traceTVarIO, tryPutTMVar, tryReadTMVar, tryTakeTMVar,
74-
writeTVar)
101+
TMVar, TVar, isEmptyTBQueue, isEmptyTMVar, isEmptyTQueue,
102+
isFullTBQueue, labelTBQueue, labelTBQueueIO, labelTMVar,
103+
labelTMVarIO, labelTQueue, labelTQueueIO, labelTVar,
104+
labelTVarIO, lengthTBQueue, modifyTVar, newEmptyTMVar,
105+
newEmptyTMVarIO, newEmptyTMVarM, newTBQueue, newTBQueueIO,
106+
newTMVar, newTMVarIO, newTMVarM, newTQueue, newTQueueIO,
107+
newTVar, newTVarIO, newTVarM, peekTBQueue, peekTQueue,
108+
putTMVar, readTBQueue, readTMVar, readTQueue, readTVar,
109+
readTVarIO, stateTVar, swapTMVar, swapTVar, takeTMVar,
110+
traceTBQueue, traceTBQueueIO, traceTMVar, traceTMVarIO,
111+
traceTQueue, traceTQueueIO, traceTVar, traceTVarIO,
112+
tryPeekTBQueue, tryPeekTQueue, tryPutTMVar, tryReadTBQueue,
113+
tryReadTMVar, tryReadTQueue, tryTakeTMVar, writeTBQueue,
114+
writeTQueue, writeTVar)
75115
import qualified Control.Monad.Class.MonadSTM as Lazy
76116
import GHC.Stack
117+
import Numeric.Natural (Natural)
77118

78119
{-------------------------------------------------------------------------------
79120
Lazy TVar
80121
-------------------------------------------------------------------------------}
81122

82-
type LazyTVar m = Lazy.TVar m
83-
type LazyTMVar m = Lazy.TMVar m
123+
type LazyTVar m = Lazy.TVar m
124+
type LazyTMVar m = Lazy.TMVar m
125+
type LazyTQueue m = Lazy.TQueue m
126+
type LazyTBQueue m = Lazy.TBQueue m
84127

85128
{-------------------------------------------------------------------------------
86129
Strict TVar
@@ -290,6 +333,119 @@ swapTMVar (StrictTMVar tmvar) !a = Lazy.swapTMVar tmvar a
290333
isEmptyTMVar :: MonadSTM m => StrictTMVar m a -> STM m Bool
291334
isEmptyTMVar (StrictTMVar tmvar) = Lazy.isEmptyTMVar tmvar
292335

336+
{-------------------------------------------------------------------------------
337+
Strict TQueue
338+
-------------------------------------------------------------------------------}
339+
340+
newtype StrictTQueue m a = StrictTQueue { toLazyTQueue :: LazyTQueue m a }
341+
342+
fromLazyTQueue :: LazyTQueue m a -> StrictTQueue m a
343+
fromLazyTQueue = StrictTQueue
344+
345+
labelTQueue :: MonadLabelledSTM m => StrictTQueue m a -> String -> STM m ()
346+
labelTQueue (StrictTQueue queue) = Lazy.labelTQueue queue
347+
348+
labelTQueueIO :: MonadLabelledSTM m => StrictTQueue m a -> String -> m ()
349+
labelTQueueIO (StrictTQueue queue) = Lazy.labelTQueueIO queue
350+
351+
traceTQueue :: MonadTraceSTM m
352+
=> proxy m
353+
-> StrictTQueue m a
354+
-> ((Maybe [a]) -> [a] -> InspectMonad m TraceValue)
355+
-> STM m ()
356+
traceTQueue p (StrictTQueue queue) = Lazy.traceTQueue p queue
357+
358+
traceTQueueIO :: MonadTraceSTM m
359+
=> proxy m
360+
-> StrictTQueue m a
361+
-> ((Maybe [a]) -> [a] -> InspectMonad m TraceValue)
362+
-> m ()
363+
traceTQueueIO p (StrictTQueue queue) = Lazy.traceTQueueIO p queue
364+
365+
newTQueue :: MonadSTM m => STM m (StrictTQueue m a)
366+
newTQueue = StrictTQueue <$> Lazy.newTQueue
367+
368+
newTQueueIO :: MonadSTM m => m (StrictTQueue m a)
369+
newTQueueIO = atomically newTQueue
370+
371+
readTQueue :: MonadSTM m => StrictTQueue m a -> STM m a
372+
readTQueue = Lazy.readTQueue . toLazyTQueue
373+
374+
tryReadTQueue :: MonadSTM m => StrictTQueue m a -> STM m (Maybe a)
375+
tryReadTQueue = Lazy.tryReadTQueue . toLazyTQueue
376+
377+
peekTQueue :: MonadSTM m => StrictTQueue m a -> STM m a
378+
peekTQueue = Lazy.peekTQueue . toLazyTQueue
379+
380+
tryPeekTQueue :: MonadSTM m => StrictTQueue m a -> STM m (Maybe a)
381+
tryPeekTQueue = Lazy.tryPeekTQueue . toLazyTQueue
382+
383+
writeTQueue :: MonadSTM m => StrictTQueue m a -> a -> STM m ()
384+
writeTQueue (StrictTQueue tqueue) !a = Lazy.writeTQueue tqueue a
385+
386+
isEmptyTQueue :: MonadSTM m => StrictTQueue m a -> STM m Bool
387+
isEmptyTQueue = Lazy.isEmptyTQueue . toLazyTQueue
388+
389+
{-------------------------------------------------------------------------------
390+
Strict TBQueue
391+
-------------------------------------------------------------------------------}
392+
393+
newtype StrictTBQueue m a = StrictTBQueue { toLazyTBQueue :: LazyTBQueue m a }
394+
395+
fromLazyTBQueue :: LazyTBQueue m a -> StrictTBQueue m a
396+
fromLazyTBQueue = StrictTBQueue
397+
398+
labelTBQueue :: MonadLabelledSTM m => StrictTBQueue m a -> String -> STM m ()
399+
labelTBQueue (StrictTBQueue queue) = Lazy.labelTBQueue queue
400+
401+
labelTBQueueIO :: MonadLabelledSTM m => StrictTBQueue m a -> String -> m ()
402+
labelTBQueueIO (StrictTBQueue queue) = Lazy.labelTBQueueIO queue
403+
404+
traceTBQueue :: MonadTraceSTM m
405+
=> proxy m
406+
-> StrictTBQueue m a
407+
-> ((Maybe [a]) -> [a] -> InspectMonad m TraceValue)
408+
-> STM m ()
409+
traceTBQueue p (StrictTBQueue queue) = Lazy.traceTBQueue p queue
410+
411+
traceTBQueueIO :: MonadTraceSTM m
412+
=> proxy m
413+
-> StrictTBQueue m a
414+
-> ((Maybe [a]) -> [a] -> InspectMonad m TraceValue)
415+
-> m ()
416+
traceTBQueueIO p (StrictTBQueue queue) = Lazy.traceTBQueueIO p queue
417+
418+
newTBQueue :: MonadSTM m => Natural -> STM m (StrictTBQueue m a)
419+
newTBQueue n = StrictTBQueue <$> Lazy.newTBQueue n
420+
421+
newTBQueueIO :: MonadSTM m => Natural -> m (StrictTBQueue m a)
422+
newTBQueueIO = atomically . newTBQueue
423+
424+
readTBQueue :: MonadSTM m => StrictTBQueue m a -> STM m a
425+
readTBQueue = Lazy.readTBQueue . toLazyTBQueue
426+
427+
tryReadTBQueue :: MonadSTM m => StrictTBQueue m a -> STM m (Maybe a)
428+
tryReadTBQueue = Lazy.tryReadTBQueue . toLazyTBQueue
429+
430+
peekTBQueue :: MonadSTM m => StrictTBQueue m a -> STM m a
431+
peekTBQueue = Lazy.peekTBQueue . toLazyTBQueue
432+
433+
tryPeekTBQueue :: MonadSTM m => StrictTBQueue m a -> STM m (Maybe a)
434+
tryPeekTBQueue = Lazy.tryPeekTBQueue . toLazyTBQueue
435+
436+
writeTBQueue :: MonadSTM m => StrictTBQueue m a -> a -> STM m ()
437+
writeTBQueue (StrictTBQueue tqueue) !a = Lazy.writeTBQueue tqueue a
438+
439+
lengthTBQueue :: MonadSTM m => StrictTBQueue m a -> STM m Natural
440+
lengthTBQueue = Lazy.lengthTBQueue . toLazyTBQueue
441+
442+
isEmptyTBQueue :: MonadSTM m => StrictTBQueue m a -> STM m Bool
443+
isEmptyTBQueue = Lazy.isEmptyTBQueue . toLazyTBQueue
444+
445+
isFullTBQueue :: MonadSTM m => StrictTBQueue m a -> STM m Bool
446+
isFullTBQueue = Lazy.isFullTBQueue . toLazyTBQueue
447+
448+
293449
{-------------------------------------------------------------------------------
294450
Dealing with invariants
295451
-------------------------------------------------------------------------------}

0 commit comments

Comments
 (0)