async-manager 0.1.0.0 → 0.1.1.0
raw patch · 2 files changed
+34/−13 lines, 2 filesdep ~basePVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependency ranges changed: base
API changes (from Hackage documentation)
- Control.Concurrent.AsyncManager: newtype AsyncManager
- Control.Concurrent.AsyncManager: unAsyncManager :: AsyncManager -> MVar (HashMap ThreadId AnyAsync)
+ Control.Concurrent.AsyncManager: childrenManagers :: AsyncManager -> MVar [AsyncManager]
+ Control.Concurrent.AsyncManager: childrenThreads :: AsyncManager -> MVar (HashMap ThreadId AnyAsync)
+ Control.Concurrent.AsyncManager: data AsyncManager
+ Control.Concurrent.AsyncManager: newChildManager :: AsyncManager -> IO AsyncManager
- Control.Concurrent.AsyncManager: AsyncManager :: MVar (HashMap ThreadId AnyAsync) -> AsyncManager
+ Control.Concurrent.AsyncManager: AsyncManager :: MVar (HashMap ThreadId AnyAsync) -> MVar [AsyncManager] -> AsyncManager
Files
async-manager.cabal view
@@ -2,9 +2,9 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: async-manager-version: 0.1.0.0+version: 0.1.1.0 synopsis: A thread manager for async-description: Cleanup and kill async threads.+description: Cleanup and kill async threads. homepage: http://github.com/jfischoff/async-manager license: BSD3 license-file: LICENSE@@ -20,7 +20,7 @@ exposed-modules: Control.Concurrent.AsyncManager -- other-modules: other-extensions: ExistentialQuantification- build-depends: base >=4.7 && <4.8+ build-depends: base >=4.6 && <4.8 , async >=2.0 && <2.1 , stm >=2.4 && <2.5 , unordered-containers >=0.2 && <0.3
src/Control/Concurrent/AsyncManager.hs view
@@ -20,39 +20,59 @@ -- | An Async thread manager. Keeps track of allocated and running -- Async threads. Useful for ensure threads are cleaned up-newtype AsyncManager = AsyncManager { unAsyncManager :: MVar (HashMap ThreadId AnyAsync) }+data AsyncManager = AsyncManager + { childrenThreads :: MVar (HashMap ThreadId AnyAsync) + , childrenManagers :: MVar [AsyncManager]+ }+ -- | Create a new empty manager newAsyncManager :: IO AsyncManager-newAsyncManager = AsyncManager <$> newMVar mempty+newAsyncManager = AsyncManager <$> newMVar mempty <*> newMVar mempty +-- | Create a child manager+newChildManager :: AsyncManager -> IO AsyncManager+newChildManager (AsyncManager _ csRef) = modifyMVar csRef $ \cs -> do+ child <- newAsyncManager + return (child : cs, child)+ -- | Insert a new thread into the manager -- TODO: maybe I should check if the computation is still running insert :: AsyncManager -> Async a -> IO ()-insert (AsyncManager ref) as +insert (AsyncManager ref _) as = modifyMVar_ ref $ return . (H.insert (asyncThreadId as) (AnyAsync as)) -- | Cancel all threads and empty the manager clear :: AsyncManager -> IO ()-clear (AsyncManager ref) = modifyMVar_ ref $ \xs -> do- forM_ (toList xs) $ \(AnyAsync x) -> cancel x- return mempty+clear (AsyncManager ref csRef) = do + modifyMVar_ ref $ \xs -> do+ forM_ (toList xs) $ \(AnyAsync x) -> cancel x+ return mempty+ + modifyMVar_ csRef $ \xs -> do+ forM_ xs $ \x -> clear x+ return mempty -- | Thread count. Includes alive and dead threads count :: AsyncManager -> IO Int-count (AsyncManager ref) = H.size <$> readMVar ref+count (AsyncManager ref csRef) = do+ threadCount <- H.size <$> readMVar ref+ managerCount <- length <$> readMVar csRef+ return $ threadCount + managerCount -- | Remove references to dead threads compact :: AsyncManager -> IO ()-compact (AsyncManager ref) = modifyMVar_ ref $ +compact (AsyncManager ref _) = modifyMVar_ ref $ fmap H.fromList . filterM ((\(AnyAsync x) -> isJust <$> poll x) . snd) . H.toList +-- TODO deepCompact+ -- | Cancel the thread and remove the entry from the manager cancelWithManager :: AsyncManager -> Async a -> IO ()-cancelWithManager (AsyncManager ref) as = do+cancelWithManager (AsyncManager ref _) as = do cancel as modifyMVar_ ref $ return . H.delete (asyncThreadId as) @@ -64,7 +84,8 @@ result <- async act insert am result return result- ++-- Create a thread with ghc event label labelAsyncWithManager :: AsyncManager -> String -> IO a