diff options
author | joe <joe@jerkface.net> | 2017-01-25 20:05:42 -0500 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-01-25 20:18:50 -0500 |
commit | 71db760db0291f8df7a7fa854a7a315e59556f71 (patch) | |
tree | 86ae08ba9ce6fc8e5cba5b0add82e657b0e4ec37 | |
parent | 5d8576d0ee6a0523ab650afb6c18296761fd5d1a (diff) |
Fixed leak in thread instrumentation.
-rw-r--r-- | src/Control/Concurrent/Lifted/Instrument.hs | 25 |
1 files changed, 8 insertions, 17 deletions
diff --git a/src/Control/Concurrent/Lifted/Instrument.hs b/src/Control/Concurrent/Lifted/Instrument.hs index e5ed9d5e..673e77cd 100644 --- a/src/Control/Concurrent/Lifted/Instrument.hs +++ b/src/Control/Concurrent/Lifted/Instrument.hs | |||
@@ -13,31 +13,26 @@ import Control.Concurrent.Lifted hiding (fork) | |||
13 | import Control.Monad.Trans.Control | 13 | import Control.Monad.Trans.Control |
14 | import System.IO.Unsafe | 14 | import System.IO.Unsafe |
15 | import System.Mem.Weak | 15 | import System.Mem.Weak |
16 | import qualified Data.Map as Map | 16 | import qualified Data.Map.Strict as Map |
17 | import qualified Data.IntMap as IntMap | ||
18 | import Control.Exception.Lifted | 17 | import Control.Exception.Lifted |
19 | import Control.Monad.Base | 18 | import Control.Monad.Base |
20 | -- import Control.Monad.IO.Class | ||
21 | import qualified GHC.Conc as GHC | 19 | import qualified GHC.Conc as GHC |
22 | import Data.Time() | 20 | import Data.Time() |
23 | import Data.Time.Clock | 21 | import Data.Time.Clock |
24 | 22 | ||
25 | data PerThread = PerThread | 23 | data PerThread = PerThread |
26 | { -- wkid :: Weak ThreadId | 24 | { lbl :: !String |
27 | lbl :: String | 25 | , startTime :: !UTCTime |
28 | , startTime :: UTCTime | ||
29 | } | 26 | } |
30 | deriving (Eq,Ord,Show) -- ,Data,Generic) | 27 | deriving (Eq,Ord,Show) |
31 | 28 | ||
32 | data GlobalState = GlobalState | 29 | data GlobalState = GlobalState |
33 | { threads :: Map.Map ThreadId PerThread -- IntMap.IntMap PerThread | 30 | { threads :: ! ( Map.Map ThreadId PerThread ) |
34 | -- , uniqSource :: Int | ||
35 | } | 31 | } |
36 | 32 | ||
37 | globals :: MVar GlobalState | 33 | globals :: MVar GlobalState |
38 | globals = unsafePerformIO $ newMVar $ GlobalState | 34 | globals = unsafePerformIO $ newMVar $ GlobalState |
39 | { threads = Map.empty | 35 | { threads = Map.empty |
40 | -- , uniqSource = 0 | ||
41 | } | 36 | } |
42 | {-# NOINLINE globals #-} | 37 | {-# NOINLINE globals #-} |
43 | 38 | ||
@@ -48,8 +43,6 @@ forkIO = fork | |||
48 | fork :: MonadBaseControl IO m => m () -> m ThreadId | 43 | fork :: MonadBaseControl IO m => m () -> m ThreadId |
49 | fork action = do | 44 | fork action = do |
50 | t <- Raw.fork $ do | 45 | t <- Raw.fork $ do |
51 | -- wkid <- myThreadId >>= liftBase . mkWeakThreadId | ||
52 | -- tid <- newUniq | ||
53 | tid <- myThreadId | 46 | tid <- myThreadId |
54 | tm <- liftBase getCurrentTime | 47 | tm <- liftBase getCurrentTime |
55 | bracket_ (modifyThreads $ Map.insert tid (PerThread "" tm)) | 48 | bracket_ (modifyThreads $ Map.insert tid (PerThread "" tm)) |
@@ -68,12 +61,10 @@ threadsInformation = do | |||
68 | m <- threads <$> readMVar globals | 61 | m <- threads <$> readMVar globals |
69 | return $ Map.elems m | 62 | return $ Map.elems m |
70 | 63 | ||
71 | -- newUniq :: MonadBaseControl IO m => m Int | 64 | |
72 | -- newUniq = do | ||
73 | -- modifyMVar globals (\st -> return (st { uniqSource = succ (uniqSource st) }, uniqSource st)) | ||
74 | |||
75 | modifyThreads :: MonadBase IO m => (Map.Map ThreadId PerThread -> Map.Map ThreadId PerThread) -> m () | 65 | modifyThreads :: MonadBase IO m => (Map.Map ThreadId PerThread -> Map.Map ThreadId PerThread) -> m () |
76 | modifyThreads f = do | 66 | modifyThreads f = do |
77 | g <- takeMVar globals | 67 | g <- takeMVar globals |
78 | let f' st = st { threads = f (threads st) } | 68 | let f' st = st { threads = f (threads st) } |
79 | putMVar globals (f' g) | 69 | r = f' g |
70 | threads r `seq` putMVar globals r | ||