2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-09-08 22:55:18 +00:00
|
|
|
|
|
|
|
package syncs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AssertLocked panics if m is not locked.
|
|
|
|
func AssertLocked(m *sync.Mutex) {
|
2022-03-17 00:02:16 +00:00
|
|
|
if m.TryLock() {
|
|
|
|
m.Unlock()
|
2020-09-08 22:55:18 +00:00
|
|
|
panic("mutex is not locked")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AssertRLocked panics if rw is not locked for reading or writing.
|
|
|
|
func AssertRLocked(rw *sync.RWMutex) {
|
2022-03-17 00:02:16 +00:00
|
|
|
if rw.TryLock() {
|
|
|
|
rw.Unlock()
|
|
|
|
panic("mutex is not locked")
|
2020-09-08 22:55:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AssertWLocked panics if rw is not locked for writing.
|
|
|
|
func AssertWLocked(rw *sync.RWMutex) {
|
2022-03-17 00:02:16 +00:00
|
|
|
if rw.TryRLock() {
|
|
|
|
rw.RUnlock()
|
|
|
|
panic("mutex is not rlocked")
|
|
|
|
}
|
2020-09-08 22:55:18 +00:00
|
|
|
}
|