gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/checklocks/test/return.go (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package test
    16  
    17  // +checklocks:tc.mu
    18  func testReturnInvalidGuard() (tc *oneGuardStruct) { // +checklocksfail
    19  	return new(oneGuardStruct)
    20  }
    21  
    22  // +checklocksrelease:tc.mu
    23  func testReturnInvalidRelease() (tc *oneGuardStruct) { // +checklocksfail
    24  	return new(oneGuardStruct)
    25  }
    26  
    27  // +checklocksacquire:tc.mu
    28  func testReturnInvalidAcquire() (tc *oneGuardStruct) {
    29  	return new(oneGuardStruct) // +checklocksfail
    30  }
    31  
    32  // +checklocksacquire:tc.mu
    33  func testReturnValidAcquire() (tc *oneGuardStruct) {
    34  	tc = new(oneGuardStruct)
    35  	tc.mu.Lock()
    36  	return tc
    37  }
    38  
    39  func testReturnAcquireCall() {
    40  	tc := testReturnValidAcquire()
    41  	tc.guardedField = 1
    42  	tc.mu.Unlock()
    43  }
    44  
    45  // +checklocksacquire:tc.val.mu
    46  // +checklocksacquire:tc.ptr.mu
    47  func testReturnValidNestedAcquire() (tc *nestedGuardStruct) {
    48  	tc = new(nestedGuardStruct)
    49  	tc.ptr = new(oneGuardStruct)
    50  	tc.val.mu.Lock()
    51  	tc.ptr.mu.Lock()
    52  	return tc
    53  }
    54  
    55  func testReturnNestedAcquireCall() {
    56  	tc := testReturnValidNestedAcquire()
    57  	tc.val.guardedField = 1
    58  	tc.ptr.guardedField = 1
    59  	tc.val.mu.Unlock()
    60  	tc.ptr.mu.Unlock()
    61  }