Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions deadlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,20 @@ func (m *Mutex) id() lockID {
// Unless deadlock detection is disabled, logs potential deadlocks to Opts.LogBuf,
// calling Opts.OnPotentialDeadlock on each occasion.
func (m *Mutex) Lock() {
// shortcut for disabled deadlock detection to prevent extra copying of `m.mu.Lock` to the heap
if Opts.Disable {
m.mu.Lock()
return
}

counterMu.Lock()
if m.muId == 0 {
m.muId = currID
currID++
}
counterMu.Unlock()
lock(m.mu.Lock, m, false)

lockEnabled(m.mu.Lock, m, false)
}

// Unlock unlocks the mutex.
Expand Down Expand Up @@ -114,14 +121,19 @@ func (m *RWMutex) id() lockID {
// Unless deadlock detection is disabled, logs potential deadlocks to Opts.LogBuf,
// calling Opts.OnPotentialDeadlock on each occasion.
func (m *RWMutex) Lock() {
if Opts.Disable {
m.mu.Lock()
return
}

counterMu.Lock()
if m.muId == 0 {
m.muId = currID
currID++
}
counterMu.Unlock()

lock(m.mu.Lock, m, false)
lockEnabled(m.mu.Lock, m, false)
}

// Unlock unlocks the mutex for writing. It is a run-time error if rw is
Expand All @@ -142,14 +154,19 @@ func (m *RWMutex) Unlock() {
// Unless deadlock detection is disabled, logs potential deadlocks to Opts.LogBuf,
// calling Opts.OnPotentialDeadlock on each occasion.
func (m *RWMutex) RLock() {
if Opts.Disable {
m.mu.RLock()
return
}

counterMu.Lock()
if m.muId == 0 {
m.muId = currID
currID++
}
counterMu.Unlock()

lock(m.mu.RLock, m, true)
lockEnabled(m.mu.RLock, m, true)
}

// RUnlock undoes a single RLock call;
Expand Down Expand Up @@ -189,11 +206,7 @@ func checkLockOrdering(skip int, p identifiable, gid int64) {
lo.checkLockOrdering(skip, p, gid)
}

func lock(lockFn func(), ptr identifiable, preLockCheckRecursiveLocking bool) {
if Opts.Disable {
lockFn()
return
}
func lockEnabled(lockFn func(), ptr identifiable, preLockCheckRecursiveLocking bool) {
// grab the current goroutine identifier
gid := goid.Get()
preLock(4, ptr, gid, preLockCheckRecursiveLocking)
Expand Down
21 changes: 21 additions & 0 deletions deadlock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,24 @@ func TestRWMutexDoubleRLockFail(t *testing.T) {
t.Fatalf("expected 1 deadlocks, detected %d", deadlocks)
}
}

//go:noinline
func benchRWAlloc(mu *RWMutex, res *int) {
mu.Lock()
defer mu.Unlock() // defer prefers inlining for the benchmark
*res++
}

// BenchmarkRWMutexAlloc demonstrates there is one alloc per lock invocation.
func BenchmarkRWMutexAlloc(b *testing.B) {
disable := Opts.Disable
Opts.Disable = true
defer func() {
Opts.Disable = disable
}()
var mu *RWMutex = &RWMutex{}
var res int
for i := 0; i < b.N; i++ {
benchRWAlloc(mu, &res)
}
}