Last active
March 12, 2019 09:50
-
-
Save santatic/993c82327e37eea8f8dbf1cf2beca510 to your computer and use it in GitHub Desktop.
Golang mutex debugger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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