1 --!!! Testing the MVar primitives
3 -- I quickly converted some of this code to work in the new system.
4 -- Many of the rest haven't been updated or tested much and you'll
5 -- find that the claims about what they "should print" are wrong
6 -- being based on the old Hugs behaviour instead of assuming an
7 -- arbitrary interleaving.
11 module TestMVar(test1,test2,test3,test4,test5,test6,test7,test8) where
15 -- should print "a" then deadlock
30 p1 v = do { put v 'a'; get v }
31 p2 v = do { get v ; put v 'b' }
35 newEmptyMVar >>= \ v ->
40 -- NB: it's important that p1 is called from the main thread to make sure
41 -- that the final get is executed
49 p1 v1 v2 = do { put v1 'a'; get v2 }
50 p2 v1 v2 = do { get v1 ; put v2 'b' }
52 -- should abort: primPutMVar: full MVar
54 newEmptyMVar >>= \ v ->
58 -- test blocking of two processes on the same variable.
63 ; forkIO (get x >> put ack 'X')
64 ; forkIO (get x >> put ack 'X')
65 ; put x 'a' >> get ack -- use up one reader
66 ; put x 'b' >> get ack -- use up the other
67 ; put x 'c' >> get ack -- deadlock
70 ----------------------------------------------------------------
71 -- Non-deterministic tests below this point
72 -- Must be tested interactively and probably don't work using
73 -- "logical concurrency".
76 -- should print interleaving of a's and b's
77 -- (degree of interleaving depends on granularity of concurrency)
81 a = putStr "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
82 b = putStr "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
84 -- should give infinite interleaving of a's and b's
85 -- (degree of interleaving depends on granularity of concurrency)
86 -- Ming's example. The Hugs read-eval-print loop gets confused if
87 -- there's no type signature
92 -- symbols carefully chosen to make them look very different on screen
96 -- test blocking of two processes on the same variable.
97 -- may print "aXbY{Deadlock}" or "aYbX{Deadlock}"
100 ; ack <- newEmptyMVar
101 ; forkIO (get x >> put ack 'X')
102 ; forkIO (get x >> put ack 'Y')
103 ; put x 'a' >> get ack -- use up one reader
104 ; put x 'b' >> get ack -- use up the other
105 ; put x 'c' >> get ack -- deadlock
112 takeMVar v >>= \ x ->