github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/sentry/fsimpl/overlay/dir_mutex.go (about)

     1  package overlay
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/metacubex/gvisor/pkg/sync"
     7  	"github.com/metacubex/gvisor/pkg/sync/locking"
     8  )
     9  
    10  // Mutex is sync.Mutex with the correctness validator.
    11  type dirMutex struct {
    12  	mu sync.Mutex
    13  }
    14  
    15  var dirprefixIndex *locking.MutexClass
    16  
    17  // lockNames is a list of user-friendly lock names.
    18  // Populated in init.
    19  var dirlockNames []string
    20  
    21  // lockNameIndex is used as an index passed to NestedLock and NestedUnlock,
    22  // referring to an index within lockNames.
    23  // Values are specified using the "consts" field of go_template_instance.
    24  type dirlockNameIndex int
    25  
    26  // DO NOT REMOVE: The following function automatically replaced with lock index constants.
    27  const (
    28  	dirLockNew      = dirlockNameIndex(0)
    29  	dirLockReplaced = dirlockNameIndex(1)
    30  	dirLockChild    = dirlockNameIndex(2)
    31  )
    32  const ()
    33  
    34  // Lock locks m.
    35  // +checklocksignore
    36  func (m *dirMutex) Lock() {
    37  	locking.AddGLock(dirprefixIndex, -1)
    38  	m.mu.Lock()
    39  }
    40  
    41  // NestedLock locks m knowing that another lock of the same type is held.
    42  // +checklocksignore
    43  func (m *dirMutex) NestedLock(i dirlockNameIndex) {
    44  	locking.AddGLock(dirprefixIndex, int(i))
    45  	m.mu.Lock()
    46  }
    47  
    48  // Unlock unlocks m.
    49  // +checklocksignore
    50  func (m *dirMutex) Unlock() {
    51  	locking.DelGLock(dirprefixIndex, -1)
    52  	m.mu.Unlock()
    53  }
    54  
    55  // NestedUnlock unlocks m knowing that another lock of the same type is held.
    56  // +checklocksignore
    57  func (m *dirMutex) NestedUnlock(i dirlockNameIndex) {
    58  	locking.DelGLock(dirprefixIndex, int(i))
    59  	m.mu.Unlock()
    60  }
    61  
    62  // DO NOT REMOVE: The following function is automatically replaced.
    63  func dirinitLockNames() { dirlockNames = []string{"new", "replaced", "child"} }
    64  
    65  func init() {
    66  	dirinitLockNames()
    67  	dirprefixIndex = locking.NewMutexClass(reflect.TypeOf(dirMutex{}), dirlockNames)
    68  }