File tree 7 files changed +24
-57
lines changed
7 files changed +24
-57
lines changed Original file line number Diff line number Diff line change 13
13
// permissions and limitations under the License. See the AUTHORS file
14
14
// for names of contributors.
15
15
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
19
17
20
- func GetGoID () int64
18
+ #include <runtime.h>
19
+
20
+ void ·Get (int64 ret ) {
21
+ ret = g -> goid ;
22
+ USED (& ret );
23
+ }
Original file line number Diff line number Diff line change 13
13
// permissions and limitations under the License. See the AUTHORS file
14
14
// for names of contributors.
15
15
16
- // On go 1.3 (and older), we can import the goid13 package.
17
16
// +build !go1.4
18
17
19
18
package goid
20
19
21
- import "github.com/petermattis/goid/internal/goid13"
22
-
23
- func GoID () int64 {
24
- return goid13 .GetGoID ()
25
- }
20
+ func Get () int64
Original file line number Diff line number Diff line change 13
13
// permissions and limitations under the License. See the AUTHORS file
14
14
// for names of contributors.
15
15
16
- // On go 1.3 (and newer), we can import the goid14 package.
17
16
// +build go1.4
18
17
19
18
package goid
20
19
21
- import "github.com/petermattis/goid/internal/goid14 "
20
+ import "unsafe "
22
21
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 ))
25
33
}
File renamed without changes.
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ import (
26
26
// Parse the goid from runtime.Stack() output. Slow, but it works.
27
27
var goroutineRE = regexp .MustCompile (`^goroutine\s+(\d+)\s+.*` )
28
28
29
- func getGoIDSlow () int64 {
29
+ func getSlow () int64 {
30
30
var buf [1024 ]byte
31
31
s := buf [0 :runtime .Stack (buf [:], false )]
32
32
m := goroutineRE .FindSubmatch (s )
@@ -37,12 +37,12 @@ func getGoIDSlow() int64 {
37
37
return v
38
38
}
39
39
40
- func TestGetGoID (t * testing.T ) {
40
+ func TestGet (t * testing.T ) {
41
41
ch := make (chan * string , 100 )
42
42
for i := 0 ; i < cap (ch ); i ++ {
43
43
go func (i int ) {
44
- goid := GoID ()
45
- expected := getGoIDSlow ()
44
+ goid := Get ()
45
+ expected := getSlow ()
46
46
if goid == expected {
47
47
ch <- nil
48
48
return
Load Diff This file was deleted.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments