]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: start goroutine ids at 1
authorDmitriy Vyukov <dvyukov@google.com>
Wed, 16 Jul 2014 08:19:33 +0000 (12:19 +0400)
committerDmitriy Vyukov <dvyukov@google.com>
Wed, 16 Jul 2014 08:19:33 +0000 (12:19 +0400)
LGTM=rsc
R=golang-codereviews, rsc
CC=golang-codereviews, khr
https://golang.org/cl/117810043

src/pkg/runtime/crash_test.go
src/pkg/runtime/proc.c

index 8552d2fe66755c85fc560c395ed8d3b710b956b5..c61fa162f0ac704dd26ff2dc0d1352073f16b2e4 100644 (file)
@@ -167,6 +167,14 @@ func TestGoNil(t *testing.T) {
        }
 }
 
+func TestMainGoroutineId(t *testing.T) {
+       output := executeTest(t, mainGoroutineIdSource, nil)
+       want := "panic: test\n\ngoroutine 1 [running]:\n"
+       if !strings.HasPrefix(output, want) {
+               t.Fatalf("output does not start with %q:\n%s", want, output)
+       }
+}
+
 const crashSource = `
 package main
 
@@ -365,3 +373,10 @@ func main() {
        select{}
 }
 `
+
+const mainGoroutineIdSource = `
+package main
+func main() {
+       panic("test")
+}
+`
index 22ddce5bd462541a2e205d6d4b037080ae318a34..0b75415acaddb33ae1422f6d45e41b80d83a09d5 100644 (file)
@@ -1882,7 +1882,11 @@ runtime·newproc1(FuncVal *fn, byte *argp, int32 narg, int32 nret, void *callerp
        newg->gopc = (uintptr)callerpc;
        newg->status = Grunnable;
        if(p->goidcache == p->goidcacheend) {
+               // Sched.goidgen is the last allocated id,
+               // this batch must be [sched.goidgen+1, sched.goidgen+GoidCacheBatch].
+               // At startup sched.goidgen=0, so main goroutine receives goid=1.
                p->goidcache = runtime·xadd64(&runtime·sched.goidgen, GoidCacheBatch);
+               p->goidcache -= GoidCacheBatch - 1;
                p->goidcacheend = p->goidcache + GoidCacheBatch;
        }
        newg->goid = p->goidcache++;