]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: run init on main thread
authorRuss Cox <rsc@golang.org>
Thu, 1 Mar 2012 16:48:17 +0000 (11:48 -0500)
committerRuss Cox <rsc@golang.org>
Thu, 1 Mar 2012 16:48:17 +0000 (11:48 -0500)
Fixes #3125.

R=golang-dev, r, minux.ma
CC=golang-dev
https://golang.org/cl/5714049

src/pkg/runtime/mheap.c
src/pkg/runtime/proc.c
src/pkg/runtime/runtime_linux_test.go [new file with mode: 0644]

index 761246a47f0aaf8c1f1734e164ae7955ac107173..c877bfca91b288dc4b9db051101bcc6c5204faa6 100644 (file)
@@ -326,7 +326,7 @@ MHeap_FreeLocked(MHeap *h, MSpan *s)
 }
 
 // Release (part of) unused memory to OS.
-// Goroutine created in runtime·schedinit.
+// Goroutine created at startup.
 // Loop forever.
 void
 runtime·MHeap_Scavenger(void)
index ddac048a000b355eba20b54edd8a272ee28e3dfc..de7090c527eb28c1f23a15b3df9fbe6cec0f84f3 100644 (file)
@@ -209,8 +209,6 @@ runtime·schedinit(void)
 
        mstats.enablegc = 1;
        m->nomemprof--;
-
-       scvg = runtime·newproc1((byte*)runtime·MHeap_Scavenger, nil, 0, 0, runtime·schedinit);
 }
 
 extern void main·init(void);
@@ -228,6 +226,7 @@ runtime·main(void)
        // to preserve the lock.
        runtime·LockOSThread();
        runtime·sched.init = true;
+       scvg = runtime·newproc1((byte*)runtime·MHeap_Scavenger, nil, 0, 0, runtime·main);
        main·init();
        runtime·sched.init = false;
        if(!runtime·sched.lockmain)
@@ -587,10 +586,11 @@ top:
                mput(m);
        }
 
-       // Look for deadlock situation: one single active g which happens to be scvg.
-       if(runtime·sched.grunning == 1 && runtime·sched.gwait == 0) {
-               if(scvg->status == Grunning || scvg->status == Gsyscall)
-                       runtime·throw("all goroutines are asleep - deadlock!");
+       // Look for deadlock situation.
+       if((scvg == nil && runtime·sched.grunning == 0) ||
+          (scvg != nil && runtime·sched.grunning == 1 && runtime·sched.gwait == 0 &&
+           (scvg->status == Grunning || scvg->status == Gsyscall))) {
+               runtime·throw("all goroutines are asleep - deadlock!");
        }
 
        m->nextg = nil;
diff --git a/src/pkg/runtime/runtime_linux_test.go b/src/pkg/runtime/runtime_linux_test.go
new file mode 100644 (file)
index 0000000..5344ed2
--- /dev/null
@@ -0,0 +1,29 @@
+// Copyright 2012 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package runtime_test
+
+import (
+       . "runtime"
+       "syscall"
+       "testing"
+)
+
+var pid, tid int
+
+func init() {
+       // Record pid and tid of init thread for use during test.
+       // The call to LockOSThread is just to exercise it;
+       // we can't test that it does anything.
+       // Instead we're testing that the conditions are good
+       // for how it is used in init (must be on main thread).
+       pid, tid = syscall.Getpid(), syscall.Gettid()
+       LockOSThread()
+}
+
+func TestLockOSThread(t *testing.T) {
+       if pid != tid {
+               t.Fatalf("pid=%d but tid=%d", pid, tid)
+       }
+}