2012-07-06 20:39:07 +00:00
|
|
|
{- git-annex assistant transfer slots
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Assistant.TransferSlots where
|
|
|
|
|
|
|
|
import Control.Exception
|
|
|
|
import Control.Concurrent
|
|
|
|
|
2012-07-18 23:13:56 +00:00
|
|
|
import Common.Annex
|
|
|
|
import Assistant.ThreadedMonad
|
|
|
|
|
2012-07-06 20:39:07 +00:00
|
|
|
type TransferSlots = QSemN
|
|
|
|
|
|
|
|
{- Number of concurrent transfers allowed to be run from the assistant.
|
|
|
|
-
|
|
|
|
- Transfers launched by other means, including by remote assistants,
|
|
|
|
- do not currently take up slots.
|
|
|
|
-}
|
|
|
|
numSlots :: Int
|
|
|
|
numSlots = 1
|
|
|
|
|
|
|
|
newTransferSlots :: IO TransferSlots
|
|
|
|
newTransferSlots = newQSemN numSlots
|
|
|
|
|
|
|
|
{- Waits until a transfer slot becomes available, and runs a transfer
|
2012-07-18 23:13:56 +00:00
|
|
|
- action in the slot, in its own thread. -}
|
|
|
|
inTransferSlot :: TransferSlots -> ThreadState -> Annex a -> IO ThreadId
|
2012-07-25 16:07:30 +00:00
|
|
|
inTransferSlot s st a = do
|
|
|
|
waitQSemN s 1
|
|
|
|
forkIO $ bracket_ noop done run
|
2012-07-07 03:45:08 +00:00
|
|
|
where
|
2012-07-18 23:13:56 +00:00
|
|
|
done = transferComplete s
|
|
|
|
run = unsafeRunThreadState st a
|
2012-07-07 03:45:08 +00:00
|
|
|
|
|
|
|
{- Call when a transfer is complete. -}
|
|
|
|
transferComplete :: TransferSlots -> IO ()
|
|
|
|
transferComplete s = signalQSemN s 1
|