Skip to content

Commit 99e2a11

Browse files
committed
io-sim: use oneShot
1 parent 59968d0 commit 99e2a11

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

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

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ import qualified Debug.Trace as Debug
107107
import Text.Printf
108108

109109
import GHC.Generics (Generic)
110+
import GHC.Exts (oneShot)
110111
import Quiet (Quiet (..))
111112

112113
import Control.Monad.IOSim.CommonTypes
@@ -126,10 +127,10 @@ runIOSim :: IOSim s a -> SimA s a
126127
runIOSim (IOSim k) = k Return
127128

128129
traceM :: Typeable a => a -> IOSim s ()
129-
traceM x = IOSim $ \k -> Output (toDyn x) (k ())
130+
traceM x = IOSim $ oneShot $ \k -> Output (toDyn x) (k ())
130131

131132
traceSTM :: Typeable a => a -> STMSim s ()
132-
traceSTM x = STM $ \k -> OutputStm (toDyn x) (k ())
133+
traceSTM x = STM $ oneShot $ \k -> OutputStm (toDyn x) (k ())
133134

134135
data SimA s a where
135136
Return :: a -> SimA s a
@@ -203,24 +204,24 @@ type SimSTM = STM
203204

204205
instance Functor (IOSim s) where
205206
{-# INLINE fmap #-}
206-
fmap f = \d -> IOSim $ \k -> unIOSim d (k . f)
207+
fmap f = \d -> IOSim $ oneShot $ \k -> unIOSim d (k . f)
207208

208209
instance Applicative (IOSim s) where
209210
{-# INLINE pure #-}
210-
pure = \x -> IOSim $ \k -> k x
211+
pure = \x -> IOSim $ oneShot $ \k -> k x
211212

212213
{-# INLINE (<*>) #-}
213-
(<*>) = \df dx -> IOSim $ \k ->
214+
(<*>) = \df dx -> IOSim $ oneShot $ \k ->
214215
unIOSim df (\f -> unIOSim dx (\x -> k (f x)))
215216

216217
{-# INLINE (*>) #-}
217-
(*>) = \dm dn -> IOSim $ \k -> unIOSim dm (\_ -> unIOSim dn k)
218+
(*>) = \dm dn -> IOSim $ oneShot $ \k -> unIOSim dm (\_ -> unIOSim dn k)
218219

219220
instance Monad (IOSim s) where
220221
return = pure
221222

222223
{-# INLINE (>>=) #-}
223-
(>>=) = \dm f -> IOSim $ \k -> unIOSim dm (\m -> unIOSim (f m) k)
224+
(>>=) = \dm f -> IOSim $ oneShot $ \k -> unIOSim dm (\m -> unIOSim (f m) k)
224225

225226
{-# INLINE (>>) #-}
226227
(>>) = (*>)
@@ -240,32 +241,32 @@ instance Monoid a => Monoid (IOSim s a) where
240241
#endif
241242

242243
instance Fail.MonadFail (IOSim s) where
243-
fail msg = IOSim $ \_ -> Throw (toException (IO.Error.userError msg))
244+
fail msg = IOSim $ oneShot $ \_ -> Throw (toException (IO.Error.userError msg))
244245

245246
instance MonadFix (IOSim s) where
246-
mfix f = IOSim $ \k -> Fix f k
247+
mfix f = IOSim $ oneShot $ \k -> Fix f k
247248

248249

249250
instance Functor (STM s) where
250251
{-# INLINE fmap #-}
251-
fmap f = \d -> STM $ \k -> unSTM d (k . f)
252+
fmap f = \d -> STM $ oneShot $ \k -> unSTM d (k . f)
252253

253254
instance Applicative (STM s) where
254255
{-# INLINE pure #-}
255-
pure = \x -> STM $ \k -> k x
256+
pure = \x -> STM $ oneShot $ \k -> k x
256257

257258
{-# INLINE (<*>) #-}
258-
(<*>) = \df dx -> STM $ \k ->
259+
(<*>) = \df dx -> STM $ oneShot $ \k ->
259260
unSTM df (\f -> unSTM dx (\x -> k (f x)))
260261

261262
{-# INLINE (*>) #-}
262-
(*>) = \dm dn -> STM $ \k -> unSTM dm (\_ -> unSTM dn k)
263+
(*>) = \dm dn -> STM $ oneShot $ \k -> unSTM dm (\_ -> unSTM dn k)
263264

264265
instance Monad (STM s) where
265266
return = pure
266267

267268
{-# INLINE (>>=) #-}
268-
(>>=) = \dm f -> STM $ \k -> unSTM dm (\m -> unSTM (f m) k)
269+
(>>=) = \dm f -> STM $ oneShot $ \k -> unSTM dm (\m -> unSTM (f m) k)
269270

270271
{-# INLINE (>>) #-}
271272
(>>) = (*>)
@@ -275,7 +276,7 @@ instance Monad (STM s) where
275276
#endif
276277

277278
instance Fail.MonadFail (STM s) where
278-
fail msg = STM $ \_ -> ThrowStm (toException (ErrorCall msg))
279+
fail msg = STM $ oneShot $ \_ -> ThrowStm (toException (ErrorCall msg))
279280

280281
instance Alternative (STM s) where
281282
empty = MonadSTM.retry
@@ -284,19 +285,19 @@ instance Alternative (STM s) where
284285
instance MonadPlus (STM s) where
285286

286287
instance MonadSay (IOSim s) where
287-
say msg = IOSim $ \k -> Say msg (k ())
288+
say msg = IOSim $ oneShot $ \k -> Say msg (k ())
288289

289290
instance MonadThrow (IOSim s) where
290-
throwIO e = IOSim $ \_ -> Throw (toException e)
291+
throwIO e = IOSim $ oneShot $ \_ -> Throw (toException e)
291292

292293
instance MonadEvaluate (IOSim s) where
293-
evaluate a = IOSim $ \k -> Evaluate a k
294+
evaluate a = IOSim $ oneShot $ \k -> Evaluate a k
294295

295296
instance Exceptions.MonadThrow (IOSim s) where
296297
throwM = MonadThrow.throwIO
297298

298299
instance MonadThrow (STM s) where
299-
throwIO e = STM $ \_ -> ThrowStm (toException e)
300+
throwIO e = STM $ oneShot $ \_ -> ThrowStm (toException e)
300301

301302
-- Since these involve re-throwing the exception and we don't provide
302303
-- CatchSTM at all, then we can get away with trivial versions:
@@ -316,7 +317,7 @@ instance Exceptions.MonadThrow (STM s) where
316317

317318
instance MonadCatch (IOSim s) where
318319
catch action handler =
319-
IOSim $ \k -> Catch (runIOSim action) (runIOSim . handler) k
320+
IOSim $ oneShot $ \k -> Catch (runIOSim action) (runIOSim . handler) k
320321

321322
instance Exceptions.MonadCatch (IOSim s) where
322323
catch = MonadThrow.catch
@@ -363,19 +364,19 @@ blockUninterruptible a = IOSim (SetMaskState MaskedUninterruptible a)
363364

364365
instance MonadThread (IOSim s) where
365366
type ThreadId (IOSim s) = ThreadId
366-
myThreadId = IOSim $ \k -> GetThreadId k
367-
labelThread t l = IOSim $ \k -> LabelThread t l (k ())
367+
myThreadId = IOSim $ oneShot $ \k -> GetThreadId k
368+
labelThread t l = IOSim $ oneShot $ \k -> LabelThread t l (k ())
368369

369370
instance MonadFork (IOSim s) where
370-
forkIO task = IOSim $ \k -> Fork task k
371+
forkIO task = IOSim $ oneShot $ \k -> Fork task k
371372
forkIOWithUnmask f = forkIO (f unblock)
372-
throwTo tid e = IOSim $ \k -> ThrowTo (toException e) tid (k ())
373+
throwTo tid e = IOSim $ oneShot $ \k -> ThrowTo (toException e) tid (k ())
373374

374375
instance MonadTest (IOSim s) where
375-
exploreRaces = IOSim $ \k -> ExploreRaces (k ())
376+
exploreRaces = IOSim $ oneShot $ \k -> ExploreRaces (k ())
376377

377378
instance MonadSay (STMSim s) where
378-
say msg = STM $ \k -> SayStm msg (k ())
379+
say msg = STM $ oneShot $ \k -> SayStm msg (k ())
379380

380381

381382
instance MonadLabelledSTM (IOSim s) where
@@ -391,13 +392,13 @@ instance MonadSTM (IOSim s) where
391392
type TQueue (IOSim s) = TQueueDefault (IOSim s)
392393
type TBQueue (IOSim s) = TBQueueDefault (IOSim s)
393394

394-
atomically action = IOSim $ \k -> Atomically action k
395+
atomically action = IOSim $ oneShot $ \k -> Atomically action k
395396

396-
newTVar x = STM $ \k -> NewTVar Nothing x k
397-
readTVar tvar = STM $ \k -> ReadTVar tvar k
398-
writeTVar tvar x = STM $ \k -> WriteTVar tvar x (k ())
399-
retry = STM $ \_ -> Retry
400-
orElse a b = STM $ \k -> OrElse (runSTM a) (runSTM b) k
397+
newTVar x = STM $ oneShot $ \k -> NewTVar Nothing x k
398+
readTVar tvar = STM $ oneShot $ \k -> ReadTVar tvar k
399+
writeTVar tvar x = STM $ oneShot $ \k -> WriteTVar tvar x (k ())
400+
retry = STM $ oneShot $ \_ -> Retry
401+
orElse a b = STM $ oneShot $ \k -> OrElse (runSTM a) (runSTM b) k
401402

402403
newTMVar = MonadSTM.newTMVarDefault
403404
newEmptyTMVar = MonadSTM.newEmptyTMVarDefault
@@ -484,26 +485,26 @@ instance MonadST (IOSim s) where
484485
withLiftST f = f liftST
485486

486487
liftST :: StrictST.ST s a -> IOSim s a
487-
liftST action = IOSim $ \k -> LiftST action k
488+
liftST action = IOSim $ oneShot $ \k -> LiftST action k
488489

489490
instance MonadMonotonicTime (IOSim s) where
490-
getMonotonicTime = IOSim $ \k -> GetMonoTime k
491+
getMonotonicTime = IOSim $ oneShot $ \k -> GetMonoTime k
491492

492493
instance MonadTime (IOSim s) where
493-
getCurrentTime = IOSim $ \k -> GetWallTime k
494+
getCurrentTime = IOSim $ oneShot $ \k -> GetWallTime k
494495

495496
-- | Set the current wall clock time for the thread's clock domain.
496497
--
497498
setCurrentTime :: UTCTime -> IOSim s ()
498-
setCurrentTime t = IOSim $ \k -> SetWallTime t (k ())
499+
setCurrentTime t = IOSim $ oneShot $ \k -> SetWallTime t (k ())
499500

500501
-- | Put the thread into a new wall clock domain, not shared with the parent
501502
-- thread. Changing the wall clock time in the new clock domain will not affect
502503
-- the other clock of other threads. All threads forked by this thread from
503504
-- this point onwards will share the new clock domain.
504505
--
505506
unshareClock :: IOSim s ()
506-
unshareClock = IOSim $ \k -> UnshareClock (k ())
507+
unshareClock = IOSim $ oneShot $ \k -> UnshareClock (k ())
507508

508509
instance MonadDelay (IOSim s) where
509510
-- Use default in terms of MonadTimer
@@ -518,9 +519,9 @@ instance MonadTimer (IOSim s) where
518519
readTimeout (Timeout var _bvar _key) = MonadSTM.readTVar var
519520
readTimeout (NegativeTimeout _key) = pure TimeoutCancelled
520521

521-
newTimeout d = IOSim $ \k -> NewTimeout d k
522-
updateTimeout t d = IOSim $ \k -> UpdateTimeout t d (k ())
523-
cancelTimeout t = IOSim $ \k -> CancelTimeout t (k ())
522+
newTimeout d = IOSim $ oneShot $ \k -> NewTimeout d k
523+
updateTimeout t d = IOSim $ oneShot $ \k -> UpdateTimeout t d (k ())
524+
cancelTimeout t = IOSim $ oneShot $ \k -> CancelTimeout t (k ())
524525

525526
timeout d action
526527
| d < 0 = Just <$> action
@@ -543,7 +544,7 @@ instance MonadTimer (IOSim s) where
543544
throwTo pid' AsyncCancelled)
544545
(\_ -> Just <$> action)
545546

546-
registerDelay d = IOSim $ \k -> NewTimeout d (\(Timeout _var bvar _) -> k bvar)
547+
registerDelay d = IOSim $ oneShot $ \k -> NewTimeout d (\(Timeout _var bvar _) -> k bvar)
547548

548549
newtype TimeoutException = TimeoutException TimeoutId deriving Eq
549550

0 commit comments

Comments
 (0)