github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/sentry/mm/active_mutex.go (about)

     1  package mm
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/metacubex/gvisor/pkg/sync"
     7  	"github.com/metacubex/gvisor/pkg/sync/locking"
     8  )
     9  
    10  // RWMutex is sync.RWMutex with the correctness validator.
    11  type activeRWMutex struct {
    12  	mu sync.RWMutex
    13  }
    14  
    15  // lockNames is a list of user-friendly lock names.
    16  // Populated in init.
    17  var activelockNames []string
    18  
    19  // lockNameIndex is used as an index passed to NestedLock and NestedUnlock,
    20  // referring to an index within lockNames.
    21  // Values are specified using the "consts" field of go_template_instance.
    22  type activelockNameIndex int
    23  
    24  // DO NOT REMOVE: The following function automatically replaced with lock index constants.
    25  const (
    26  	activeLockForked = activelockNameIndex(0)
    27  )
    28  const ()
    29  
    30  // Lock locks m.
    31  // +checklocksignore
    32  func (m *activeRWMutex) Lock() {
    33  	locking.AddGLock(activeprefixIndex, -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 *activeRWMutex) NestedLock(i activelockNameIndex) {
    40  	locking.AddGLock(activeprefixIndex, int(i))
    41  	m.mu.Lock()
    42  }
    43  
    44  // Unlock unlocks m.
    45  // +checklocksignore
    46  func (m *activeRWMutex) Unlock() {
    47  	m.mu.Unlock()
    48  	locking.DelGLock(activeprefixIndex, -1)
    49  }
    50  
    51  // NestedUnlock unlocks m knowing that another lock of the same type is held.
    52  // +checklocksignore
    53  func (m *activeRWMutex) NestedUnlock(i activelockNameIndex) {
    54  	m.mu.Unlock()
    55  	locking.DelGLock(activeprefixIndex, int(i))
    56  }
    57  
    58  // RLock locks m for reading.
    59  // +checklocksignore
    60  func (m *activeRWMutex) RLock() {
    61  	locking.AddGLock(activeprefixIndex, -1)
    62  	m.mu.RLock()
    63  }
    64  
    65  // RUnlock undoes a single RLock call.
    66  // +checklocksignore
    67  func (m *activeRWMutex) RUnlock() {
    68  	m.mu.RUnlock()
    69  	locking.DelGLock(activeprefixIndex, -1)
    70  }
    71  
    72  // RLockBypass locks m for reading without executing the validator.
    73  // +checklocksignore
    74  func (m *activeRWMutex) RLockBypass() {
    75  	m.mu.RLock()
    76  }
    77  
    78  // RUnlockBypass undoes a single RLockBypass call.
    79  // +checklocksignore
    80  func (m *activeRWMutex) RUnlockBypass() {
    81  	m.mu.RUnlock()
    82  }
    83  
    84  // DowngradeLock atomically unlocks rw for writing and locks it for reading.
    85  // +checklocksignore
    86  func (m *activeRWMutex) DowngradeLock() {
    87  	m.mu.DowngradeLock()
    88  }
    89  
    90  var activeprefixIndex *locking.MutexClass
    91  
    92  // DO NOT REMOVE: The following function is automatically replaced.
    93  func activeinitLockNames() { activelockNames = []string{"forked"} }
    94  
    95  func init() {
    96  	activeinitLockNames()
    97  	activeprefixIndex = locking.NewMutexClass(reflect.TypeOf(activeRWMutex{}), activelockNames)
    98  }