on master help sign in
flatten getCurrent.
641ae83Kazu Yamamoto
auto-update/Control/AutoUpdate.hs+12−8
@@ -116,21 +116,25 @@ getCurrent :: Int -- ^ frequency
116 -> IORef (Status a) -- ^ mutable state
116 -> IORef (Status a) -- ^ mutable state
117 -> IO a
117 -> IO a
118getCurrent freq spawnThreshold action istatus = do
118getCurrent freq spawnThreshold action istatus = do
119− ea <- atomicModifyIORef istatus $ \status ->
119+ ea <- atomicModifyIORef istatus increment
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)
123 case ea of
120 case ea of
124 Return a -> return a
121 Return a -> return a
125 Manual -> action
122 Manual -> action
126 Spawn -> do
123 Spawn -> do
127 a <- action
124 a <- action
128 tid <- forkIO $ spawn freq action istatus
125 tid <- forkIO $ spawn freq action istatus
129− join $ atomicModifyIORef istatus $ \status ->
126+ join $ atomicModifyIORef istatus $ turnToAuto a tid
130− case status of
131− AutoUpdated _ cnt old -> (AutoUpdated a cnt tid, throwTo old Replaced)
132− ManualUpdates cnt -> (AutoUpdated a cnt tid, return ())
133 return a
127 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 ())
134
138
135spawn :: Int -> IO a -> IORef (Status a) -> IO ()
139spawn :: Int -> IO a -> IORef (Status a) -> IO ()
136spawn freq action istatus = handle (onErr istatus) $ forever $ do
140spawn freq action istatus = handle (onErr istatus) $ forever $ do