@@ -116,21 +116,25 @@ getCurrent :: Int -- ^ frequency
116116 -> IORef (Status a) -- ^ mutable state
117117 -> IO a
118118getCurrent freq spawnThreshold action istatus = do
119− ea <- atomicModifyIORef istatus $ \status ->
120− case status of
121− AutoUpdated a cnt tid -> (AutoUpdated a (succ cnt) tid, Return a)
122− ManualUpdates i -> (ManualUpdates (succ i), if i > spawnThreshold then Spawn else Manual)
119+ ea <- atomicModifyIORef istatus increment
123120 case ea of
124121 Return a -> return a
125122 Manual -> action
126123 Spawn -> do
127124 a <- action
128125 tid <- forkIO $ spawn freq action istatus
129− join $ atomicModifyIORef istatus $ \status ->
130− case status of
131− AutoUpdated _ cnt old -> (AutoUpdated a cnt tid, throwTo old Replaced)
132− ManualUpdates cnt -> (AutoUpdated a cnt tid, return ())
126+ join $ atomicModifyIORef istatus $ turnToAuto a tid
133127 return a
128+ where
129+ increment (AutoUpdated a cnt tid) = (AutoUpdated a (succ cnt) tid, Return a)
130+ increment (ManualUpdates i) = (ManualUpdates (succ i), act)
131+ where
132+ act = if i > spawnThreshold then Spawn else Manual
133+
134+ turnToAuto a tid (AutoUpdated _ cnt old) = (AutoUpdated a cnt tid
135+ ,throwTo old Replaced)
136+ turnToAuto a tid (ManualUpdates cnt) = (AutoUpdated a cnt tid
137+ ,return ())
134138
135139spawn :: Int -> IO a -> IORef (Status a) -> IO ()
136140spawn freq action istatus = handle (onErr istatus) $ forever $ do