@@ -0,0 +1,24 @@
1+module Network.Wai.Handler.Warp.Counter (
2+ Counter
3+ , newCounter
4+ , isZero
5+ , increase
6+ , decrease
7+ ) where
8+
9+import Network.Wai.Handler.Warp.IORef
10+import Control.Applicative ((<$>))
11+
12+newtype Counter = Counter (IORef Int)
13+
14+newCounter :: IO Counter
15+newCounter = Counter <$> newIORef 0
16+
17+isZero :: Counter -> IO Bool
18+isZero (Counter ref) = (== 0) <$> readIORef ref
19+
20+increase :: Counter -> IO ()
21+increase (Counter ref) = atomicModifyIORef' ref $ \x -> (x + 1, ())
22+
23+decrease :: Counter -> IO ()
24+decrease (Counter ref) = atomicModifyIORef' ref $ \x -> (x - 1, ())