runtime: fix deadlock in runtime.Stack

It shouldn't semacquire() inside an acquirem(), the runtime
thinks that means deadlock.  It actually isn't a deadlock, but it
looks like it because acquirem() does m.locks++.

Candidate for inclusion in 1.4.1.  runtime.Stack with all=true
is pretty unuseable in GOMAXPROCS>1 environment.

fixes #9321

Change-Id: Iac6b664217d24763b9878c20e49229a1ecffc805
Reviewed-on: https://go-review.googlesource.com/1600
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
diff --git a/test/fixedbugs/issue9321.go b/test/fixedbugs/issue9321.go
new file mode 100644
index 0000000..06cb5a6
--- /dev/null
+++ b/test/fixedbugs/issue9321.go
@@ -0,0 +1,37 @@
+// run
+
+// Copyright 2014 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 main
+
+import (
+	"bytes"
+	"runtime"
+	"runtime/pprof"
+	"sync"
+)
+
+func test() {
+	var wg sync.WaitGroup
+	wg.Add(2)
+	test := func() {
+		for i := 0; i < 100; i++ {
+			buf := &bytes.Buffer{}
+			pprof.Lookup("goroutine").WriteTo(buf, 2)
+		}
+		wg.Done()
+	}
+
+	go test()
+	go test()
+	wg.Wait()
+}
+
+func main() {
+	runtime.GOMAXPROCS(2)
+	for i := 0; i < 100; i++ {
+		test()
+	}
+}