summaryrefslogtreecommitdiff
path: root/Assistant/TransferSlots.hs
blob: c394dc30d21e621d2c0ad0a84c7aa32c8f4bc42a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{- 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

import Common.Annex

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
 - action in the slot, in its own thread. Note that this thread is
 - subject to being killed when the transfer is canceled. -}
inTransferSlot :: TransferSlots -> IO () -> IO ThreadId
inTransferSlot s a = do
	waitQSemN s 1
	forkIO $ bracket_ noop done a
	where
		done = transferComplete s

{- Call when a transfer is complete. -}
transferComplete :: TransferSlots -> IO ()
transferComplete s = signalQSemN s 1