1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package test
import (
"sync"
)
type testMethods struct {
mu sync.Mutex
// +checklocks:mu
guardedField int
}
func (t *testMethods) methodValid() {
t.mu.Lock()
t.guardedField = 1
t.mu.Unlock()
}
func (t *testMethods) methodInvalid() {
t.guardedField = 2 // +checklocksfail
}
// +checklocks:t.mu
func (t *testMethods) MethodLocked(a, b, c int) {
t.guardedField = 3
}
// +checklocksignore
func (t *testMethods) methodIgnore() {
t.guardedField = 2
}
func testMethodCallsValid(tc *testMethods) {
tc.methodValid()
}
func testMethodCallsValidPreconditions(tc *testMethods) {
tc.mu.Lock()
tc.MethodLocked(1, 2, 3)
tc.mu.Unlock()
}
func testMethodCallsInvalid(tc *testMethods) {
tc.MethodLocked(4, 5, 6) // +checklocksfail
}
func testMultipleParameters(tc1, tc2, tc3 *testMethods) {
tc1.mu.Lock()
tc1.guardedField = 1
tc2.guardedField = 2 // +checklocksfail
tc3.guardedField = 3 // +checklocksfail
tc1.mu.Unlock()
}
type testMethodsWithParameters struct {
mu sync.Mutex
// +checklocks:mu
guardedField int
}
type ptrToTestMethodsWithParameters *testMethodsWithParameters
// +checklocks:t.mu
// +checklocks:a.mu
func (t *testMethodsWithParameters) methodLockedWithParameters(a *testMethodsWithParameters, b *testMethodsWithParameters) {
t.guardedField = a.guardedField
b.guardedField = a.guardedField // +checklocksfail
}
// +checklocks:t.mu
// +checklocks:a.mu
// +checklocks:b.mu
func (t *testMethodsWithParameters) methodLockedWithPtrType(a *testMethodsWithParameters, b ptrToTestMethodsWithParameters) {
t.guardedField = a.guardedField
b.guardedField = a.guardedField
}
// +checklocks:a.mu
func standaloneFunctionWithGuard(a *testMethodsWithParameters) {
a.guardedField = 1
a.mu.Unlock()
a.guardedField = 1 // +checklocksfail
}
type testMethodsWithEmbedded struct {
mu sync.Mutex
// +checklocks:mu
guardedField int
p *testMethodsWithParameters
}
// +checklocks:t.mu
func (t *testMethodsWithEmbedded) DoLocked(a, b *testMethodsWithParameters) {
t.guardedField = 1
a.mu.Lock()
b.mu.Lock()
t.p.methodLockedWithParameters(a, b) // +checklocksfail
a.mu.Unlock()
b.mu.Unlock()
}
|