Skip to content

Commit b11a549

Browse files
committedDec 1, 2016
Implement slow fallback
Also adds Travis CI and a bunch of missing license headers.
1 parent 8afecda commit b11a549

9 files changed

+116
-20
lines changed
 

‎.travis.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: go
2+
3+
go:
4+
- 1.3
5+
- 1.4
6+
- 1.5
7+
- 1.6
8+
- 1.7

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# goid
22

3-
Demo of how to programatically retrieve the current goroutine's
4-
ID. Works for both pre-go1.4 and post-go1.4.
3+
Programatically retrieve the current goroutine's ID. See [the CI
4+
configuration](.travis.yml) for supported Go versions.

‎goid.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2016 Peter Mattis.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License. See the AUTHORS file
14+
// for names of contributors.
15+
16+
package goid
17+
18+
import (
19+
"bytes"
20+
"runtime"
21+
"strconv"
22+
)
23+
24+
func ExtractGID(s []byte) int64 {
25+
s = s[len("goroutine "):]
26+
s = s[:bytes.IndexByte(s, ' ')]
27+
gid, _ := strconv.ParseInt(string(s), 10, 64)
28+
return gid
29+
}
30+
31+
// Parse the goid from runtime.Stack() output. Slow, but it works.
32+
func getSlow() int64 {
33+
var buf [64]byte
34+
return ExtractGID(buf[:runtime.Stack(buf[:], false)])
35+
}

‎goid_go1.5.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1-
// +build go1.5, !go1.6
1+
// Copyright 2016 Peter Mattis.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License. See the AUTHORS file
14+
// for names of contributors.
15+
16+
// +build amd64 amd64p32 arm
17+
// +build go1.5,!go1.6
218

319
package goid
420

‎goid_go1.5plus.s

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Copyright 2016 Peter Mattis.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License. See the AUTHORS file
14+
// for names of contributors.
15+
116
// Assembly to mimic runtime.getg.
217

318
// +build amd64 amd64p32

‎goid_go1.5plus_arm.s

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Copyright 2016 Peter Mattis.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License. See the AUTHORS file
14+
// for names of contributors.
15+
116
// Assembly to mimic runtime.getg.
217
// This should work on arm64 as well, but it hasn't been tested.
318

‎goid_go1.6plus.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// +build amd64 amd64p32 arm
12
// +build go1.6
23

34
package goid

‎goid_slow.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2016 Peter Mattis.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License. See the AUTHORS file
14+
// for names of contributors.
15+
16+
// +build go1.4,!go1.5,!amd64,!amd64p32,!arm,!386 go1.5,!go1.6,!amd64,!amd64p32,!arm go1.6,!amd64,!amd64p32,!arm
17+
18+
package goid
19+
20+
// Get returns the id of the current goroutine.
21+
func Get() int64 {
22+
return getSlow()
23+
}

‎goid_test.go

-17
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,9 @@ package goid
1717

1818
import (
1919
"fmt"
20-
"regexp"
21-
"runtime"
22-
"strconv"
2320
"testing"
2421
)
2522

26-
// Parse the goid from runtime.Stack() output. Slow, but it works.
27-
var goroutineRE = regexp.MustCompile(`^goroutine\s+(\d+)\s+.*`)
28-
29-
func getSlow() int64 {
30-
var buf [1024]byte
31-
s := buf[0:runtime.Stack(buf[:], false)]
32-
m := goroutineRE.FindSubmatch(s)
33-
if m == nil {
34-
return -1
35-
}
36-
v, _ := strconv.ParseInt(string(m[1]), 10, 64)
37-
return v
38-
}
39-
4023
func TestGet(t *testing.T) {
4124
ch := make(chan *string, 100)
4225
for i := 0; i < cap(ch); i++ {

0 commit comments

Comments
 (0)
Please sign in to comment.