@@ -59,12 +59,7 @@ import GHC.Weak (Weak (..))
5959import System.Mem.Weak (deRefWeak)
6060import Data.Typeable (Typeable)
6161import Control.Reaper
62−#if USE_ATOMIC_PRIMOPS
6362import qualified Data.Atomics.Counter.Unboxed as C
64−import Data.Atomics.Counter.Unboxed (casCounter)
65−#else
66−import Network.Wai.Handler.Warp.IORef
67−#endif
6863
6964----------------------------------------------------------------
7065
@@ -75,12 +70,7 @@ type Manager = Reaper [Handle] Handle
7570type TimeoutAction = IO ()
7671
7772-- | A handle used by 'Manager'
78−data Handle = Handle TimeoutAction
79−#if USE_ATOMIC_PRIMOPS
80− {-# UNPACK #-} !C.AtomicCounter
81−#else
82− {-# UNPACK #-} !(IORef Int)
83−#endif
73+data Handle = Handle TimeoutAction {-# UNPACK #-} !C.AtomicCounter
8474
8575-- Four states for the AtomicCounter:
8676--
@@ -101,7 +91,7 @@ initialize timeout = mkReaper defaultReaperSettings
10191 where
10292 prune m@(Handle onTimeout iactive) = do
10393 -- Try to change from active to inactive
104− (wasActive, newState) <- casCounter iactive 1 2
94+ (wasActive, newState) <- C.casCounter iactive 1 2
10595 case newState of
10696 2 | not wasActive -> do -- inactive
10797 onTimeout `E.catch` ignoreAll
@@ -109,14 +99,6 @@ initialize timeout = mkReaper defaultReaperSettings
10999 4 -> return Nothing -- canceled
110100 _ -> return $ Just m
111101
112−#if !USE_ATOMIC_PRIMOPS
113−casCounter :: IORef Int -> Int -> Int -> IO (Bool, Int)
114−casCounter ref old new = atomicModifyIORef' ref $ \curr ->
115− if old == curr
116− then (new, (True, new))
117− else (old, (False, old))
118−#endif
119−
120102----------------------------------------------------------------
121103
122104-- | Stopping timeout manager.
@@ -133,11 +115,7 @@ ignoreAll _ = return ()
133115-- | Registering a timeout action.
134116register :: Manager -> TimeoutAction -> IO Handle
135117register mgr onTimeout = do
136−#if USE_ATOMIC_PRIMOPS
137118 iactive <- C.newCounter 1
138−#else
139− iactive <- newIORef 1
140−#endif
141119 let h = Handle onTimeout iactive
142120 reaperAdd mgr h
143121 return h
@@ -171,28 +149,20 @@ mkWeakThreadId t@(ThreadId t#) = IO $ \s ->
171149
172150----------------------------------------------------------------
173151
174−writeCounter :: Int -> Handle -> IO ()
175−#if USE_ATOMIC_PRIMOPS
176−writeCounter i (Handle _ iactive) = C.writeCounter iactive i
177−#else
178−writeCounter i (Handle _ iactive) = writeIORef iactive i
179−#endif
180−{-# INLINE writeCounter #-}
181−
182152-- | Setting the state to active.
183153-- 'Manager' turns active to inactive repeatedly.
184154tickle :: Handle -> IO ()
185−tickle = writeCounter 1
155+tickle (Handle _ iactive) = C.writeCounter iactive 1
186156
187157-- | Setting the state to canceled.
188158-- 'Manager' eventually removes this without timeout action.
189159cancel :: Handle -> IO ()
190−cancel = writeCounter 4
160+cancel (Handle _ iactive) = C.writeCounter iactive 4
191161
192162-- | Setting the state to paused.
193163-- 'Manager' does not change the value.
194164pause :: Handle -> IO ()
195−pause = writeCounter 3
165+pause (Handle _ iactive) = C.writeCounter iactive 3
196166
197167-- | Setting the paused state to active.
198168-- This is an alias to 'tickle'.