Make OncePer.Once reentrant

The value constructor passed to OncePer.Once may call back into
Once.  Replace the global mutex with a temporary channel stored
in the map using the key, and have callers for the same key
wait on the channel if they find it instead of a real value.

Test: TestOncePerReentrant
Change-Id: Ifa88a3c48981b5076b83f47fbdee5d26311725c6
This commit is contained in:
Colin Cross
2019-02-11 15:06:16 -08:00
parent e48ff5bbd4
commit e5cdaf9221
2 changed files with 36 additions and 11 deletions

View File

@@ -133,3 +133,14 @@ func TestNewCustomOnceKey(t *testing.T) {
t.Errorf(`second call to Once with the NewCustomOnceKey from equal key should return "a": %q`, b)
}
}
func TestOncePerReentrant(t *testing.T) {
once := OncePer{}
key1 := NewOnceKey("key")
key2 := NewOnceKey("key")
a := once.Once(key1, func() interface{} { return once.Once(key2, func() interface{} { return "a" }) })
if a != "a" {
t.Errorf(`reentrant Once should return "a": %q`, a)
}
}