diff options
author | Adin Scannell <ascannell@google.com> | 2021-10-18 11:07:11 -0700 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2021-10-18 11:09:39 -0700 |
commit | 4f6cda4d0ed8694a1b09d27a039eb4e57432bc75 (patch) | |
tree | 33fce0369425e33bce8a4fd2664b69cc04d8e3ce /tools/checklocks/test | |
parent | ae8f93e555cdce4b8d423342011b1043f1b470a6 (diff) |
Support distinction for RWMutex and read-only locks.
Fixes #6590
PiperOrigin-RevId: 404007524
Diffstat (limited to 'tools/checklocks/test')
-rw-r--r-- | tools/checklocks/test/BUILD | 1 | ||||
-rw-r--r-- | tools/checklocks/test/basics.go | 6 | ||||
-rw-r--r-- | tools/checklocks/test/rwmutex.go | 52 |
3 files changed, 56 insertions, 3 deletions
diff --git a/tools/checklocks/test/BUILD b/tools/checklocks/test/BUILD index d4d98c256..f2ea6c7c6 100644 --- a/tools/checklocks/test/BUILD +++ b/tools/checklocks/test/BUILD @@ -16,6 +16,7 @@ go_library( "methods.go", "parameters.go", "return.go", + "rwmutex.go", "test.go", ], ) diff --git a/tools/checklocks/test/basics.go b/tools/checklocks/test/basics.go index 7a773171f..e941fba5b 100644 --- a/tools/checklocks/test/basics.go +++ b/tools/checklocks/test/basics.go @@ -108,14 +108,14 @@ type rwGuardStruct struct { func testRWValidRead(tc *rwGuardStruct) { tc.rwMu.Lock() - tc.guardedField = 1 + _ = tc.guardedField tc.rwMu.Unlock() } func testRWValidWrite(tc *rwGuardStruct) { - tc.rwMu.RLock() + tc.rwMu.Lock() tc.guardedField = 2 - tc.rwMu.RUnlock() + tc.rwMu.Unlock() } func testRWInvalidWrite(tc *rwGuardStruct) { diff --git a/tools/checklocks/test/rwmutex.go b/tools/checklocks/test/rwmutex.go new file mode 100644 index 000000000..d27ed10e3 --- /dev/null +++ b/tools/checklocks/test/rwmutex.go @@ -0,0 +1,52 @@ +// Copyright 2021 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" +) + +// oneReadGuardStruct has one read-guarded field. +type oneReadGuardStruct struct { + mu sync.RWMutex + // +checklocks:mu + guardedField int +} + +func testRWAccessValidRead(tc *oneReadGuardStruct) { + tc.mu.Lock() + _ = tc.guardedField + tc.mu.Unlock() + tc.mu.RLock() + _ = tc.guardedField + tc.mu.RUnlock() +} + +func testRWAccessValidWrite(tc *oneReadGuardStruct) { + tc.mu.Lock() + tc.guardedField = 1 + tc.mu.Unlock() +} + +func testRWAccessInvalidWrite(tc *oneReadGuardStruct) { + tc.guardedField = 2 // +checklocksfail + tc.mu.RLock() + tc.guardedField = 2 // +checklocksfail + tc.mu.RUnlock() +} + +func testRWAccessInvalidRead(tc *oneReadGuardStruct) { + _ = tc.guardedField // +checklocksfail +} |