conc015.hs 1.28 KB
Newer Older
1
2
import Control.Concurrent
import Control.Exception
3
4
5
6
7
8
9
10
11
12
13
14

-- test blocking & unblocking of async exceptions.

-- the first exception "foo" should be caught by the "caught1" handler,
-- since async exceptions are blocked outside this handler.

-- the second exception "bar" should be caught by the outer "caught2" handler,
-- (i.e. this tests that async exceptions are properly unblocked after
-- being blocked).

main = do
  main_thread <- myThreadId
Simon Marlow's avatar
Simon Marlow committed
15
  print =<< blocked
16
17
18
19
20
21
22
23
24
25
  m <- newEmptyMVar
  m2 <- newEmptyMVar
  forkIO (do takeMVar m
	     throwTo main_thread (ErrorCall "foo")
	     throwTo main_thread (ErrorCall "bar") 
	     putMVar m2 ()
	 )
  ( do
    block (do
	putMVar m ()
Simon Marlow's avatar
Simon Marlow committed
26
        print =<< blocked
27
	sum [1..100000] `seq` -- give 'foo' a chance to be raised
Simon Marlow's avatar
Simon Marlow committed
28
  	  (unblock (do print =<< blocked; myDelay 500000))
29
30
		`Control.Exception.catch` 
                    \e -> putStrLn ("caught1: " ++ show (e::SomeException))
31
     )
32
    threadDelay 10000
33
34
    takeMVar m2
   )
sof's avatar
sof committed
35
    `Control.Exception.catch`
36
37
       \e -> do print =<< blocked
                putStrLn ("caught2: " ++ show (e::SomeException))
Simon Marlow's avatar
Simon Marlow committed
38
39
40
41
42
43
44

-- compensate for the fact that threadDelay is non-interruptible
-- on Windows with the threaded RTS in 6.6.
myDelay usec = do
  m <- newEmptyMVar
  forkIO $ do threadDelay usec; putMVar m ()
  takeMVar m