Skip to content

Instantly share code, notes, and snippets.

@santatic
Last active March 12, 2019 09:50
Show Gist options
  • Select an option

  • Save santatic/993c82327e37eea8f8dbf1cf2beca510 to your computer and use it in GitHub Desktop.

Select an option

Save santatic/993c82327e37eea8f8dbf1cf2beca510 to your computer and use it in GitHub Desktop.
Golang mutex debugger
type debugMutex struct {
name string
mux sync.Mutex
}
func (d *debugMutex) Lock() {
d.print("Lock")
d.mux.Lock()
d.print("Lock after")
}
func (d *debugMutex) Unlock() {
d.mux.Unlock()
d.print("Unlock")
}
func (d *debugMutex) print(l string) {
_, fn, line, _ := runtime.Caller(2)
fmt.Printf("Locker: %s:%d -> %v:%v -> %v\n", fn, line, d.name, l, getGID())
}
func getGID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment