summaryrefslogtreecommitdiff
path: root/testsuite/tests/concurrent/should_run/throwto002.hs
blob: eaaae0c0cbf5c0b3be363ab3148df877ccdebe92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{-# LANGUAGE RecursiveDo, ScopedTypeVariables #-}
import Control.Concurrent
import Control.Exception
import Data.Array
import System.Environment
import Control.Monad
import GHC.Conc
import Data.IORef

main = do
  r <- newIORef 0
  rec
    t1 <- mask $ \restore -> forkIO (thread restore r t2)
    t2 <- mask $ \restore -> forkIO (thread restore r t1)
  threadDelay 1000000
  readIORef r >>= print . (/= 0)

thread restore r t = run
  where
    run = (restore $ forever $ do killThread t
                                  i <- atomicModifyIORef r (\i -> (i + 1, i))
                                  evaluate i)
             `catch` \(e::SomeException) -> run