Skip to content

Commit 9a8e406

Browse files
committedFeb 5, 2015
Cgo respects build constraints. Simplify implementation.
1 parent 2e5faf8 commit 9a8e406

File tree

7 files changed

+24
-57
lines changed

7 files changed

+24
-57
lines changed
 

‎internal/goid13/goid13.go ‎goid_go1.3.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
// permissions and limitations under the License. See the AUTHORS file
1414
// for names of contributors.
1515

16-
// Package goid provides a function to get an identifier for the current goroutine.
17-
// This package is not compatible with go 1.4 and higher.
18-
package goid13
16+
// +build !go1.4
1917

20-
func GetGoID() int64
18+
#include <runtime.h>
19+
20+
void ·Get(int64 ret) {
21+
ret = g->goid;
22+
USED(&ret);
23+
}

‎goid_go1.3.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,8 @@
1313
// permissions and limitations under the License. See the AUTHORS file
1414
// for names of contributors.
1515

16-
// On go 1.3 (and older), we can import the goid13 package.
1716
// +build !go1.4
1817

1918
package goid
2019

21-
import "github.com/petermattis/goid/internal/goid13"
22-
23-
func GoID() int64 {
24-
return goid13.GetGoID()
25-
}
20+
func Get() int64

‎goid_go1.4.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,21 @@
1313
// permissions and limitations under the License. See the AUTHORS file
1414
// for names of contributors.
1515

16-
// On go 1.3 (and newer), we can import the goid14 package.
1716
// +build go1.4
1817

1918
package goid
2019

21-
import "github.com/petermattis/goid/internal/goid14"
20+
import "unsafe"
2221

23-
func GoID() int64 {
24-
return goid14.GetGoID()
22+
var pointerSize = unsafe.Sizeof(uintptr(0))
23+
24+
// Backdoor access to runtime·getg().
25+
func getg() uintptr // in goid_go1.4.s
26+
27+
func Get() int64 {
28+
// The goid is the 16th field in the G struct where each field is a
29+
// pointer, uintptr or padded to that size. See runtime.h from the
30+
// Go sources. I'm not aware of a cleaner way to determine the
31+
// offset.
32+
return *(*int64)(unsafe.Pointer(getg() + 16*pointerSize))
2533
}
File renamed without changes.

‎goid_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
// Parse the goid from runtime.Stack() output. Slow, but it works.
2727
var goroutineRE = regexp.MustCompile(`^goroutine\s+(\d+)\s+.*`)
2828

29-
func getGoIDSlow() int64 {
29+
func getSlow() int64 {
3030
var buf [1024]byte
3131
s := buf[0:runtime.Stack(buf[:], false)]
3232
m := goroutineRE.FindSubmatch(s)
@@ -37,12 +37,12 @@ func getGoIDSlow() int64 {
3737
return v
3838
}
3939

40-
func TestGetGoID(t *testing.T) {
40+
func TestGet(t *testing.T) {
4141
ch := make(chan *string, 100)
4242
for i := 0; i < cap(ch); i++ {
4343
go func(i int) {
44-
goid := GoID()
45-
expected := getGoIDSlow()
44+
goid := Get()
45+
expected := getSlow()
4646
if goid == expected {
4747
ch <- nil
4848
return

‎internal/goid13/goid13.c

-6
This file was deleted.

‎internal/goid14/goid14.go

-33
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.