github.com/nebulouslabs/sia@v1.3.7/modules/host/storageobligationslock.go (about)

     1  package host
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/NebulousLabs/Sia/sync"
     7  	"github.com/NebulousLabs/Sia/types"
     8  )
     9  
    10  var (
    11  	// errObligationLocked is returned if the file contract being requested is
    12  	// currently locked. The lock can be in place if there is a storage proof
    13  	// being submitted, if there is another renter altering the contract, or if
    14  	// there have been network connections with have not resolved yet.
    15  	errObligationLocked = errors.New("the requested file contract is currently locked")
    16  )
    17  
    18  // managedLockStorageObligation puts a storage obligation under lock in the
    19  // host.
    20  func (h *Host) managedLockStorageObligation(soid types.FileContractID) {
    21  	// Check if a lock has been created for this storage obligation. If not,
    22  	// create one. The map must be accessed under lock, but the request for the
    23  	// storage lock must not be made under lock.
    24  	h.mu.Lock()
    25  	tl, exists := h.lockedStorageObligations[soid]
    26  	if !exists {
    27  		tl = new(sync.TryMutex)
    28  		h.lockedStorageObligations[soid] = tl
    29  	}
    30  	h.mu.Unlock()
    31  
    32  	tl.Lock()
    33  }
    34  
    35  // managedTryLockStorageObligation attempts to put a storage obligation under
    36  // lock, returning an error if the lock cannot be obtained.
    37  func (h *Host) managedTryLockStorageObligation(soid types.FileContractID) error {
    38  	// Check if a lock has been created for this storage obligation. If not,
    39  	// create one. The map must be accessed under lock, but the request for the
    40  	// storage lock must not be made under lock.
    41  	h.mu.Lock()
    42  	tl, exists := h.lockedStorageObligations[soid]
    43  	if !exists {
    44  		tl = new(sync.TryMutex)
    45  		h.lockedStorageObligations[soid] = tl
    46  	}
    47  	h.mu.Unlock()
    48  
    49  	if tl.TryLockTimed(obligationLockTimeout) {
    50  		return nil
    51  	}
    52  	return errObligationLocked
    53  }
    54  
    55  // managedUnlockStorageObligation takes a storage obligation out from under lock in
    56  // the host.
    57  func (h *Host) managedUnlockStorageObligation(soid types.FileContractID) {
    58  	// Check if a lock has been created for this storage obligation. The map
    59  	// must be accessed under lock, but the request for the unlock must not
    60  	// be made under lock.
    61  	h.mu.Lock()
    62  	tl, exists := h.lockedStorageObligations[soid]
    63  	if !exists {
    64  		h.log.Critical(errObligationUnlocked)
    65  		return
    66  	}
    67  	h.mu.Unlock()
    68  
    69  	tl.Unlock()
    70  }