runtime: prevent garbage collection during hashmap insertion

Inserting a key-value pair into a hashmap storing keys or values
indirectly can cause the garbage collector to find the hashmap in
an inconsistent	state.

Fixes #5074.

R=golang-dev, minux.ma, rsc
CC=golang-dev
https://golang.org/cl/7913043
This commit is contained in:
Jan Ziak 2013-03-19 22:17:39 +01:00
parent b79afe1b71
commit 54dffda2b6
3 changed files with 33 additions and 4 deletions

View file

@ -7,6 +7,7 @@ package runtime_test
import (
"os"
"runtime"
"runtime/debug"
"testing"
)
@ -82,3 +83,17 @@ func TestGcDeepNesting(t *testing.T) {
t.Fail()
}
}
func TestGcHashmapIndirection(t *testing.T) {
defer debug.SetGCPercent(debug.SetGCPercent(1))
runtime.GC()
type T struct {
a [256]int
}
m := make(map[T]T)
for i := 0; i < 2000; i++ {
var a T
a.a[0] = i
m[a] = T{}
}
}