diff --git a/info.go b/info.go index 0fae335..f8c0abf 100644 --- a/info.go +++ b/info.go @@ -9,6 +9,6 @@ func getg() *g // GoroutineId return id of current goroutine. // It's guaranteed to be unique globally during app's life time. func GoroutineId() int64 { - gp := getg() - return gp.goid + gp := getg() + return gp.goid } diff --git a/info_test.go b/info_test.go index 16f34ee..d1adca5 100644 --- a/info_test.go +++ b/info_test.go @@ -5,43 +5,43 @@ package goroutine import ( - "testing" - "time" - "fmt" - "math/rand" + "fmt" + "math/rand" + "testing" + "time" ) func TestGoroutineIdConsistency(t *testing.T) { - cnt := 10 - exit := make(chan error) - - for i := 0; i < cnt; i++ { - go func(n int) { - id1 := GoroutineId() - time.Sleep(time.Duration(rand.Int63n(100)) * time.Millisecond) - id2 := GoroutineId() - - if id1 != id2 { - exit <- fmt.Errorf("Inconsistent goroutine id. [old:%v] [new:%v]", id1, id2) - return - } - - exit <- nil - }(i) - } - - failed := false - - for i := 0; i < cnt; i++ { - err := <-exit - - if err != nil { - t.Logf("Found error. [err:%v]", err) - failed = true - } - } - - if failed { - t.Fatalf("Test failed.") - } + cnt := 10 + exit := make(chan error) + + for i := 0; i < cnt; i++ { + go func(n int) { + id1 := GoroutineId() + time.Sleep(time.Duration(rand.Int63n(100)) * time.Millisecond) + id2 := GoroutineId() + + if id1 != id2 { + exit <- fmt.Errorf("Inconsistent goroutine id. [old:%v] [new:%v]", id1, id2) + return + } + + exit <- nil + }(i) + } + + failed := false + + for i := 0; i < cnt; i++ { + err := <-exit + + if err != nil { + t.Logf("Found error. [err:%v]", err) + failed = true + } + } + + if failed { + t.Fatalf("Test failed.") + } } diff --git a/opaque.go b/opaque.go index 8b79a1f..3bc4666 100644 --- a/opaque.go +++ b/opaque.go @@ -6,12 +6,12 @@ package goroutine import "unsafe" -const ptrSize = 4 << (^uintptr(0) >> 63) // unsafe.Sizeof(uintptr(0)) but an ideal const -const regSize = 4 << (^uintreg(0) >> 63) // unsafe.Sizeof(uintreg(0)) but an ideal const +const ptrSize = 4 << (^uintptr(0) >> 63) // unsafe.Sizeof(uintptr(0)) but an ideal const +const regSize = 4 << (^uintreg(0) >> 63) // unsafe.Sizeof(uintreg(0)) but an ideal const -type mcache struct {} -type typeAlg struct {} -type traceBuf struct {} +type mcache struct{} +type typeAlg struct{} +type traceBuf struct{} type gcMarkWorkerMode int diff --git a/runtime2.go b/runtime2.go index 736bedc..443cf1b 100644 --- a/runtime2.go +++ b/runtime2.go @@ -129,6 +129,7 @@ type guintptr uintptr func (gp guintptr) ptr() *g { return (*g)(unsafe.Pointer(gp)) } func (gp *guintptr) set(g *g) { *gp = guintptr(unsafe.Pointer(g)) } + // func (gp *guintptr) cas(old, new guintptr) bool { // return casuintptr((*uintptr)(unsafe.Pointer(gp)), uintptr(old), uintptr(new)) // }