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