From 233b2599e941a770260da623cf30656905bc644a Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Tue, 18 Mar 2014 18:47:34 +0400 Subject: Use IORef instead of MVar --- src/Network/BitTorrent/Tracker/Session.hs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/Network') 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 import Data.Default import Data.Fixed import Data.Foldable +import Data.IORef import Data.List as L import Data.Maybe import Data.Time @@ -158,7 +159,7 @@ data Session = Session -- | Status of this client is used to filter duplicated -- notifications, for e.g. we don't want to notify a tracker with -- ['Stopped', 'Stopped'], the last should be ignored. - , currentStatus :: !(MVar Status) + , currentStatus :: !(IORef Status) -- | A set of single-tracker sessions. Any request to a tracker -- must take a lock. @@ -171,7 +172,7 @@ data Session = Session newSession :: InfoHash -> TrackerList URI -> IO Session newSession ih origUris = do uris <- shuffleTiers origUris - status <- newMVar def + status <- newIORef def entries <- newMVar (fmap nullEntry uris) return (Session ih status entries) @@ -186,7 +187,7 @@ withSession ih uris = bracket (newSession ih uris) closeSession -- | Get last announced status. The only action can alter this status -- is 'notify'. getStatus :: Session -> IO Status -getStatus Session {..} = readMVar currentStatus +getStatus Session {..} = readIORef currentStatus -- | Do we need to sent this event to a first working tracker or to -- the all known good trackers? @@ -209,8 +210,8 @@ notifyAll mgr Session {..} event = do -- This function /may/ block until tracker query proceed. notify :: Manager -> Session -> Event -> IO () notify mgr ses event = do - prevStatus <- modifyMVar (currentStatus ses) $ \ s -> - return (fromMaybe s (nextStatus event), s) + prevStatus <- atomicModifyIORef (currentStatus ses) $ \ s -> + (fromMaybe s (nextStatus event), s) when (needNotify event (Just prevStatus) == Just True) $ do notifyAll mgr ses event -- cgit v1.2.3