/
example2.hs
70 lines (56 loc) · 1.75 KB
/
example2.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
-- example2
import Data.IORef
import Control.Applicative
import Control.Monad
import Control.Exception (SomeException)
import qualified Control.Exception as E
import Control.Concurrent (threadDelay)
import System.IO.Unsafe (unsafePerformIO)
import System.Timeout (timeout)
{-# NOINLINE numHandles #-}
numHandles :: IORef Int
numHandles = unsafePerformIO $ newIORef 0
{-# NOINLINE dataWritten #-}
dataWritten :: IORef [String]
dataWritten = unsafePerformIO $ newIORef []
test :: IO () -> IO ()
test action = do
action `E.catch` \e -> do
putStrLn $ "exception: " ++ show (e :: SomeException)
readIORef numHandles >>= putStrLn . ("Number of open handles: " ++) . show
readIORef dataWritten >>= putStrLn . ("Data writtern to file: " ++) . show
data Handle = Handle (IORef (Maybe String))
openFile :: FilePath -> IO Handle
openFile _ = do
modifyIORef' numHandles succ
Handle <$> newIORef Nothing
hClose :: Handle -> IO ()
hClose h = hFlushFailing h
`E.finally` modifyIORef numHandles pred
hFlushFailing :: Handle -> IO ()
hFlushFailing _ = error "hFlush failed"
hFlush :: Handle -> IO ()
hFlush (Handle ref) = do
val <- readIORef ref
case val of
Just str -> modifyIORef dataWritten (str :)
_ -> return ()
writeIORef ref Nothing
hPutStr :: Handle -> String -> IO ()
hPutStr h@(Handle ref) str = do
hFlush h
writeIORef ref (Just str)
bracket :: IO a -> (a -> IO ()) -> (a -> IO b) -> IO b
bracket allocate release use =
E.mask $ \restore -> do
resource <- allocate
restore (use resource)
`E.finally` release resource
example :: IO ()
example = void $ timeout (1 * 1000 * 1000) $
bracket (openFile "path") hClose $ \h -> do
hPutStr h "Hello"
hPutStr h "World"
threadDelay (2 * 1000 * 1000)
main :: IO ()
main = test example