File: interrupt.hs

package info (click to toggle)
haskell-filelock 0.1.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 88 kB
  • sloc: haskell: 148; makefile: 2
file content (24 lines) | stat: -rw-r--r-- 748 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import Control.Concurrent
import Control.Exception
import Control.Monad
import System.FileLock
import System.Exit
import System.Timeout

main :: IO ()
main = withFileLock lockFilePath Exclusive $ \_ -> do
  mvar <- newMVar Nothing
  _ <- forkIO $ do
    -- The attempt to lock the file again should block, but it should be
    -- interrupted by the timeout, returning Nothing.
    --
    -- Also masking shouldn't change interruptibility.
    r <- timeout 1000000 $ mask $ \_ -> lockFile lockFilePath Exclusive
    _ <- swapMVar mvar (Just r)
    return ()
  threadDelay 2000000
  res <- readMVar mvar
  when (res /= Just Nothing) $
    die $ "unexpected result: " ++ show (fmap (const ()) <$> res)
  where
    lockFilePath = "interrupt_test.lock"