Skip to content

Commit aeb7532

Browse files
committed
Added threadLabel to MonadThread
Not adding `listThreads` as it's not very well specified in `IO`: > primop ListThreadsOp "listThreads#" GenPrimOp > State# RealWorld -> (# State# RealWorld, Array# ThreadId# #) > { Returns an array of the threads started by the program. Note that this > threads which have finished execution may or may not be present in this > list, depending upon whether they have been collected by the garbage collector.
1 parent de8d691 commit aeb7532

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

io-classes/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Revsion history of io-classes
22

3+
### next version
4+
5+
### Breaking changes
6+
7+
* Added `threadLabel` to `MonadThread`
8+
39
### 1.7.0.0
410

511
### Breaking changes

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE FlexibleContexts #-}
23
{-# LANGUAGE RankNTypes #-}
34
{-# LANGUAGE ScopedTypeVariables #-}
@@ -18,7 +19,7 @@ import Control.Exception (AsyncException (ThreadKilled), Exception,
1819
SomeException)
1920
import Control.Monad.Reader (ReaderT (..), lift)
2021
import Data.Kind (Type)
21-
import GHC.Conc.Sync qualified as IO (labelThread)
22+
import GHC.Conc.Sync qualified as IO
2223

2324

2425
class (Monad m, Eq (ThreadId m),
@@ -30,6 +31,11 @@ class (Monad m, Eq (ThreadId m),
3031
myThreadId :: m (ThreadId m)
3132
labelThread :: ThreadId m -> String -> m ()
3233

34+
-- | Requires ghc-9.6.1 or newer.
35+
--
36+
-- @since 1.8.0.0
37+
threadLabel :: ThreadId m -> m (Maybe String)
38+
3339
-- | Apply the label to the current thread
3440
labelThisThread :: MonadThread m => String -> m ()
3541
labelThisThread label = myThreadId >>= \tid -> labelThread tid label
@@ -53,6 +59,11 @@ instance MonadThread IO where
5359
type ThreadId IO = IO.ThreadId
5460
myThreadId = IO.myThreadId
5561
labelThread = IO.labelThread
62+
#if MIN_VERSION_base(4,18,0)
63+
threadLabel = IO.threadLabel
64+
#else
65+
threadLabel = \_ -> pure Nothing
66+
#endif
5667

5768
instance MonadFork IO where
5869
forkIO = IO.forkIO
@@ -67,6 +78,7 @@ instance MonadThread m => MonadThread (ReaderT r m) where
6778
type ThreadId (ReaderT r m) = ThreadId m
6879
myThreadId = lift myThreadId
6980
labelThread t l = lift (labelThread t l)
81+
threadLabel = lift . threadLabel
7082

7183
instance MonadFork m => MonadFork (ReaderT e m) where
7284
forkIO (ReaderT f) = ReaderT $ \e -> forkIO (f e)

io-sim/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Revision history of io-sim
22

3+
## next version
4+
5+
- Support `threadLabel` (`io-classes-1.8`)
6+
37
## 1.6.0.0
48

59
- Upgraded to `io-classes-1.6.0.0`

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,13 @@ schedule !thread@Thread{
547547
, threadLabel = Just l }
548548
schedule thread' simstate
549549

550+
GetThreadLabel tid' k -> do
551+
let tlbl' | tid' == tid = tlbl
552+
| otherwise = tid' `Map.lookup` threads
553+
>>= threadLabel
554+
thread' = thread { threadControl = ThreadControl (k tlbl') ctl }
555+
schedule thread' simstate
556+
550557
LabelThread tid' l k -> do
551558
let thread' = thread { threadControl = ThreadControl k ctl }
552559
threads' = Map.adjust (\t -> t { threadLabel = Just l }) tid' threads

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,10 @@ data SimA s a where
186186
SimA s a -> (e -> SimA s a) -> (a -> SimA s b) -> SimA s b
187187
Evaluate :: a -> (a -> SimA s b) -> SimA s b
188188

189-
Fork :: IOSim s () -> (IOSimThreadId -> SimA s b) -> SimA s b
190-
GetThreadId :: (IOSimThreadId -> SimA s b) -> SimA s b
191-
LabelThread :: IOSimThreadId -> String -> SimA s b -> SimA s b
189+
Fork :: IOSim s () -> (IOSimThreadId -> SimA s b) -> SimA s b
190+
GetThreadId :: (IOSimThreadId -> SimA s b) -> SimA s b
191+
LabelThread :: IOSimThreadId -> String -> SimA s b -> SimA s b
192+
GetThreadLabel :: IOSimThreadId -> (Maybe String -> SimA s b) -> SimA s b
192193

193194
Atomically :: STM s a -> (a -> SimA s b) -> SimA s b
194195

@@ -469,6 +470,7 @@ instance MonadThread (IOSim s) where
469470
type ThreadId (IOSim s) = IOSimThreadId
470471
myThreadId = IOSim $ oneShot $ \k -> GetThreadId k
471472
labelThread t l = IOSim $ oneShot $ \k -> LabelThread t l (k ())
473+
threadLabel t = IOSim $ oneShot $ \k -> GetThreadLabel t k
472474

473475
instance MonadFork (IOSim s) where
474476
forkIO task = IOSim $ oneShot $ \k -> Fork task k
@@ -815,10 +817,8 @@ ppSimEvent timeWidth tidWidth tLabelWidth SimEvent {seTime = Time time, seThread
815817
tidWidth
816818
(ppIOSimThreadId seThreadId)
817819
tLabelWidth
818-
threadLabel
820+
(fromMaybe "" seThreadLabel)
819821
(ppSimEventType seType)
820-
where
821-
threadLabel = fromMaybe "" seThreadLabel
822822

823823
ppSimEvent timeWidth tidWidth tLableWidth SimPOREvent {seTime = Time time, seThreadId, seStep, seThreadLabel, seType} =
824824
printf "%-*s - %-*s %-*s - %s"
@@ -827,10 +827,8 @@ ppSimEvent timeWidth tidWidth tLableWidth SimPOREvent {seTime = Time time, seThr
827827
tidWidth
828828
(ppStepId (seThreadId, seStep))
829829
tLableWidth
830-
threadLabel
830+
(fromMaybe "" seThreadLabel)
831831
(ppSimEventType seType)
832-
where
833-
threadLabel = fromMaybe "" seThreadLabel
834832

835833
ppSimEvent _ _ _ (SimRacesFound controls) =
836834
"RacesFound "++show controls

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,13 @@ schedule thread@Thread{
703703
, threadLabel = Just l }
704704
schedule thread' simstate
705705

706+
GetThreadLabel tid' k -> do
707+
let tlbl' | tid' == tid = tlbl
708+
| otherwise = tid' `Map.lookup` threads
709+
>>= threadLabel
710+
thread' = thread { threadControl = ThreadControl (k tlbl') ctl }
711+
schedule thread' simstate
712+
706713
LabelThread tid' l k -> do
707714
let thread' = thread { threadControl = ThreadControl k ctl }
708715
threads' = Map.adjust (\t -> t { threadLabel = Just l }) tid' threads

0 commit comments

Comments
 (0)