Skip to content

Commit 5b23de2

Browse files
committed
update for Go 1.4
1 parent 7028b93 commit 5b23de2

File tree

5 files changed

+72
-9
lines changed

5 files changed

+72
-9
lines changed

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
This project is dead since Go 1.4 (can't get goroutine id)
2-
=================================
3-
41
[English](http://github.com/funny/sync/blob/master/README_EN.md)
52
---------
63

7-
自从Go 1.4发布之后,这个项目已经跪了 (无法获得goroutine id了)
8-
================================
9-
104
[中文说明](http://github.com/funny/sync/blob/master/README_CN.md)
115
---------

README_CN.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33

44
这个包用来在开发调试期,帮助排查程序中的死锁情况。
55

6+
*** 请先运行运行时Hack脚本:[`hack.sh`](https://github.com/funny/sync/blob/master/)***
7+
8+
*** 从Go 1.4开始,我们无法在`runtime`包之外使用`goc`机制。 ***
9+
10+
*** 所以现在我们需要把`GetGoId()`函数加入到`runtime`包。 ***
11+
12+
*** 这个项目提供了一个Shell脚本用来修改和重新编译`runtime`包。***
13+
14+
*** 所以你需要一个包含代码的Go环境和Linux或Mac操作系统。 ***
15+
616
用法
717
====
818

README_EN.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ Introduction
33

44
This package is used to detect deadlock in Go program.
55

6+
*** Please run the hack script [`hack.sh`](https://github.com/funny/sync/blob/master/) before use. ***
7+
8+
*** Since Go 1.4 we can't use `goc` file outside `runtime` package. ***
9+
10+
*** So now we need to add the `GetGoId()` function into `runtime` package. ***
11+
12+
*** This project provide a shell script to hack the `runtime` package and re-compile it. ***
13+
14+
*** So you need a source-based Go environment and Linux/Mac system. ***
15+
616
Usage
717
=====
818

deadlock.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"bytes"
77
"container/list"
88
"github.com/funny/debug"
9-
"github.com/funny/goid"
9+
"runtime"
1010
"strconv"
1111
"sync"
1212
)
@@ -81,7 +81,7 @@ func (m *monitor) wait(mode byte) *lockUsage {
8181
globalMutex.Lock()
8282
defer globalMutex.Unlock()
8383

84-
waitInfo := &lockUsage{m, mode, goid.Get(), debug.StackTrace(3, 0)}
84+
waitInfo := &lockUsage{m, mode, runtime.GetGoId(), debug.StackTrace(3, 0)}
8585
waitingList[waitInfo.goid] = waitInfo
8686

8787
if m.holders == nil {
@@ -118,7 +118,7 @@ func (m *monitor) using(waitInfo *lockUsage) {
118118
}
119119

120120
func (m *monitor) release(mode byte) {
121-
id := goid.Get()
121+
id := runtime.GetGoId()
122122
for i := m.holders.Back(); i != nil; i = i.Prev() {
123123
if info := i.Value.(*lockUsage); info.goid == id && info.mode == mode {
124124
m.holders.Remove(i)

hack.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/sh
2+
3+
echo "# Hack runtime"
4+
5+
cat >> $GOROOT/src/runtime/runtime.c << EOF
6+
7+
void
8+
runtime·GetGoId(int32 ret)
9+
{
10+
ret = g->goid;
11+
USED(&ret);
12+
}
13+
14+
EOF
15+
16+
cat >> $GOROOT/src/runtime/extern.go << EOF
17+
18+
func GetGoId() int32
19+
20+
EOF
21+
22+
cd $GOROOT/src
23+
./make.bash
24+
25+
cat > $$$$.go << EOF
26+
package main
27+
28+
import (
29+
"fmt"
30+
"runtime"
31+
)
32+
33+
func main() {
34+
runtime.GetGoId()
35+
fmt.Print("done")
36+
}
37+
EOF
38+
x=`go run $$$$.go`
39+
rm $$$$.go
40+
41+
echo ""
42+
echo ""
43+
echo "---"
44+
45+
if [ $x = "done" ]; then
46+
echo "Done"
47+
else
48+
echo "Failed"
49+
fi

0 commit comments

Comments
 (0)