pkg/store: use a sync.Once to synchronize channel close
Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
parent
1f28f8d2fe
commit
4ed26f3116
@ -20,28 +20,20 @@ import "sync"
|
||||
|
||||
// StopCh is used to propagate the stop information of a container.
|
||||
type StopCh struct {
|
||||
mu sync.Mutex
|
||||
ch chan struct{}
|
||||
closed bool
|
||||
ch chan struct{}
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
// NewStopCh creates a stop channel. The channel is open by default.
|
||||
func NewStopCh() *StopCh {
|
||||
return &StopCh{
|
||||
ch: make(chan struct{}),
|
||||
closed: false,
|
||||
}
|
||||
return &StopCh{ch: make(chan struct{})}
|
||||
}
|
||||
|
||||
// Stop close stopCh of the container.
|
||||
func (s *StopCh) Stop() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.closed {
|
||||
return
|
||||
}
|
||||
close(s.ch)
|
||||
s.closed = true
|
||||
s.once.Do(func() {
|
||||
close(s.ch)
|
||||
})
|
||||
}
|
||||
|
||||
// Stopped return the stopCh of the container as a readonly channel.
|
||||
|
Loading…
Reference in New Issue
Block a user