github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/state/lock.go (about)

     1  package state
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  // TrackingLock provides locking facilities with automatic state tracking
     8  // notifications.
     9  type TrackingLock struct {
    10  	// lock is the underlying mutex.
    11  	lock sync.Mutex
    12  	// tracker is the underlying tracker.
    13  	tracker *Tracker
    14  }
    15  
    16  // NewTrackingLock creates a new tracking lock with the specified tracker.
    17  func NewTrackingLock(tracker *Tracker) *TrackingLock {
    18  	return &TrackingLock{
    19  		tracker: tracker,
    20  	}
    21  }
    22  
    23  // Lock locks the tracking lock.
    24  func (l *TrackingLock) Lock() {
    25  	l.lock.Lock()
    26  }
    27  
    28  // Unlock unlocks the tracking lock and triggers a state update notification.
    29  func (l *TrackingLock) Unlock() {
    30  	l.lock.Unlock()
    31  	l.tracker.NotifyOfChange()
    32  }
    33  
    34  // UnlockWithoutNotify unlocks the tracking lock without triggering a state
    35  // update notification.
    36  func (l *TrackingLock) UnlockWithoutNotify() {
    37  	l.lock.Unlock()
    38  }