diff options
author | Andrew Cady <d@jerkface.net> | 2018-06-21 22:26:41 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2018-06-21 22:35:10 -0400 |
commit | 02717b117646c952848704890ccdd1705fcb11c5 (patch) | |
tree | eff84e7cb848a8bb2cf661140986a436145f313e | |
parent | 7f896220887d588bad65aed5900e71823c65692e (diff) |
"unschedule" is STM variant of "cancel"
-rw-r--r-- | Announcer.hs | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/Announcer.hs b/Announcer.hs index ef931cce..8d2087dd 100644 --- a/Announcer.hs +++ b/Announcer.hs | |||
@@ -18,6 +18,7 @@ module Announcer | |||
18 | , cancel | 18 | , cancel |
19 | , itemStatusNum | 19 | , itemStatusNum |
20 | , runAction | 20 | , runAction |
21 | , unschedule | ||
21 | 22 | ||
22 | -- lower level, Announcer.Tox needs these. | 23 | -- lower level, Announcer.Tox needs these. |
23 | , scheduleImmediately | 24 | , scheduleImmediately |
@@ -121,9 +122,11 @@ stopAnnouncer announcer = do | |||
121 | atomically $ writeTChan (commander announcer) ShutdownScheduler | 122 | atomically $ writeTChan (commander announcer) ShutdownScheduler |
122 | atomically $ readTVar (announcerActive announcer) >>= check . not | 123 | atomically $ readTVar (announcerActive announcer) >>= check . not |
123 | 124 | ||
125 | unschedule :: Announcer -> AnnounceKey -> STM () | ||
126 | unschedule announcer k = writeTChan (commander announcer) $ UnscheduleAction k | ||
127 | |||
124 | cancel :: Announcer -> AnnounceKey -> IO () | 128 | cancel :: Announcer -> AnnounceKey -> IO () |
125 | cancel announcer k = do | 129 | cancel = ((.).(.)) atomically unschedule |
126 | atomically $ writeTChan (commander announcer) $ ScheduleAction (k, 0, (ScheduledItem (\a k p -> return $ atomically $ return ()))) | ||
127 | 130 | ||
128 | -- | Construct an 'Announcer' object and fork a thread in which to perform the | 131 | -- | Construct an 'Announcer' object and fork a thread in which to perform the |
129 | -- Kademlia searches and announces. | 132 | -- Kademlia searches and announces. |
@@ -143,7 +146,7 @@ readTChanTimeout delay pktChannel = do | |||
143 | toMicroseconds :: POSIXTime -> Int | 146 | toMicroseconds :: POSIXTime -> Int |
144 | toMicroseconds = round . (* 1000) . (* 1000) | 147 | toMicroseconds = round . (* 1000) . (* 1000) |
145 | 148 | ||
146 | data SchedulerCommand = ShutdownScheduler | ScheduleAction KPS | RunAction (IO ()) | 149 | data SchedulerCommand = ShutdownScheduler | ScheduleAction KPS | RunAction (IO ()) | UnscheduleAction AnnounceKey |
147 | 150 | ||
148 | listener :: Announcer -> IO () | 151 | listener :: Announcer -> IO () |
149 | listener announcer = relisten | 152 | listener announcer = relisten |
@@ -174,16 +177,17 @@ listener announcer = relisten | |||
174 | ScheduledItem f -> (fmap (>> relisten) (fmap fork (f announcer k now))) | 177 | ScheduledItem f -> (fmap (>> relisten) (fmap fork (f announcer k now))) |
175 | 178 | ||
176 | where | 179 | where |
180 | modifyScheduled f = modifyTVar (scheduled announcer) (Schedule . f . unSchedule) | ||
177 | declareInactive = writeTVar (announcerActive announcer) False | 181 | declareInactive = writeTVar (announcerActive announcer) False |
178 | handleCommand = | 182 | handleCommand = |
179 | \case | 183 | \case |
180 | ShutdownScheduler -> atomically declareInactive | 184 | ShutdownScheduler -> atomically declareInactive |
181 | RunAction io -> io >> relisten | 185 | RunAction io -> io >> relisten |
186 | UnscheduleAction k -> do | ||
187 | atomically $ modifyScheduled $ PSQ.delete k | ||
188 | relisten | ||
182 | ScheduleAction (k, p, s) -> do | 189 | ScheduleAction (k, p, s) -> do |
183 | atomically $ | 190 | atomically $ modifyScheduled $ PSQ.insert' k s p |
184 | modifyTVar | ||
185 | (scheduled announcer) | ||
186 | (Schedule . PSQ.insert' k s p . unSchedule) | ||
187 | relisten | 191 | relisten |
188 | 192 | ||
189 | announceThread :: Announcer -> IO () | 193 | announceThread :: Announcer -> IO () |