@@ -107,6 +107,7 @@ import qualified Debug.Trace as Debug
107
107
import Text.Printf
108
108
109
109
import GHC.Generics (Generic )
110
+ import GHC.Exts (oneShot )
110
111
import Quiet (Quiet (.. ))
111
112
112
113
import Control.Monad.IOSim.CommonTypes
@@ -126,10 +127,10 @@ runIOSim :: IOSim s a -> SimA s a
126
127
runIOSim (IOSim k) = k Return
127
128
128
129
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 () )
130
131
131
132
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 () )
133
134
134
135
data SimA s a where
135
136
Return :: a -> SimA s a
@@ -203,24 +204,24 @@ type SimSTM = STM
203
204
204
205
instance Functor (IOSim s ) where
205
206
{-# INLINE fmap #-}
206
- fmap f = \ d -> IOSim $ \ k -> unIOSim d (k . f)
207
+ fmap f = \ d -> IOSim $ oneShot $ \ k -> unIOSim d (k . f)
207
208
208
209
instance Applicative (IOSim s ) where
209
210
{-# INLINE pure #-}
210
- pure = \ x -> IOSim $ \ k -> k x
211
+ pure = \ x -> IOSim $ oneShot $ \ k -> k x
211
212
212
213
{-# INLINE (<*>) #-}
213
- (<*>) = \ df dx -> IOSim $ \ k ->
214
+ (<*>) = \ df dx -> IOSim $ oneShot $ \ k ->
214
215
unIOSim df (\ f -> unIOSim dx (\ x -> k (f x)))
215
216
216
217
{-# INLINE (*>) #-}
217
- (*>) = \ dm dn -> IOSim $ \ k -> unIOSim dm (\ _ -> unIOSim dn k)
218
+ (*>) = \ dm dn -> IOSim $ oneShot $ \ k -> unIOSim dm (\ _ -> unIOSim dn k)
218
219
219
220
instance Monad (IOSim s ) where
220
221
return = pure
221
222
222
223
{-# 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)
224
225
225
226
{-# INLINE (>>) #-}
226
227
(>>) = (*>)
@@ -240,32 +241,32 @@ instance Monoid a => Monoid (IOSim s a) where
240
241
#endif
241
242
242
243
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))
244
245
245
246
instance MonadFix (IOSim s ) where
246
- mfix f = IOSim $ \ k -> Fix f k
247
+ mfix f = IOSim $ oneShot $ \ k -> Fix f k
247
248
248
249
249
250
instance Functor (STM s ) where
250
251
{-# INLINE fmap #-}
251
- fmap f = \ d -> STM $ \ k -> unSTM d (k . f)
252
+ fmap f = \ d -> STM $ oneShot $ \ k -> unSTM d (k . f)
252
253
253
254
instance Applicative (STM s ) where
254
255
{-# INLINE pure #-}
255
- pure = \ x -> STM $ \ k -> k x
256
+ pure = \ x -> STM $ oneShot $ \ k -> k x
256
257
257
258
{-# INLINE (<*>) #-}
258
- (<*>) = \ df dx -> STM $ \ k ->
259
+ (<*>) = \ df dx -> STM $ oneShot $ \ k ->
259
260
unSTM df (\ f -> unSTM dx (\ x -> k (f x)))
260
261
261
262
{-# INLINE (*>) #-}
262
- (*>) = \ dm dn -> STM $ \ k -> unSTM dm (\ _ -> unSTM dn k)
263
+ (*>) = \ dm dn -> STM $ oneShot $ \ k -> unSTM dm (\ _ -> unSTM dn k)
263
264
264
265
instance Monad (STM s ) where
265
266
return = pure
266
267
267
268
{-# 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)
269
270
270
271
{-# INLINE (>>) #-}
271
272
(>>) = (*>)
@@ -275,7 +276,7 @@ instance Monad (STM s) where
275
276
#endif
276
277
277
278
instance Fail. MonadFail (STM s ) where
278
- fail msg = STM $ \ _ -> ThrowStm (toException (ErrorCall msg))
279
+ fail msg = STM $ oneShot $ \ _ -> ThrowStm (toException (ErrorCall msg))
279
280
280
281
instance Alternative (STM s ) where
281
282
empty = MonadSTM. retry
@@ -284,19 +285,19 @@ instance Alternative (STM s) where
284
285
instance MonadPlus (STM s ) where
285
286
286
287
instance MonadSay (IOSim s ) where
287
- say msg = IOSim $ \ k -> Say msg (k () )
288
+ say msg = IOSim $ oneShot $ \ k -> Say msg (k () )
288
289
289
290
instance MonadThrow (IOSim s ) where
290
- throwIO e = IOSim $ \ _ -> Throw (toException e)
291
+ throwIO e = IOSim $ oneShot $ \ _ -> Throw (toException e)
291
292
292
293
instance MonadEvaluate (IOSim s ) where
293
- evaluate a = IOSim $ \ k -> Evaluate a k
294
+ evaluate a = IOSim $ oneShot $ \ k -> Evaluate a k
294
295
295
296
instance Exceptions. MonadThrow (IOSim s ) where
296
297
throwM = MonadThrow. throwIO
297
298
298
299
instance MonadThrow (STM s ) where
299
- throwIO e = STM $ \ _ -> ThrowStm (toException e)
300
+ throwIO e = STM $ oneShot $ \ _ -> ThrowStm (toException e)
300
301
301
302
-- Since these involve re-throwing the exception and we don't provide
302
303
-- CatchSTM at all, then we can get away with trivial versions:
@@ -316,7 +317,7 @@ instance Exceptions.MonadThrow (STM s) where
316
317
317
318
instance MonadCatch (IOSim s ) where
318
319
catch action handler =
319
- IOSim $ \ k -> Catch (runIOSim action) (runIOSim . handler) k
320
+ IOSim $ oneShot $ \ k -> Catch (runIOSim action) (runIOSim . handler) k
320
321
321
322
instance Exceptions. MonadCatch (IOSim s ) where
322
323
catch = MonadThrow. catch
@@ -363,19 +364,19 @@ blockUninterruptible a = IOSim (SetMaskState MaskedUninterruptible a)
363
364
364
365
instance MonadThread (IOSim s ) where
365
366
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 () )
368
369
369
370
instance MonadFork (IOSim s ) where
370
- forkIO task = IOSim $ \ k -> Fork task k
371
+ forkIO task = IOSim $ oneShot $ \ k -> Fork task k
371
372
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 () )
373
374
374
375
instance MonadTest (IOSim s ) where
375
- exploreRaces = IOSim $ \ k -> ExploreRaces (k () )
376
+ exploreRaces = IOSim $ oneShot $ \ k -> ExploreRaces (k () )
376
377
377
378
instance MonadSay (STMSim s ) where
378
- say msg = STM $ \ k -> SayStm msg (k () )
379
+ say msg = STM $ oneShot $ \ k -> SayStm msg (k () )
379
380
380
381
381
382
instance MonadLabelledSTM (IOSim s ) where
@@ -391,13 +392,13 @@ instance MonadSTM (IOSim s) where
391
392
type TQueue (IOSim s ) = TQueueDefault (IOSim s )
392
393
type TBQueue (IOSim s ) = TBQueueDefault (IOSim s )
393
394
394
- atomically action = IOSim $ \ k -> Atomically action k
395
+ atomically action = IOSim $ oneShot $ \ k -> Atomically action k
395
396
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
401
402
402
403
newTMVar = MonadSTM. newTMVarDefault
403
404
newEmptyTMVar = MonadSTM. newEmptyTMVarDefault
@@ -484,26 +485,26 @@ instance MonadST (IOSim s) where
484
485
withLiftST f = f liftST
485
486
486
487
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
488
489
489
490
instance MonadMonotonicTime (IOSim s ) where
490
- getMonotonicTime = IOSim $ \ k -> GetMonoTime k
491
+ getMonotonicTime = IOSim $ oneShot $ \ k -> GetMonoTime k
491
492
492
493
instance MonadTime (IOSim s ) where
493
- getCurrentTime = IOSim $ \ k -> GetWallTime k
494
+ getCurrentTime = IOSim $ oneShot $ \ k -> GetWallTime k
494
495
495
496
-- | Set the current wall clock time for the thread's clock domain.
496
497
--
497
498
setCurrentTime :: UTCTime -> IOSim s ()
498
- setCurrentTime t = IOSim $ \ k -> SetWallTime t (k () )
499
+ setCurrentTime t = IOSim $ oneShot $ \ k -> SetWallTime t (k () )
499
500
500
501
-- | Put the thread into a new wall clock domain, not shared with the parent
501
502
-- thread. Changing the wall clock time in the new clock domain will not affect
502
503
-- the other clock of other threads. All threads forked by this thread from
503
504
-- this point onwards will share the new clock domain.
504
505
--
505
506
unshareClock :: IOSim s ()
506
- unshareClock = IOSim $ \ k -> UnshareClock (k () )
507
+ unshareClock = IOSim $ oneShot $ \ k -> UnshareClock (k () )
507
508
508
509
instance MonadDelay (IOSim s ) where
509
510
-- Use default in terms of MonadTimer
@@ -518,9 +519,9 @@ instance MonadTimer (IOSim s) where
518
519
readTimeout (Timeout var _bvar _key ) = MonadSTM.readTVar var
519
520
readTimeout (NegativeTimeout _key ) = pure TimeoutCancelled
520
521
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 () )
524
525
525
526
timeout d action
526
527
| d < 0 = Just <$> action
@@ -543,7 +544,7 @@ instance MonadTimer (IOSim s) where
543
544
throwTo pid' AsyncCancelled )
544
545
(\ _ -> Just <$> action )
545
546
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 )
547
548
548
549
newtype TimeoutException = TimeoutException TimeoutId deriving Eq
549
550
0 commit comments