summaryrefslogtreecommitdiff
path: root/src/Control
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-01-25 20:05:42 -0500
committerjoe <joe@jerkface.net>2017-01-25 20:18:50 -0500
commit71db760db0291f8df7a7fa854a7a315e59556f71 (patch)
tree86ae08ba9ce6fc8e5cba5b0add82e657b0e4ec37 /src/Control
parent5d8576d0ee6a0523ab650afb6c18296761fd5d1a (diff)
Fixed leak in thread instrumentation.
Diffstat (limited to 'src/Control')
-rw-r--r--src/Control/Concurrent/Lifted/Instrument.hs25
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)
13import Control.Monad.Trans.Control 13import Control.Monad.Trans.Control
14import System.IO.Unsafe 14import System.IO.Unsafe
15import System.Mem.Weak 15import System.Mem.Weak
16import qualified Data.Map as Map 16import qualified Data.Map.Strict as Map
17import qualified Data.IntMap as IntMap
18import Control.Exception.Lifted 17import Control.Exception.Lifted
19import Control.Monad.Base 18import Control.Monad.Base
20-- import Control.Monad.IO.Class
21import qualified GHC.Conc as GHC 19import qualified GHC.Conc as GHC
22import Data.Time() 20import Data.Time()
23import Data.Time.Clock 21import Data.Time.Clock
24 22
25data PerThread = PerThread 23data 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
32data GlobalState = GlobalState 29data GlobalState = GlobalState
33 { threads :: Map.Map ThreadId PerThread -- IntMap.IntMap PerThread 30 { threads :: ! ( Map.Map ThreadId PerThread )
34 -- , uniqSource :: Int
35 } 31 }
36 32
37globals :: MVar GlobalState 33globals :: MVar GlobalState
38globals = unsafePerformIO $ newMVar $ GlobalState 34globals = 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
48fork :: MonadBaseControl IO m => m () -> m ThreadId 43fork :: MonadBaseControl IO m => m () -> m ThreadId
49fork action = do 44fork 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
75modifyThreads :: MonadBase IO m => (Map.Map ThreadId PerThread -> Map.Map ThreadId PerThread) -> m () 65modifyThreads :: MonadBase IO m => (Map.Map ThreadId PerThread -> Map.Map ThreadId PerThread) -> m ()
76modifyThreads f = do 66modifyThreads 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