forked from NebulousLabs/Sia
/
storageobligationslock.go
70 lines (61 loc) · 2.15 KB
/
storageobligationslock.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package host
import (
"errors"
"github.com/NebulousLabs/Sia/sync"
"github.com/NebulousLabs/Sia/types"
)
var (
// errObligationLocked is returned if the file contract being requested is
// currently locked. The lock can be in place if there is a storage proof
// being submitted, if there is another renter altering the contract, or if
// there have been network connections with have not resolved yet.
errObligationLocked = errors.New("the requested file contract is currently locked")
)
// managedLockStorageObligation puts a storage obligation under lock in the
// host.
func (h *Host) managedLockStorageObligation(soid types.FileContractID) {
// Check if a lock has been created for this storage obligation. If not,
// create one. The map must be accessed under lock, but the request for the
// storage lock must not be made under lock.
h.mu.Lock()
tl, exists := h.lockedStorageObligations[soid]
if !exists {
tl = new(sync.TryMutex)
h.lockedStorageObligations[soid] = tl
}
h.mu.Unlock()
tl.Lock()
}
// managedTryLockStorageObligation attempts to put a storage obligation under
// lock, returning an error if the lock cannot be obtained.
func (h *Host) managedTryLockStorageObligation(soid types.FileContractID) error {
// Check if a lock has been created for this storage obligation. If not,
// create one. The map must be accessed under lock, but the request for the
// storage lock must not be made under lock.
h.mu.Lock()
tl, exists := h.lockedStorageObligations[soid]
if !exists {
tl = new(sync.TryMutex)
h.lockedStorageObligations[soid] = tl
}
h.mu.Unlock()
if tl.TryLockTimed(obligationLockTimeout) {
return nil
}
return errObligationLocked
}
// managedUnlockStorageObligation takes a storage obligation out from under lock in
// the host.
func (h *Host) managedUnlockStorageObligation(soid types.FileContractID) {
// Check if a lock has been created for this storage obligation. The map
// must be accessed under lock, but the request for the unlock must not
// be made under lock.
h.mu.Lock()
tl, exists := h.lockedStorageObligations[soid]
if !exists {
h.log.Critical(errObligationUnlocked)
return
}
h.mu.Unlock()
tl.Unlock()
}