|
|
1
|
+{-# LANGUAGE MagicHash #-}
|
|
|
2
|
+{-# LANGUAGE UnliftedFFITypes #-}
|
|
|
3
|
+
|
|
|
4
|
+module Main where
|
|
|
5
|
+
|
|
|
6
|
+import Control.Concurrent
|
|
|
7
|
+import Control.Monad
|
|
|
8
|
+import Foreign.C.Types
|
|
|
9
|
+import GHC.Conc.Sync (ThreadId(..), forkOn, myThreadId, setNumCapabilities)
|
|
|
10
|
+import GHC.Exts (ThreadId#)
|
|
|
11
|
+
|
|
|
12
|
+foreign import ccall unsafe "rts_enableStopNextBreakpoint"
|
|
|
13
|
+ rts_enableStopNextBreakpoint :: ThreadId# -> IO ()
|
|
|
14
|
+
|
|
|
15
|
+foreign import ccall unsafe "rts_disableStopNextBreakpoint"
|
|
|
16
|
+ rts_disableStopNextBreakpoint :: ThreadId# -> IO ()
|
|
|
17
|
+
|
|
|
18
|
+foreign import ccall unsafe "rts_enableStopAfterReturn"
|
|
|
19
|
+ rts_enableStopAfterReturn :: ThreadId# -> IO ()
|
|
|
20
|
+
|
|
|
21
|
+foreign import ccall unsafe "rts_disableStopAfterReturn"
|
|
|
22
|
+ rts_disableStopAfterReturn :: ThreadId# -> IO ()
|
|
|
23
|
+
|
|
|
24
|
+foreign import ccall unsafe "has_local_stop_next_breakpoint"
|
|
|
25
|
+ c_hasLocalStopNextBreakpoint :: IO CInt
|
|
|
26
|
+
|
|
|
27
|
+foreign import ccall unsafe "has_local_stop_after_return"
|
|
|
28
|
+ c_hasLocalStopAfterReturn :: IO CInt
|
|
|
29
|
+
|
|
|
30
|
+main :: IO ()
|
|
|
31
|
+main = do
|
|
|
32
|
+ setNumCapabilities 2
|
|
|
33
|
+ checkFlag
|
|
|
34
|
+ "TSO_STOP_NEXT_BREAKPOINT"
|
|
|
35
|
+ rts_enableStopNextBreakpoint
|
|
|
36
|
+ rts_disableStopNextBreakpoint
|
|
|
37
|
+ c_hasLocalStopNextBreakpoint
|
|
|
38
|
+ checkFlag
|
|
|
39
|
+ "TSO_STOP_AFTER_RETURN"
|
|
|
40
|
+ rts_enableStopAfterReturn
|
|
|
41
|
+ rts_disableStopAfterReturn
|
|
|
42
|
+ c_hasLocalStopAfterReturn
|
|
|
43
|
+
|
|
|
44
|
+checkFlag
|
|
|
45
|
+ :: String
|
|
|
46
|
+ -> (ThreadId# -> IO ())
|
|
|
47
|
+ -> (ThreadId# -> IO ())
|
|
|
48
|
+ -> IO CInt
|
|
|
49
|
+ -> IO ()
|
|
|
50
|
+checkFlag label enable disable isMyThreadFlagSet = do
|
|
|
51
|
+ -- Print the main thread's capability (should be 0)
|
|
|
52
|
+ print =<< threadCapability =<< myThreadId
|
|
|
53
|
+
|
|
|
54
|
+ -- Target thread will write its own flag value here
|
|
|
55
|
+ targetCheckVar <- newEmptyMVar
|
|
|
56
|
+
|
|
|
57
|
+ -- Run the new TSO runs on capability 1
|
|
|
58
|
+ ThreadId tid# <- forkOn 1 $ do
|
|
|
59
|
+ replicateM_ 2 $ do
|
|
|
60
|
+ replyVar <- takeMVar targetCheckVar
|
|
|
61
|
+ isSet <- (/= 0) <$> isMyThreadFlagSet
|
|
|
62
|
+ putMVar replyVar isSet
|
|
|
63
|
+
|
|
|
64
|
+ -- Enable the other TSO's flag
|
|
|
65
|
+ enable tid#
|
|
|
66
|
+ -- It will check whether it is set and reply here
|
|
|
67
|
+ renderCheck label "set" =<< checkTarget targetCheckVar
|
|
|
68
|
+
|
|
|
69
|
+ -- Ditto.
|
|
|
70
|
+ disable tid#
|
|
|
71
|
+ renderCheck label "unset" . not =<< checkTarget targetCheckVar
|
|
|
72
|
+
|
|
|
73
|
+checkTarget :: MVar (MVar Bool) -> IO Bool
|
|
|
74
|
+checkTarget targetCheckVar = do
|
|
|
75
|
+ replyVar <- newEmptyMVar
|
|
|
76
|
+ putMVar targetCheckVar replyVar
|
|
|
77
|
+ takeMVar replyVar
|
|
|
78
|
+
|
|
|
79
|
+renderCheck :: String -> String -> Bool -> IO ()
|
|
|
80
|
+renderCheck label state ok = putStrLn $
|
|
|
81
|
+ label ++ " " ++ state ++ ": " ++ if ok then "ok" else "failed" |