diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-03-18 18:47:34 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-03-18 18:47:34 +0400 |
commit | 233b2599e941a770260da623cf30656905bc644a (patch) | |
tree | 9e722d63002076c24fa1fb8595d0a18620f969c7 | |
parent | 447335009ff6b762073004de1c0cab3b4372ed44 (diff) |
Use IORef instead of MVar
-rw-r--r-- | src/Network/BitTorrent/Tracker/Session.hs | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/src/Network/BitTorrent/Tracker/Session.hs b/src/Network/BitTorrent/Tracker/Session.hs index a9f436e8..6767eb68 100644 --- a/src/Network/BitTorrent/Tracker/Session.hs +++ b/src/Network/BitTorrent/Tracker/Session.hs | |||
@@ -37,6 +37,7 @@ import Control.Monad | |||
37 | import Data.Default | 37 | import Data.Default |
38 | import Data.Fixed | 38 | import Data.Fixed |
39 | import Data.Foldable | 39 | import Data.Foldable |
40 | import Data.IORef | ||
40 | import Data.List as L | 41 | import Data.List as L |
41 | import Data.Maybe | 42 | import Data.Maybe |
42 | import Data.Time | 43 | import Data.Time |
@@ -158,7 +159,7 @@ data Session = Session | |||
158 | -- | Status of this client is used to filter duplicated | 159 | -- | Status of this client is used to filter duplicated |
159 | -- notifications, for e.g. we don't want to notify a tracker with | 160 | -- notifications, for e.g. we don't want to notify a tracker with |
160 | -- ['Stopped', 'Stopped'], the last should be ignored. | 161 | -- ['Stopped', 'Stopped'], the last should be ignored. |
161 | , currentStatus :: !(MVar Status) | 162 | , currentStatus :: !(IORef Status) |
162 | 163 | ||
163 | -- | A set of single-tracker sessions. Any request to a tracker | 164 | -- | A set of single-tracker sessions. Any request to a tracker |
164 | -- must take a lock. | 165 | -- must take a lock. |
@@ -171,7 +172,7 @@ data Session = Session | |||
171 | newSession :: InfoHash -> TrackerList URI -> IO Session | 172 | newSession :: InfoHash -> TrackerList URI -> IO Session |
172 | newSession ih origUris = do | 173 | newSession ih origUris = do |
173 | uris <- shuffleTiers origUris | 174 | uris <- shuffleTiers origUris |
174 | status <- newMVar def | 175 | status <- newIORef def |
175 | entries <- newMVar (fmap nullEntry uris) | 176 | entries <- newMVar (fmap nullEntry uris) |
176 | return (Session ih status entries) | 177 | return (Session ih status entries) |
177 | 178 | ||
@@ -186,7 +187,7 @@ withSession ih uris = bracket (newSession ih uris) closeSession | |||
186 | -- | Get last announced status. The only action can alter this status | 187 | -- | Get last announced status. The only action can alter this status |
187 | -- is 'notify'. | 188 | -- is 'notify'. |
188 | getStatus :: Session -> IO Status | 189 | getStatus :: Session -> IO Status |
189 | getStatus Session {..} = readMVar currentStatus | 190 | getStatus Session {..} = readIORef currentStatus |
190 | 191 | ||
191 | -- | Do we need to sent this event to a first working tracker or to | 192 | -- | Do we need to sent this event to a first working tracker or to |
192 | -- the all known good trackers? | 193 | -- the all known good trackers? |
@@ -209,8 +210,8 @@ notifyAll mgr Session {..} event = do | |||
209 | -- This function /may/ block until tracker query proceed. | 210 | -- This function /may/ block until tracker query proceed. |
210 | notify :: Manager -> Session -> Event -> IO () | 211 | notify :: Manager -> Session -> Event -> IO () |
211 | notify mgr ses event = do | 212 | notify mgr ses event = do |
212 | prevStatus <- modifyMVar (currentStatus ses) $ \ s -> | 213 | prevStatus <- atomicModifyIORef (currentStatus ses) $ \ s -> |
213 | return (fromMaybe s (nextStatus event), s) | 214 | (fromMaybe s (nextStatus event), s) |
214 | when (needNotify event (Just prevStatus) == Just True) $ do | 215 | when (needNotify event (Just prevStatus) == Just True) $ do |
215 | notifyAll mgr ses event | 216 | notifyAll mgr ses event |
216 | 217 | ||