Public
Snippet $288 authored by Kirill Smelkov

Trace sync.Mutex Lock/Unlock

mutex-trace.go
package debug
  
import (
        "fmt"
        "path"
        "sync"

        "lab.nexedi.com/kirr/go123/xruntime"
)


// Mutex is sync.Mutex that traces every Lock/Unlock operation
type Mutex struct {
        sync.Mutex
}

func (m *Mutex) Lock() {
        tb := xruntime.Traceback(2)
        s := fmt.Sprintf("%p Lock\n", m)
        for i := 0; i < 2; i++ {
                who := tb[i]
                s += fmt.Sprintf("\t%s\t%s:%d\n", path.Base(who.Function), path.Base(who.File), who.Line)
        }
        fmt.Println(s)

        m.Mutex.Lock()
}

func (m *Mutex) Unlock() {
        tb := xruntime.Traceback(2)
        s := fmt.Sprintf("%p Unlock\n", m)
        for i := 0; i < 2; i++ {
                who := tb[i]
                s += fmt.Sprintf("\t%s\t%s:%d\n", path.Base(who.Function), path.Base(who.File), who.Line)
        }
        fmt.Println(s)

        m.Mutex.Unlock()
}