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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE RecordWildCards #-}
module System.TimeManager (
-- ** Types
Manager,
defaultManager,
TimeoutAction,
Handle,
emptyHandle,
-- ** Manager
initialize,
stopManager,
killManager,
withManager,
withManager',
-- ** Registering a timeout action
withHandle,
withHandleKillThread,
-- ** Control timeout
tickle,
pause,
resume,
-- ** Low level
register,
registerKillThread,
cancel,
-- ** Exceptions
TimeoutThread (..),
) where
import Control.Concurrent (mkWeakThreadId, myThreadId)
import qualified Control.Exception as E
import Control.Monad (void)
import Control.Reaper
import Data.IORef (IORef)
import qualified Data.IORef as I
import Data.Typeable (Typeable)
import System.IO.Unsafe
import System.Mem.Weak (deRefWeak)
----------------------------------------------------------------
-- | A timeout manager
data Manager = Manager (Reaper [Handle] Handle) | NoManager
-- | No manager.
defaultManager :: Manager
defaultManager = NoManager
-- | An action to be performed on timeout.
type TimeoutAction = IO ()
-- | A handle used by a timeout manager.
data Handle = Handle
{ handleManager :: Manager
, handleActionRef :: IORef TimeoutAction
, handleStateRef :: IORef State
}
{-# NOINLINE emptyAction #-}
emptyAction :: IORef TimeoutAction
emptyAction = unsafePerformIO $ I.newIORef (return ())
{-# NOINLINE emptyState #-}
emptyState :: IORef State
emptyState = unsafePerformIO $ I.newIORef Inactive
emptyHandle :: Handle
emptyHandle =
Handle
{ handleManager = NoManager
, handleActionRef = emptyAction
, handleStateRef = emptyState
}
data State
= Active -- Manager turns it to Inactive.
| Inactive -- Manager removes it with timeout action.
| Paused -- Manager does not change it.
----------------------------------------------------------------
-- | Creating timeout manager which works every N microseconds
-- where N is the first argument.
initialize :: Int -> IO Manager
initialize timeout
| timeout <= 0 = return NoManager
initialize timeout =
Manager
<$> mkReaper
defaultReaperSettings
{ -- Data.Set cannot be used since 'partition' cannot be used
-- with 'readIORef`. So, let's just use a list.
reaperAction = mkListAction prune
, reaperDelay = timeout
, reaperThreadName = "WAI timeout manager (Reaper)"
}
where
prune m@Handle{..} = do
state <- I.atomicModifyIORef' handleStateRef (\x -> (inactivate x, x))
case state of
Inactive -> do
onTimeout <- I.readIORef handleActionRef
onTimeout `E.catch` ignoreSync
return Nothing
_ -> return $ Just m
inactivate Active = Inactive
inactivate x = x
----------------------------------------------------------------
-- | Stopping timeout manager with onTimeout fired.
stopManager :: Manager -> IO ()
stopManager NoManager = return ()
stopManager (Manager mgr) = E.mask_ (reaperStop mgr >>= mapM_ fire)
where
fire Handle{..} = do
onTimeout <- I.readIORef handleActionRef
onTimeout `E.catch` ignoreSync
-- | Killing timeout manager immediately without firing onTimeout.
killManager :: Manager -> IO ()
killManager NoManager = return ()
killManager (Manager mgr) = reaperKill mgr
----------------------------------------------------------------
-- | Registering a timeout action and unregister its handle
-- when the body action is finished.
-- 'Nothing' is returned on timeout.
withHandle :: Manager -> TimeoutAction -> (Handle -> IO a) -> IO (Maybe a)
withHandle mgr onTimeout action =
E.handle ignore $ E.bracket (register mgr onTimeout) cancel $ \th ->
Just <$> action th
where
ignore TimeoutThread = return Nothing
-- | Registering a timeout action of killing this thread and
-- unregister its handle when the body action is killed or finished.
withHandleKillThread :: Manager -> TimeoutAction -> (Handle -> IO ()) -> IO ()
withHandleKillThread mgr onTimeout action =
E.handle ignore $ E.bracket (registerKillThread mgr onTimeout) cancel action
where
ignore TimeoutThread = return ()
----------------------------------------------------------------
-- | Registering a timeout action.
register :: Manager -> TimeoutAction -> IO Handle
register NoManager _ = return emptyHandle
register m@(Manager mgr) !onTimeout = do
actionRef <- I.newIORef onTimeout
stateRef <- I.newIORef Active
let h =
Handle
{ handleManager = m
, handleActionRef = actionRef
, handleStateRef = stateRef
}
reaperAdd mgr h
return h
-- | Removing the 'Handle' from the 'Manager' immediately.
cancel :: Handle -> IO ()
cancel Handle{..} = case handleManager of
NoManager -> return ()
Manager mgr -> void $ reaperModify mgr filt
where
-- It's very important that this function forces the whole workload so we
-- don't retain old handles, otherwise disasterous leaks occur.
filt [] = []
filt (h@(Handle _ _ ref) : hs)
| handleStateRef == ref = hs
| otherwise =
let !hs' = filt hs
in h : hs'
----------------------------------------------------------------
-- | The asynchronous exception thrown if a thread is registered via
-- 'registerKillThread'.
data TimeoutThread = TimeoutThread
deriving (Typeable)
instance E.Exception TimeoutThread where
toException = E.asyncExceptionToException
fromException = E.asyncExceptionFromException
instance Show TimeoutThread where
show TimeoutThread = "Thread killed by timeout manager"
-- | Registering a timeout action of killing this thread.
-- 'TimeoutThread' is thrown to the thread which called this
-- function on timeout. Catch 'TimeoutThread' if you don't
-- want to leak the asynchronous exception to GHC RTS.
registerKillThread :: Manager -> TimeoutAction -> IO Handle
registerKillThread m onTimeout = do
tid <- myThreadId
wtid <- mkWeakThreadId tid
-- First run the timeout action in case the child thread is masked.
register m $
onTimeout `E.finally` do
mtid <- deRefWeak wtid
case mtid of
Nothing -> return ()
Just tid' -> E.throwTo tid' TimeoutThread
----------------------------------------------------------------
-- | Setting the state to active.
-- 'Manager' turns active to inactive repeatedly.
tickle :: Handle -> IO ()
tickle Handle{..} = I.writeIORef handleStateRef Active
-- | Setting the state to paused.
-- 'Manager' does not change the value.
pause :: Handle -> IO ()
pause Handle{..} = I.writeIORef handleStateRef Paused
-- | Setting the paused state to active.
-- This is an alias to 'tickle'.
resume :: Handle -> IO ()
resume = tickle
----------------------------------------------------------------
-- | Call the inner function with a timeout manager.
-- 'stopManager' is used after that.
withManager
:: Int
-- ^ timeout in microseconds
-> (Manager -> IO a)
-> IO a
withManager timeout f =
E.bracket
(initialize timeout)
stopManager
f
-- | Call the inner function with a timeout manager.
-- 'killManager' is used after that.
withManager'
:: Int
-- ^ timeout in microseconds
-> (Manager -> IO a)
-> IO a
withManager' timeout f =
E.bracket
(initialize timeout)
killManager
f
----------------------------------------------------------------
isAsyncException :: E.Exception e => e -> Bool
isAsyncException e =
case E.fromException (E.toException e) of
Just (E.SomeAsyncException _) -> True
Nothing -> False
ignoreSync :: E.SomeException -> IO ()
ignoreSync se
| isAsyncException se = E.throwIO se
| otherwise = return ()
|