2020-09-08 23:55:18 +01:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
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 23:55:18 +01: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 23:55:18 +01: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 23:55:18 +01:00
|
|
|
}
|