Skip to content

Commit f0d7d53

Browse files
Merge #2837
2837: Preparatory changes for ThreadNet rewrite (PR number 2) r=nfrisby a=nfrisby This PR is the second of a few. These are relatively minor lower-level changes that I've needed for the ongoing ThreadNet rewrite. Since it's beginning to mature, I'm going to open a few PRs that contain a few of these changes. This set of commits primary alteration is a configurable timeout for the response to ChainSync's `MsgFindIntersect` message. In the tests create large delays that are difficult to predict, so we simply suspend this timeout in the test (it is currently `shortWait` in the implementation). We also provide some additional STM primitives that have been useful in tracing/debuging STM-based deadlocks in `io-sim`. Lastly, there is a slightly more granular export that allows additional reuse in the rewritten ThreadNet code. Co-authored-by: Nicolas Frisby <[email protected]>
2 parents 7dbbdcd + 2031aa6 commit f0d7d53

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Control.Monad.IOSim (
1414
runSimTraceST,
1515
liftST,
1616
traceM,
17+
traceSTM,
1718
-- * Simulation time
1819
setCurrentTime,
1920
unshareClock,

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

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{-# LANGUAGE FlexibleInstances #-}
88
{-# LANGUAGE GADTSyntax #-}
99
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
10+
{-# LANGUAGE LambdaCase #-}
1011
{-# LANGUAGE MultiParamTypeClasses #-}
1112
{-# LANGUAGE NamedFieldPuns #-}
1213
{-# LANGUAGE RankNTypes #-}
@@ -24,6 +25,7 @@ module Control.Monad.IOSim.Internal (
2425
runIOSim,
2526
runSimTraceST,
2627
traceM,
28+
traceSTM,
2729
STM,
2830
STMSim,
2931
SimSTM,
@@ -97,6 +99,9 @@ runIOSim (IOSim k) = k Return
9799
traceM :: Typeable a => a -> IOSim s ()
98100
traceM x = IOSim $ \k -> Output (toDyn x) (k ())
99101

102+
traceSTM :: Typeable a => a -> STMSim s ()
103+
traceSTM x = STM $ \k -> OutputStm (toDyn x) (k ())
104+
100105
data SimA s a where
101106
Return :: a -> SimA s a
102107

@@ -146,6 +151,9 @@ data StmA s a where
146151
Retry :: StmA s b
147152
OrElse :: StmA s a -> StmA s a -> (a -> StmA s b) -> StmA s b
148153

154+
SayStm :: String -> StmA s b -> StmA s b
155+
OutputStm :: Dynamic -> StmA s b -> StmA s b
156+
149157
-- Exported type
150158
type STMSim = STM
151159

@@ -314,6 +322,9 @@ instance MonadFork (IOSim s) where
314322
forkIOWithUnmask f = forkIO (f unblock)
315323
throwTo tid e = IOSim $ \k -> ThrowTo (toException e) tid (k ())
316324

325+
instance MonadSay (STMSim s) where
326+
say msg = STM $ \k -> SayStm msg (k ())
327+
317328
instance MonadSTMTx (STM s) where
318329
type TVar_ (STM s) = TVar s
319330
type TMVar_ (STM s) = TMVarDefault (IOSim s)
@@ -866,8 +877,7 @@ schedule thread@Thread{
866877
, nextTid = succ nextTid }
867878
return (Trace time tid tlbl (EventThreadForked tid') trace)
868879

869-
Atomically a k -> do
870-
res <- execAtomically nextVid (runSTM a)
880+
Atomically a k -> execAtomically time tid tlbl nextVid (runSTM a) $ \res ->
871881
case res of
872882
StmTxCommitted x written nextVid' -> do
873883
(wakeup, wokeby) <- threadsUnblockedByWrites written
@@ -897,13 +907,13 @@ schedule thread@Thread{
897907
-- schedule this thread to immediately raise the exception
898908
let thread' = thread { threadControl = ThreadControl (Throw e) ctl }
899909
trace <- schedule thread' simstate
900-
return (Trace time tid tlbl EventTxAborted trace)
910+
return $ Trace time tid tlbl EventTxAborted trace
901911

902912
StmTxBlocked read -> do
903913
mapM_ (\(SomeTVar tvar) -> blockThreadOnTVar tid tvar) read
904914
vids <- traverse (\(SomeTVar tvar) -> labelledTVarId tvar) read
905915
trace <- deschedule Blocked thread simstate
906-
return (Trace time tid tlbl (EventTxBlocked vids) trace)
916+
return $ Trace time tid tlbl (EventTxBlocked vids) trace
907917

908918
GetThreadId k -> do
909919
let thread' = thread { threadControl = ThreadControl (k tid) ctl }
@@ -1278,12 +1288,16 @@ data StmStack s b a where
12781288
-> StmStack s b c
12791289
-> StmStack s a c
12801290

1281-
execAtomically :: forall s a.
1282-
TVarId
1291+
execAtomically :: forall s a c.
1292+
Time
1293+
-> ThreadId
1294+
-> Maybe ThreadLabel
1295+
-> TVarId
12831296
-> StmA s a
1284-
-> ST s (StmTxResult s a)
1285-
execAtomically =
1286-
go AtomicallyFrame Map.empty Map.empty []
1297+
-> (StmTxResult s a -> ST s (Trace c))
1298+
-> ST s (Trace c)
1299+
execAtomically time tid tlbl nextVid0 action0 k0 =
1300+
go AtomicallyFrame Map.empty Map.empty [] nextVid0 action0
12871301
where
12881302
go :: forall b.
12891303
StmStack s b a
@@ -1292,7 +1306,7 @@ execAtomically =
12921306
-> [SomeTVar s] -- vars written in order (no dups)
12931307
-> TVarId -- var fresh name supply
12941308
-> StmA s b
1295-
-> ST s (StmTxResult s a)
1309+
-> ST s (Trace c)
12961310
go ctl !read !written writtenSeq !nextVid action = assert localInvariant $
12971311
case action of
12981312
ReturnStm x -> case ctl of
@@ -1307,7 +1321,7 @@ execAtomically =
13071321
) written
13081322

13091323
-- Return the vars written, so readers can be unblocked
1310-
return (StmTxCommitted x (reverse writtenSeq) nextVid)
1324+
k0 $ StmTxCommitted x (reverse writtenSeq) nextVid
13111325

13121326
OrElseLeftFrame _b k writtenOuter writtenOuterSeq ctl' -> do
13131327
-- Commit the TVars written in this sub-transaction that are also
@@ -1340,14 +1354,14 @@ execAtomically =
13401354
ThrowStm e -> do
13411355
-- Revert all the TVar writes
13421356
traverse_ (\(SomeTVar tvar) -> revertTVar tvar) written
1343-
return (StmTxAborted (toException e))
1357+
k0 $ StmTxAborted (toException e)
13441358

13451359
Retry -> case ctl of
13461360
AtomicallyFrame -> do
13471361
-- Revert all the TVar writes
13481362
traverse_ (\(SomeTVar tvar) -> revertTVar tvar) written
13491363
-- Return vars read, so the thread can block on them
1350-
return (StmTxBlocked (Map.elems read))
1364+
k0 $ StmTxBlocked (Map.elems read)
13511365

13521366
OrElseLeftFrame b k writtenOuter writtenOuterSeq ctl' -> do
13531367
-- Revert all the TVar writes within this orElse
@@ -1394,6 +1408,15 @@ execAtomically =
13941408
execWriteTVar v x
13951409
let written' = Map.insert (tvarId v) (SomeTVar v) written
13961410
go ctl read written' (SomeTVar v : writtenSeq) nextVid k
1411+
1412+
SayStm msg k -> do
1413+
trace <- go ctl read written writtenSeq nextVid k
1414+
return $ Trace time tid tlbl (EventSay msg) trace
1415+
1416+
OutputStm x k -> do
1417+
trace <- go ctl read written writtenSeq nextVid k
1418+
return $ Trace time tid tlbl (EventLog x) trace
1419+
13971420
where
13981421
localInvariant =
13991422
Map.keysSet written

0 commit comments

Comments
 (0)