2015-02-16 05:50:52 +03:00
|
|
|
{-# LANGUAGE ImpredicativeTypes #-}
|
|
|
|
|
2014-12-24 01:01:00 +03:00
|
|
|
-- | Tests sourced from <https://github.com/sctbenchmarks>.
|
2014-12-23 18:20:11 +03:00
|
|
|
module Tests.Cases where
|
|
|
|
|
2015-01-28 20:48:26 +03:00
|
|
|
import Control.Concurrent.CVar
|
2015-02-15 02:45:14 +03:00
|
|
|
import Control.Exception (ArithException(..), ArrayException)
|
2015-02-15 02:55:58 +03:00
|
|
|
import Control.Monad (liftM, replicateM)
|
2014-12-23 18:20:11 +03:00
|
|
|
import Control.Monad.Conc.Class
|
2015-02-15 02:55:58 +03:00
|
|
|
import Control.Monad.STM.Class
|
2014-12-23 18:20:11 +03:00
|
|
|
|
|
|
|
-- | Should deadlock on a minority of schedules.
|
2015-01-28 15:10:19 +03:00
|
|
|
simple2Deadlock :: MonadConc m => m Int
|
2014-12-23 18:20:11 +03:00
|
|
|
simple2Deadlock = do
|
|
|
|
a <- newEmptyCVar
|
|
|
|
b <- newEmptyCVar
|
|
|
|
|
|
|
|
c <- newCVar 0
|
|
|
|
|
|
|
|
j1 <- spawn $ lock a >> lock b >> modifyCVar_ c (return . succ) >> unlock b >> unlock a
|
|
|
|
j2 <- spawn $ lock b >> lock a >> modifyCVar_ c (return . pred) >> unlock a >> unlock b
|
|
|
|
|
|
|
|
takeCVar j1
|
|
|
|
takeCVar j2
|
|
|
|
|
|
|
|
takeCVar c
|
2014-12-23 18:50:25 +03:00
|
|
|
|
|
|
|
-- | Dining philosophers problem, result is irrelevent, we just want
|
|
|
|
-- deadlocks.
|
2015-01-28 15:10:19 +03:00
|
|
|
philosophers :: MonadConc m => Int -> m ()
|
2014-12-23 18:50:25 +03:00
|
|
|
philosophers n = do
|
|
|
|
forks <- replicateM n newEmptyCVar
|
|
|
|
let phils = map (\(i,p) -> p i forks) $ zip [0..] $ replicate n philosopher
|
|
|
|
cvars <- mapM spawn phils
|
|
|
|
mapM_ takeCVar cvars
|
|
|
|
|
|
|
|
where
|
|
|
|
philosopher ident forks = do
|
|
|
|
let leftId = ident
|
|
|
|
let rightId = (ident + 1) `mod` length forks
|
|
|
|
lock $ forks !! leftId
|
|
|
|
lock $ forks !! rightId
|
|
|
|
-- In the traditional approach, we'd wait for a random time
|
|
|
|
-- here, but we want the only source of (important)
|
|
|
|
-- nondeterminism to come from the scheduler, which it does, as
|
|
|
|
-- pre-emption is effectively a delay.
|
|
|
|
unlock $ forks !! leftId
|
|
|
|
unlock $ forks !! rightId
|
2014-12-24 01:01:00 +03:00
|
|
|
|
|
|
|
-- | Checks if a value has been increased above a threshold, data
|
|
|
|
-- racey.
|
2015-01-28 15:10:19 +03:00
|
|
|
thresholdValue :: MonadConc m => m Bool
|
2014-12-24 01:01:00 +03:00
|
|
|
thresholdValue = do
|
|
|
|
l <- newEmptyCVar
|
2015-02-16 06:42:29 +03:00
|
|
|
x <- newCVar (0::Int)
|
2014-12-24 01:01:00 +03:00
|
|
|
|
|
|
|
fork $ lock l >> modifyCVar_ x (return . (+1)) >> unlock l
|
|
|
|
fork $ lock l >> modifyCVar_ x (return . (+2)) >> unlock l
|
2015-01-23 18:11:56 +03:00
|
|
|
res <- spawn $ lock l >> readCVar x >>= \x' -> unlock l >> return (x' >= 3)
|
2014-12-24 01:01:00 +03:00
|
|
|
|
|
|
|
takeCVar res
|
|
|
|
|
|
|
|
-- | A lock taken but never released.
|
2015-01-28 15:10:19 +03:00
|
|
|
forgottenUnlock :: MonadConc m => m ()
|
2014-12-24 01:01:00 +03:00
|
|
|
forgottenUnlock = do
|
|
|
|
l <- newEmptyCVar
|
|
|
|
m <- newEmptyCVar
|
|
|
|
|
|
|
|
let lockl = lock l >> unlock l >> lock l >> lock m >> unlock m >> lock m >> unlock m
|
|
|
|
|
|
|
|
j1 <- spawn lockl
|
|
|
|
j2 <- spawn lockl
|
|
|
|
|
|
|
|
takeCVar j1
|
|
|
|
takeCVar j2
|
|
|
|
|
|
|
|
-- | Very simple data race between two threads.
|
2015-01-28 15:10:19 +03:00
|
|
|
simple2Race :: MonadConc m => m Int
|
2014-12-24 01:01:00 +03:00
|
|
|
simple2Race = do
|
|
|
|
x <- newEmptyCVar
|
|
|
|
|
|
|
|
fork $ putCVar x 0
|
|
|
|
fork $ putCVar x 1
|
|
|
|
|
|
|
|
readCVar x
|
2014-12-24 16:15:46 +03:00
|
|
|
|
|
|
|
-- | Race on popping from a stack.
|
2015-01-28 15:10:19 +03:00
|
|
|
raceyStack :: MonadConc m => m (Maybe Int)
|
2014-12-24 16:15:46 +03:00
|
|
|
raceyStack = do
|
|
|
|
s <- newCVar []
|
|
|
|
|
|
|
|
fork $ t1 s [1..10]
|
2015-02-16 06:42:29 +03:00
|
|
|
j <- spawn $ t2 s (10::Int) 0
|
2014-12-24 16:15:46 +03:00
|
|
|
|
|
|
|
takeCVar j
|
|
|
|
|
|
|
|
where
|
|
|
|
push s a = modifyCVar_ s $ return . (a:)
|
|
|
|
pop s = do
|
|
|
|
val <- takeCVar s
|
|
|
|
case val of
|
|
|
|
[] -> putCVar s [] >> return Nothing
|
|
|
|
(x:xs) -> putCVar s xs >> return (Just x)
|
|
|
|
|
|
|
|
t1 s (x:xs) = push s x >> t1 s xs
|
|
|
|
t1 _ [] = return ()
|
|
|
|
|
|
|
|
t2 _ 0 total = return $ Just total
|
|
|
|
t2 s n total = do
|
|
|
|
val <- pop s
|
|
|
|
case val of
|
|
|
|
Just x -> t2 s (n-1) (total+x)
|
2015-01-09 05:35:28 +03:00
|
|
|
Nothing -> return Nothing
|
2015-02-15 02:45:14 +03:00
|
|
|
|
|
|
|
-- | Cause a deadlock sometimes by killing a thread.
|
|
|
|
threadKill :: MonadConc m => m ()
|
|
|
|
threadKill = do
|
|
|
|
x <- newEmptyCVar
|
|
|
|
tid <- fork $ putCVar x ()
|
|
|
|
killThread tid
|
|
|
|
readCVar x
|
|
|
|
|
|
|
|
-- | Never deadlock by masking a thread.
|
|
|
|
threadKillMask :: MonadConc m => m ()
|
|
|
|
threadKillMask = do
|
|
|
|
x <- newEmptyCVar
|
|
|
|
y <- newEmptyCVar
|
|
|
|
tid <- fork . mask . const $ putCVar x () >> putCVar y ()
|
|
|
|
readCVar x
|
|
|
|
killThread tid
|
|
|
|
readCVar y
|
|
|
|
|
|
|
|
-- | Test nested exception handlers.
|
|
|
|
excNest :: MonadConc m => m Int
|
|
|
|
excNest =
|
2015-02-16 06:42:29 +03:00
|
|
|
Control.Monad.Conc.Class.catch
|
|
|
|
(Control.Monad.Conc.Class.catch
|
|
|
|
(throw Overflow)
|
|
|
|
(\e -> return . const 1 $ (e :: ArrayException)))
|
|
|
|
(\e -> return . const 2 $ (e :: ArithException))
|
2015-02-15 02:45:14 +03:00
|
|
|
|
|
|
|
-- | Test unmasking exceptions
|
|
|
|
threadKillUmask :: MonadConc m => m ()
|
|
|
|
threadKillUmask = do
|
|
|
|
x <- newEmptyCVar
|
|
|
|
y <- newEmptyCVar
|
|
|
|
tid <- fork . mask $ \umask -> putCVar x () >> umask (putCVar y ())
|
|
|
|
readCVar x
|
|
|
|
killThread tid
|
|
|
|
readCVar y
|
2015-02-15 02:55:58 +03:00
|
|
|
|
|
|
|
-- | Test atomicity of STM.
|
|
|
|
stmAtomic :: MonadConc m => m Int
|
|
|
|
stmAtomic = do
|
2015-02-16 05:50:52 +03:00
|
|
|
x <- atomically $ newCTVar (0::Int)
|
2015-07-08 20:18:18 +03:00
|
|
|
fork . atomically $ writeCTVar x 1 >> writeCTVar x 2
|
2015-02-15 02:55:58 +03:00
|
|
|
atomically $ readCTVar x
|
|
|
|
|
|
|
|
-- | Test STM retry
|
|
|
|
stmRetry :: MonadConc m => m Bool
|
|
|
|
stmRetry = do
|
2015-02-16 05:50:52 +03:00
|
|
|
x <- atomically $ newCTVar (0::Int)
|
2015-02-15 02:55:58 +03:00
|
|
|
fork . atomically $ writeCTVar x 1 >> retry
|
|
|
|
(==0) `liftM` atomically (readCTVar x)
|
|
|
|
|
|
|
|
-- | Test STM orElse
|
|
|
|
stmOrElse :: MonadConc m => m Bool
|
|
|
|
stmOrElse = do
|
2015-02-16 05:50:52 +03:00
|
|
|
x <- atomically $ newCTVar (0::Int)
|
2015-02-15 02:55:58 +03:00
|
|
|
atomically $ (writeCTVar x 1 >> retry) `orElse` writeCTVar x 2
|
|
|
|
(==2) `liftM` atomically (readCTVar x)
|
|
|
|
|
|
|
|
-- | Test STM exceptions
|
|
|
|
stmExc :: MonadConc m => m Bool
|
|
|
|
stmExc = do
|
2015-02-16 05:50:52 +03:00
|
|
|
x <- atomically $ newCTVar (0::Int)
|
2015-02-15 02:55:58 +03:00
|
|
|
atomically $ writeCTVar x 1 >> throwSTM Overflow
|
|
|
|
(==0) `liftM` atomically (readCTVar x)
|