shelly-extra 0.2.3 → 0.2.3.1
raw patch · 2 files changed
+9/−12 lines, 2 filesdep ~SafeSemaphore
Dependency ranges changed: SafeSemaphore
Files
- Shelly/Background.hs +7/−10
- shelly-extra.cabal +2/−2
Shelly/Background.hs view
@@ -15,7 +15,7 @@ import Control.Concurrent import Control.Exception (finally, catch, throwIO, SomeException) import Prelude hiding (catch)-import qualified Control.Concurrent.MSem as Sem+import qualified Control.Concurrent.MSemN as Sem -- | Create a 'BgJobManager' that has a 'limit' on the max number of background tasks. -- an invocation of jobs is independent of any others, and not tied to the Sh monad in any way.@@ -25,15 +25,11 @@ unless (limit > 0) $ terror "expected limit to be > 0" availableJobsSem <- liftIO $ Sem.new limit res <- action $ BgJobManager availableJobsSem- liftIO $ waitForJobs availableJobsSem+ liftIO $ Sem.wait availableJobsSem limit return res- where- waitForJobs sem = do- avail <- Sem.peekAvail sem- if avail == limit then return () else waitForJobs sem -- | The manager tracks the number of jobs. Register your 'background' jobs with it.-newtype BgJobManager = BgJobManager (Sem.MSem Int)+newtype BgJobManager = BgJobManager (Sem.MSemN Int) -- | Type returned by tasks run asynchronously in the background. newtype BgResult a = BgResult (MVar a)@@ -55,17 +51,18 @@ -- It is important to do this before forkIO: -- It ensures that that jobs will block and the program won't exit before our jobs are done -- On the other hand, a user might not expect 'jobs' to block- Sem.wait manager+ Sem.wait manager 1 mvar <- newEmptyMVar -- future result + -- probably should use async package rather than manually handling exception stuff mainTid <- myThreadId _<- forkIO $ do result <- finally (- (shelly $ (put state >> proc)) `catch`+ shelly (put state >> proc) `catch` (\(e::SomeException) -> throwTo mainTid e >> throwIO e) )- (Sem.signal manager >> return ()) -- open a spot back up+ (Sem.signal manager 1) -- open a spot back up putMVar mvar result return $ BgResult mvar
shelly-extra.cabal view
@@ -1,6 +1,6 @@ Name: shelly-extra -Version: 0.2.3+Version: 0.2.3.1 Synopsis: shelly features that require extra dependencies Description: A background job implementation for performing tasks in parallel.@@ -28,7 +28,7 @@ Library Exposed-modules: Shelly.Background - Build-depends: base >= 4 && < 5, shelly >= 0.13, SafeSemaphore+ Build-depends: base >= 4 && < 5, shelly >= 0.13, SafeSemaphore >= 0.7 ghc-options: -Wall