1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package shutdown
- import (
- "sync"
- )
- type Shutdown struct {
- doing bool
- ending bool
- startCh chan struct{}
- doneCh chan struct{}
- mu sync.Mutex
- }
- func New() *Shutdown {
- return &Shutdown{
- doing: false,
- ending: false,
- startCh: make(chan struct{}),
- doneCh: make(chan struct{}),
- }
- }
- func (s *Shutdown) Start() {
- s.mu.Lock()
- defer s.mu.Unlock()
- if !s.doing {
- s.doing = true
- close(s.startCh)
- }
- }
- func (s *Shutdown) WaitStart() {
- <-s.startCh
- }
- func (s *Shutdown) Done() {
- s.mu.Lock()
- defer s.mu.Unlock()
- if !s.ending {
- s.ending = true
- close(s.doneCh)
- }
- }
- func (s *Shutdown) WaitDone() {
- <-s.doneCh
- }
|