summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-03-18 18:47:34 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-03-18 18:47:34 +0400
commit233b2599e941a770260da623cf30656905bc644a (patch)
tree9e722d63002076c24fa1fb8595d0a18620f969c7
parent447335009ff6b762073004de1c0cab3b4372ed44 (diff)
Use IORef instead of MVar
-rw-r--r--src/Network/BitTorrent/Tracker/Session.hs11
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
37import Data.Default 37import Data.Default
38import Data.Fixed 38import Data.Fixed
39import Data.Foldable 39import Data.Foldable
40import Data.IORef
40import Data.List as L 41import Data.List as L
41import Data.Maybe 42import Data.Maybe
42import Data.Time 43import 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
171newSession :: InfoHash -> TrackerList URI -> IO Session 172newSession :: InfoHash -> TrackerList URI -> IO Session
172newSession ih origUris = do 173newSession 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'.
188getStatus :: Session -> IO Status 189getStatus :: Session -> IO Status
189getStatus Session {..} = readMVar currentStatus 190getStatus 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.
210notify :: Manager -> Session -> Event -> IO () 211notify :: Manager -> Session -> Event -> IO ()
211notify mgr ses event = do 212notify 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