chanotify: do not allow adding new channels if notifier is closed
Signed-off-by: Burcu Dogan <jbd@google.com>
This commit is contained in:
parent
1ade1f6d81
commit
10d291b825
1 changed files with 5 additions and 1 deletions
|
@ -15,6 +15,7 @@ type Notifier struct {
|
||||||
|
|
||||||
m sync.Mutex // guards doneCh
|
m sync.Mutex // guards doneCh
|
||||||
doneCh map[interface{}]chan struct{}
|
doneCh map[interface{}]chan struct{}
|
||||||
|
closed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new *Notifier.
|
// New returns a new *Notifier.
|
||||||
|
@ -42,6 +43,9 @@ func (n *Notifier) killWorker(id interface{}, done chan struct{}) {
|
||||||
func (n *Notifier) Add(id interface{}, ch <-chan struct{}) {
|
func (n *Notifier) Add(id interface{}, ch <-chan struct{}) {
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
n.m.Lock()
|
n.m.Lock()
|
||||||
|
if n.closed {
|
||||||
|
panic("notifier closed; cannot add the channel")
|
||||||
|
}
|
||||||
n.doneCh[id] = done
|
n.doneCh[id] = done
|
||||||
n.m.Unlock()
|
n.m.Unlock()
|
||||||
|
|
||||||
|
@ -73,5 +77,5 @@ func (n *Notifier) Close() {
|
||||||
close(done)
|
close(done)
|
||||||
}
|
}
|
||||||
close(n.c)
|
close(n.c)
|
||||||
// TODO(jbd): Don't allow Add after Close returns.
|
n.closed = true
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue