summaryrefslogtreecommitdiff
path: root/Assistant/TransferSlots.hs
blob: 1859b281bbe89520e7941e4c3ad19d587b8a36a7 (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

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. If the action throws an exception, its slot is
 - freed here, otherwise it should be freed by the TransferWatcher when
 - the transfer is complete.
 -}
inTransferSlot :: TransferSlots -> IO a -> IO a
inTransferSlot s a = bracketOnError start abort run
	where
		start = waitQSemN s 1
		abort = const $ transferComplete s
		run = const a

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