github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/pkg/sentry/fsimpl/kernfs/deferred_dec_refs_mutex.go (about)

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