@@ -30,7 +30,6 @@ import Network.Wai.Handler.Warp.MultiMap
3030import System.Posix.IO (openFd, OpenFileFlags(..), defaultFileFlags, OpenMode(ReadOnly), closeFd)
3131import System.Posix.Types (Fd)
3232import Control.Reaper
33−import Data.Maybe (fromMaybe)
3433
3534----------------------------------------------------------------
3635
@@ -68,12 +67,10 @@ type Hash = Int
6867type FdCache = MMap Hash FdEntry
6968
7069-- | Mutable Fd cacher.
71−data MutableFdCache = MutableFdCache
72− ((Hash, FdEntry) -> IO ())
73− (IORef (Maybe FdCache))
70+type MutableFdCache = Reaper FdCache (Hash, FdEntry)
7471
7572fdCache :: MutableFdCache -> IO FdCache
76−fdCache (MutableFdCache _ ref) = fromMaybe empty <$> readIORef ref
73+fdCache mfc = reaperRead mfc
7774
7875look :: MutableFdCache -> FilePath -> Hash -> IO (Maybe FdEntry)
7976look mfc path key = searchWith key check <$> fdCache mfc
@@ -99,8 +96,7 @@ withFdCache duration action = bracket (initialize duration)
9996-- The first argument is a cache duration in second.
10097initialize :: Int -> IO (Maybe MutableFdCache)
10198initialize 0 = return Nothing
102−initialize duration = fmap (Just . uncurry MutableFdCache)
103− $ mkReaper defaultReaperSettings
99+initialize duration = Just <$> mkReaper defaultReaperSettings
104100 { reaperAction = clean
105101 , reaperDelay = duration
106102 , reaperCons = uncurry insert
@@ -133,11 +129,8 @@ prune k (Tom ent@(FdEntry _ fd mst) vs) = status mst >>= prune'
133129
134130terminate :: Maybe MutableFdCache -> IO ()
135131terminate Nothing = return ()
136−terminate (Just (MutableFdCache _ ref)) = do
137− !t <- atomicModifyIORef ref $ \mm ->
138− case mm of
139− Nothing -> (Nothing, empty)
140− Just m -> (Just empty, m)
132+terminate (Just mfc) = do
133+ !t <- reaperStop mfc
141134 mapM_ closeIt $ toList t
142135 where
143136 closeIt (_, FdEntry _ fd _) = closeFd fd
@@ -146,12 +139,12 @@ terminate (Just (MutableFdCache _ ref)) = do
146139
147140-- | Getting 'Fd' and 'Refresh' from the mutable Fd cacher.
148141getFd :: MutableFdCache -> FilePath -> IO (Fd, Refresh)
149−getFd mfc@(MutableFdCache add _) path = look mfc path key >>= getFd'
142+getFd mfc path = look mfc path key >>= getFd'
150143 where
151144 key = hash path
152145 getFd' Nothing = do
153146 ent@(FdEntry _ fd mst) <- newFdEntry path
154− add (key, ent)
147+ reaperAdd mfc (key, ent)
155148 return (fd, refresh mst)
156149 getFd' (Just (FdEntry _ fd mst)) = do
157150 refresh mst