github.com/sagernet/gvisor@v0.0.0-20240428053021-e691de28565f/pkg/tcpip/stack/endpoints_by_nic_mutex.go (about)

     1  package stack
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/sagernet/gvisor/pkg/sync"
     7  	"github.com/sagernet/gvisor/pkg/sync/locking"
     8  )
     9  
    10  // RWMutex is sync.RWMutex with the correctness validator.
    11  type endpointsByNICRWMutex struct {
    12  	mu sync.RWMutex
    13  }
    14  
    15  // lockNames is a list of user-friendly lock names.
    16  // Populated in init.
    17  var endpointsByNIClockNames []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 endpointsByNIClockNameIndex int
    23  
    24  // DO NOT REMOVE: The following function automatically replaced with lock index constants.
    25  // LOCK_NAME_INDEX_CONSTANTS
    26  const ()
    27  
    28  // Lock locks m.
    29  // +checklocksignore
    30  func (m *endpointsByNICRWMutex) Lock() {
    31  	locking.AddGLock(endpointsByNICprefixIndex, -1)
    32  	m.mu.Lock()
    33  }
    34  
    35  // NestedLock locks m knowing that another lock of the same type is held.
    36  // +checklocksignore
    37  func (m *endpointsByNICRWMutex) NestedLock(i endpointsByNIClockNameIndex) {
    38  	locking.AddGLock(endpointsByNICprefixIndex, int(i))
    39  	m.mu.Lock()
    40  }
    41  
    42  // Unlock unlocks m.
    43  // +checklocksignore
    44  func (m *endpointsByNICRWMutex) Unlock() {
    45  	m.mu.Unlock()
    46  	locking.DelGLock(endpointsByNICprefixIndex, -1)
    47  }
    48  
    49  // NestedUnlock unlocks m knowing that another lock of the same type is held.
    50  // +checklocksignore
    51  func (m *endpointsByNICRWMutex) NestedUnlock(i endpointsByNIClockNameIndex) {
    52  	m.mu.Unlock()
    53  	locking.DelGLock(endpointsByNICprefixIndex, int(i))
    54  }
    55  
    56  // RLock locks m for reading.
    57  // +checklocksignore
    58  func (m *endpointsByNICRWMutex) RLock() {
    59  	locking.AddGLock(endpointsByNICprefixIndex, -1)
    60  	m.mu.RLock()
    61  }
    62  
    63  // RUnlock undoes a single RLock call.
    64  // +checklocksignore
    65  func (m *endpointsByNICRWMutex) RUnlock() {
    66  	m.mu.RUnlock()
    67  	locking.DelGLock(endpointsByNICprefixIndex, -1)
    68  }
    69  
    70  // RLockBypass locks m for reading without executing the validator.
    71  // +checklocksignore
    72  func (m *endpointsByNICRWMutex) RLockBypass() {
    73  	m.mu.RLock()
    74  }
    75  
    76  // RUnlockBypass undoes a single RLockBypass call.
    77  // +checklocksignore
    78  func (m *endpointsByNICRWMutex) RUnlockBypass() {
    79  	m.mu.RUnlock()
    80  }
    81  
    82  // DowngradeLock atomically unlocks rw for writing and locks it for reading.
    83  // +checklocksignore
    84  func (m *endpointsByNICRWMutex) DowngradeLock() {
    85  	m.mu.DowngradeLock()
    86  }
    87  
    88  var endpointsByNICprefixIndex *locking.MutexClass
    89  
    90  // DO NOT REMOVE: The following function is automatically replaced.
    91  func endpointsByNICinitLockNames() {}
    92  
    93  func init() {
    94  	endpointsByNICinitLockNames()
    95  	endpointsByNICprefixIndex = locking.NewMutexClass(reflect.TypeOf(endpointsByNICRWMutex{}), endpointsByNIClockNames)
    96  }