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:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user