Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions goid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !amd64

package goid

func Goid() int64 {
return goidFallback()
}
2 changes: 1 addition & 1 deletion goid_amd64_1_9.s
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build go1.9
// +build go1.9,amd64

#include "textflag.h"

Expand Down
15 changes: 15 additions & 0 deletions goid_fallback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package goid

import (
"bytes"
"runtime"
"strconv"
)

func goidFallback() int64 {
buf := make([]byte, 20)
buf = buf[:runtime.Stack(buf, false)]
goidStr := string(bytes.Split(buf, []byte(" "))[1])
result, _ := strconv.ParseInt(goidStr, 10, 64)
return result
}
13 changes: 4 additions & 9 deletions goid_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package goid

import (
"bytes"
"fmt"
"runtime"
"sync"
"testing"
)
Expand All @@ -13,12 +10,10 @@ func TestGoid(t *testing.T) {
wg.Add(10000)
for i := 0; i < 10000; i++ {
go func() {
buf := make([]byte, 20)
buf = buf[:runtime.Stack(buf, false)]
goidRuntime := string(bytes.Split(buf, []byte(" "))[1])
goidAsm := fmt.Sprintf("%d", Goid())
if goidRuntime != goidAsm {
t.Errorf("goid from runtime is %s, goid from asm is %s", goidRuntime, goidAsm)
goidRuntime := goidFallback()
goidAsm := Goid()
if goidAsm == 0 || goidRuntime != goidAsm {
t.Errorf("goid from runtime is %d, goid from asm is %d", goidRuntime, goidAsm)
}
wg.Done()
}()
Expand Down