on master help sign in
refactoring and documentation.
commitf6289fc4bb975da91491f914c62b25564cf88ab1 authorKazu Yamamoto <kazu@iij.ad.jp> dateThu 10 Oct 2013, 17:18 (13 years ago) parent536b8bf
1 file changed+32−4
warp/Network/Wai/Handler/Warp/Timeout.hs+32−4
warp/Network/Wai/Handler/Warp/Timeout.hs+32−4
@@ -34,6 +34,8 @@ import Data.Typeable (Typeable)
3434import System.IO.Unsafe (unsafePerformIO)
3535import System.Mem.Weak (deRefWeak)
3636
37+----------------------------------------------------------------
38+
3739-- | A timeout manager
3840newtype Manager = Manager (IORef [Handle])
3941
@@ -42,12 +44,18 @@ newtype Manager = Manager (IORef [Handle])
4244-- First field is action to be performed on timeout.
4345data Handle = Handle (IO ()) (IORef State)
4446
47+data State = Active | Inactive | Paused | Canceled
48+
49+----------------------------------------------------------------
50+
4551-- | A dummy @Handle@.
4652dummyHandle :: Handle
4753dummyHandle = Handle (return ()) (unsafePerformIO $ I.newIORef Active)
4854
49−data State = Active | Inactive | Paused | Canceled
55+----------------------------------------------------------------
5056
57+-- | Creating timeout manager which works every N micro seconds
58+-- where N is the first argument.
5159initialize :: Int -> IO Manager
5260initialize timeout = do
5361 ref <- I.newIORef []
@@ -88,6 +96,9 @@ stopManager (Manager ihandles) = E.mask_ $ do
8896ignoreAll :: E.SomeException -> IO ()
8997ignoreAll _ = return ()
9098
99+----------------------------------------------------------------
100+
101+-- | Registering a timeout action.
91102register :: Manager -> IO () -> IO Handle
92103register (Manager ref) onTimeout = do
93104 iactive <- I.newIORef Active
@@ -95,6 +106,7 @@ register (Manager ref) onTimeout = do
95106 I.atomicModifyIORef ref (\x -> (h : x, ()))
96107 return h
97108
109+-- | Registering a timeout action of killing this thread.
98110registerKillThread :: Manager -> IO Handle
99111registerKillThread m = do
100112 wtid <- myThreadId >>= mkWeakThreadId
@@ -107,12 +119,28 @@ mkWeakThreadId t@(ThreadId t#) = IO $ \s ->
107119 (# s1, w #) -> (# s1, Weak w #)
108120#endif
109121
110−tickle, pause, resume, cancel :: Handle -> IO ()
111−tickle (Handle _ iactive) = I.writeIORef iactive Active
122+----------------------------------------------------------------
123+
124+-- | Setting the state to active.
125+-- 'Manager' turns active to inactive repeatedly.
126+resume :: Handle -> IO ()
127+resume (Handle _ iactive) = I.writeIORef iactive Active
128+
129+tickle :: Handle -> IO ()
130+tickle = resume
131+
132+-- | Setting the state to paused.
133+-- 'Manager' does not change the value.
134+pause :: Handle -> IO ()
112135pause (Handle _ iactive) = I.writeIORef iactive Paused
113−resume = tickle
136+
137+-- | Setting the state to canceled.
138+-- 'Manager' eventually removes this without timeout action.
139+cancel :: Handle -> IO ()
114140cancel (Handle _ iactive) = I.writeIORef iactive Canceled
115141
142+----------------------------------------------------------------
143+
116144-- | Call the inner function with a timeout manager.
117145withManager :: Int -- ^ timeout in microseconds
118146 -> (Manager -> IO a)