Skip to content

Commit 24a2ff1

Browse files
committed
MonadSTM: default implementations
Provide default implementations as part of `MonadSTM`, `MonadLabelledSTM` classes. Fixes IntersectMBO/ouroboros-network#3608
1 parent 2dee590 commit 24a2ff1

File tree

3 files changed

+130
-50
lines changed

3 files changed

+130
-50
lines changed

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

Lines changed: 129 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,10 @@ module Control.Monad.Class.MonadSTM
2727
, LazyTMVar
2828
-- * Default 'TMVar' implementation
2929
, TMVarDefault (..)
30-
, labelTMVarDefault
31-
, traceTMVarDefault
32-
, newTMVarDefault
33-
, newTMVarIODefault
34-
, newEmptyTMVarDefault
35-
, newEmptyTMVarIODefault
36-
, takeTMVarDefault
37-
, tryTakeTMVarDefault
38-
, putTMVarDefault
39-
, tryPutTMVarDefault
40-
, readTMVarDefault
41-
, tryReadTMVarDefault
42-
, swapTMVarDefault
43-
, isEmptyTMVarDefault
4430
-- * Default 'TBQueue' implementation
4531
, TQueueDefault (..)
46-
, labelTQueueDefault
47-
, newTQueueDefault
48-
, readTQueueDefault
49-
, tryReadTQueueDefault
50-
, peekTQueueDefault
51-
, tryPeekTQueueDefault
52-
, writeTQueueDefault
53-
, isEmptyTQueueDefault
5432
-- * Default 'TBQueue' implementation
5533
, TBQueueDefault (..)
56-
, labelTBQueueDefault
57-
, newTBQueueDefault
58-
, readTBQueueDefault
59-
, tryReadTBQueueDefault
60-
, peekTBQueueDefault
61-
, tryPeekTBQueueDefault
62-
, writeTBQueueDefault
63-
, isEmptyTBQueueDefault
64-
, isFullTBQueueDefault
65-
, lengthTBQueueDefault
66-
, flushTBQueueDefault
6734
-- * MonadThrow aliases
6835
, throwSTM
6936
, catchSTM
@@ -73,7 +40,7 @@ module Control.Monad.Class.MonadSTM
7340
, newTMVarMDefault
7441
, newEmptyTMVarM
7542
, newEmptyTMVarMDefault
76-
--
43+
-- * Utils
7744
, WrappedSTM (..)
7845
) where
7946

@@ -188,13 +155,123 @@ class ( Monad m
188155
newTQueueIO :: m (TQueue m a)
189156
newTBQueueIO :: Natural -> m (TBQueue m a)
190157

158+
--
159+
-- default implementations
160+
--
161+
162+
default newTMVar :: TMVar m ~ TMVarDefault m
163+
=> a -> STM m (TMVar m a)
164+
newTMVar = newTMVarDefault
165+
166+
default newEmptyTMVar :: TMVar m ~ TMVarDefault m
167+
=> STM m (TMVar m a)
168+
newEmptyTMVar = newEmptyTMVarDefault
169+
191170
newTVarIO = atomically . newTVar
192171
readTVarIO = atomically . readTVar
193172
newTMVarIO = atomically . newTMVar
194173
newEmptyTMVarIO = atomically newEmptyTMVar
195174
newTQueueIO = atomically newTQueue
196175
newTBQueueIO = atomically . newTBQueue
197176

177+
default takeTMVar :: TMVar m ~ TMVarDefault m
178+
=> TMVar m a -> STM m a
179+
takeTMVar = takeTMVarDefault
180+
181+
default tryTakeTMVar :: TMVar m ~ TMVarDefault m
182+
=> TMVar m a -> STM m (Maybe a)
183+
tryTakeTMVar = tryTakeTMVarDefault
184+
185+
default putTMVar :: TMVar m ~ TMVarDefault m => TMVar m a -> a -> STM m ()
186+
putTMVar = putTMVarDefault
187+
188+
default tryPutTMVar :: TMVar m ~ TMVarDefault m => TMVar m a -> a -> STM m Bool
189+
tryPutTMVar = tryPutTMVarDefault
190+
191+
default readTMVar :: TMVar m ~ TMVarDefault m
192+
=> TMVar m a -> STM m a
193+
readTMVar = readTMVarDefault
194+
195+
default tryReadTMVar :: TMVar m ~ TMVarDefault m
196+
=> TMVar m a -> STM m (Maybe a)
197+
tryReadTMVar = tryReadTMVarDefault
198+
199+
default swapTMVar :: TMVar m ~ TMVarDefault m
200+
=> TMVar m a -> a -> STM m a
201+
swapTMVar = swapTMVarDefault
202+
203+
default isEmptyTMVar :: TMVar m ~ TMVarDefault m
204+
=> TMVar m a -> STM m Bool
205+
isEmptyTMVar = isEmptyTMVarDefault
206+
207+
default newTQueue :: TQueue m ~ TQueueDefault m
208+
=> STM m (TQueue m a)
209+
newTQueue = newTQueueDefault
210+
211+
default writeTQueue :: TQueue m ~ TQueueDefault m
212+
=> TQueue m a -> a -> STM m ()
213+
writeTQueue = writeTQueueDefault
214+
215+
default readTQueue :: TQueue m ~ TQueueDefault m
216+
=> TQueue m a -> STM m a
217+
readTQueue = readTQueueDefault
218+
219+
default tryReadTQueue :: TQueue m ~ TQueueDefault m
220+
=> TQueue m a -> STM m (Maybe a)
221+
tryReadTQueue = tryReadTQueueDefault
222+
223+
default isEmptyTQueue :: TQueue m ~ TQueueDefault m
224+
=> TQueue m a -> STM m Bool
225+
isEmptyTQueue = isEmptyTQueueDefault
226+
227+
default peekTQueue :: TQueue m ~ TQueueDefault m
228+
=> TQueue m a -> STM m a
229+
peekTQueue = peekTQueueDefault
230+
231+
default tryPeekTQueue :: TQueue m ~ TQueueDefault m
232+
=> TQueue m a -> STM m (Maybe a)
233+
tryPeekTQueue = tryPeekTQueueDefault
234+
235+
default newTBQueue :: TBQueue m ~ TBQueueDefault m
236+
=> Natural -> STM m (TBQueue m a)
237+
newTBQueue = newTBQueueDefault
238+
239+
default writeTBQueue :: TBQueue m ~ TBQueueDefault m
240+
=> TBQueue m a -> a -> STM m ()
241+
writeTBQueue = writeTBQueueDefault
242+
243+
default readTBQueue :: TBQueue m ~ TBQueueDefault m
244+
=> TBQueue m a -> STM m a
245+
readTBQueue = readTBQueueDefault
246+
247+
default tryReadTBQueue :: TBQueue m ~ TBQueueDefault m
248+
=> TBQueue m a -> STM m (Maybe a)
249+
tryReadTBQueue = tryReadTBQueueDefault
250+
251+
default isEmptyTBQueue :: TBQueue m ~ TBQueueDefault m
252+
=> TBQueue m a -> STM m Bool
253+
isEmptyTBQueue = isEmptyTBQueueDefault
254+
255+
default peekTBQueue :: TBQueue m ~ TBQueueDefault m
256+
=> TBQueue m a -> STM m a
257+
peekTBQueue = peekTBQueueDefault
258+
259+
default tryPeekTBQueue :: TBQueue m ~ TBQueueDefault m
260+
=> TBQueue m a -> STM m (Maybe a)
261+
tryPeekTBQueue = tryPeekTBQueueDefault
262+
263+
default isFullTBQueue :: TBQueue m ~ TBQueueDefault m
264+
=> TBQueue m a -> STM m Bool
265+
isFullTBQueue = isFullTBQueueDefault
266+
267+
default lengthTBQueue :: TBQueue m ~ TBQueueDefault m
268+
=> TBQueue m a -> STM m Natural
269+
lengthTBQueue = lengthTBQueueDefault
270+
271+
default flushTBQueue :: TBQueue m ~ TBQueueDefault m
272+
=> TBQueue m a -> STM m [a]
273+
flushTBQueue = flushTBQueueDefault
274+
198275

199276
stateTVarDefault :: MonadSTM m => TVar m s -> (s -> (a, s)) -> STM m a
200277
stateTVarDefault var f = do
@@ -237,6 +314,22 @@ class MonadSTM m
237314
labelTQueueIO :: TQueue m a -> String -> m ()
238315
labelTBQueueIO :: TBQueue m a -> String -> m ()
239316

317+
--
318+
-- default implementations
319+
--
320+
321+
default labelTMVar :: TMVar m ~ TMVarDefault m
322+
=> TMVar m a -> String -> STM m ()
323+
labelTMVar = labelTMVarDefault
324+
325+
default labelTQueue :: TQueue m ~ TQueueDefault m
326+
=> TQueue m a -> String -> STM m ()
327+
labelTQueue = labelTQueueDefault
328+
329+
default labelTBQueue :: TBQueue m ~ TBQueueDefault m
330+
=> TBQueue m a -> String -> STM m ()
331+
labelTBQueue = labelTBQueueDefault
332+
240333
default labelTVarIO :: TVar m a -> String -> m ()
241334
labelTVarIO = \v l -> atomically (labelTVar v l)
242335

@@ -524,6 +617,7 @@ newTMVarIODefault :: MonadSTM m => a -> m (TMVarDefault m a)
524617
newTMVarIODefault a = do
525618
t <- newTVarM (Just a)
526619
return (TMVar t)
620+
{-# DEPRECATED newTMVarIODefault "MonadSTM provides a default implementation" #-}
527621

528622
newTMVarMDefault :: MonadSTM m => a -> m (TMVarDefault m a)
529623
newTMVarMDefault = newTMVarIODefault
@@ -538,6 +632,7 @@ newEmptyTMVarIODefault :: MonadSTM m => m (TMVarDefault m a)
538632
newEmptyTMVarIODefault = do
539633
t <- newTVarIO Nothing
540634
return (TMVar t)
635+
{-# DEPRECATED newEmptyTMVarIODefault "MonadSTM provides a default implementation" #-}
541636

542637
newEmptyTMVarMDefault :: MonadSTM m => m (TMVarDefault m a)
543638
newEmptyTMVarMDefault = newEmptyTMVarIODefault

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Control.Monad.Class.MonadSTM (MonadInspectSTM (..),
1414
import Numeric.Natural (Natural)
1515

1616
--
17-
-- Default TQueue implementation in terms of 'Seq' (used by sim)
17+
-- Default TQueue implementation in terms of a 'TVar' (used by sim)
1818
--
1919

2020
newtype TQueueDefault m a = TQueue (TVar m ([a], [a]))

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,6 @@ instance MonadSay (STMSim s) where
382382

383383
instance MonadLabelledSTM (IOSim s) where
384384
labelTVar tvar label = STM $ \k -> LabelTVar label tvar (k ())
385-
labelTMVar = MonadSTM.labelTMVarDefault
386385
labelTQueue = labelTQueueDefault
387386
labelTBQueue = labelTBQueueDefault
388387

@@ -401,17 +400,6 @@ instance MonadSTM (IOSim s) where
401400
retry = STM $ oneShot $ \_ -> Retry
402401
orElse a b = STM $ oneShot $ \k -> OrElse (runSTM a) (runSTM b) k
403402

404-
newTMVar = MonadSTM.newTMVarDefault
405-
newEmptyTMVar = MonadSTM.newEmptyTMVarDefault
406-
takeTMVar = MonadSTM.takeTMVarDefault
407-
tryTakeTMVar = MonadSTM.tryTakeTMVarDefault
408-
putTMVar = MonadSTM.putTMVarDefault
409-
tryPutTMVar = MonadSTM.tryPutTMVarDefault
410-
readTMVar = MonadSTM.readTMVarDefault
411-
tryReadTMVar = MonadSTM.tryReadTMVarDefault
412-
swapTMVar = MonadSTM.swapTMVarDefault
413-
isEmptyTMVar = MonadSTM.isEmptyTMVarDefault
414-
415403
newTQueue = newTQueueDefault
416404
readTQueue = readTQueueDefault
417405
tryReadTQueue = tryReadTQueueDefault
@@ -431,9 +419,6 @@ instance MonadSTM (IOSim s) where
431419
isEmptyTBQueue = isEmptyTBQueueDefault
432420
isFullTBQueue = isFullTBQueueDefault
433421

434-
newTMVarIO = MonadSTM.newTMVarIODefault
435-
newEmptyTMVarIO = MonadSTM.newEmptyTMVarIODefault
436-
437422
instance MonadInspectSTM (IOSim s) where
438423
type InspectMonad (IOSim s) = ST s
439424
inspectTVar _ TVar { tvarCurrent } = readSTRef tvarCurrent

0 commit comments

Comments
 (0)