You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This package is used to detect deadlock in Go program.
5
+
6
+
Usage
7
+
=====
8
+
9
+
Normally, we import default `sync` package in our project like this:
10
+
11
+
```go
12
+
package myapp
13
+
14
+
import"sync"
15
+
16
+
varMyLock sync.Mutex
17
+
18
+
funcMyFunc() {
19
+
MyLock.Lock()
20
+
defer MyLock.Unlock()
21
+
22
+
// .......
23
+
}
24
+
```
25
+
26
+
Just replace the default `sync` to `github.com/funny/sync`, no need to change others:
27
+
28
+
29
+
```go
30
+
package myapp
31
+
32
+
import"github.com/funny/sync"
33
+
34
+
varMyLock sync.Mutex
35
+
36
+
funcMyFunc() {
37
+
MyLock.Lock()
38
+
defer MyLock.Unlock()
39
+
40
+
// .......
41
+
}
42
+
```
43
+
44
+
Currently, deadlock detection not yet enabled, the performance of `Mutext` and `RWMutex` just like default.
45
+
46
+
When you need to compile a deadlock detection enabled version. Just add `deadlock` tag into `go build --tags` command.
47
+
48
+
For example:
49
+
50
+
```
51
+
go build -tags 'deadlock' myproject
52
+
```
53
+
54
+
This tag used for the unit test too. Otherwise the default unit test will deadlock:
55
+
56
+
```
57
+
go test -tags 'deadlock'
58
+
```
59
+
60
+
How it works
61
+
============
62
+
63
+
When deadlock detection enabled, system will maintain a global lock waiting list, and each `Mutex` and `RWMutex` will keep owner goroutine's information.
64
+
65
+
When a goroutine will waiting a lock, system will lookup the lock owner goroutine is whether waiting for the requester goroutine in directly or indirectly.
66
+
67
+
Deadlock not only happens between two goroutines, sometimes the deadlock is a link, and deadlock happens when a goroutine repeat lock a `Mutext` too.
68
+
69
+
When deadlock happens, system will dump stack trace of the gorotuines in the deadlock link.
70
+
71
+
Because we need a global lock waiting list, so the deadlock detection will drop the performance.
72
+
73
+
So, please don't use deadlock detection in production environment.
0 commit comments