frp/utils/shutdown/shutdown.go

63 lines
1.2 KiB
Go
Raw Normal View History

2017-03-08 19:03:47 +01:00
// Copyright 2017 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shutdown
import (
"sync"
)
type Shutdown struct {
2018-01-16 18:09:33 +01:00
doing bool
ending bool
startCh chan struct{}
doneCh chan struct{}
mu sync.Mutex
2017-03-08 19:03:47 +01:00
}
func New() *Shutdown {
return &Shutdown{
2018-01-16 18:09:33 +01:00
doing: false,
ending: false,
startCh: make(chan struct{}),
doneCh: make(chan struct{}),
2017-03-08 19:03:47 +01:00
}
}
func (s *Shutdown) Start() {
s.mu.Lock()
defer s.mu.Unlock()
if !s.doing {
s.doing = true
2018-01-16 18:09:33 +01:00
close(s.startCh)
2017-03-08 19:03:47 +01:00
}
}
func (s *Shutdown) WaitStart() {
2018-01-16 18:09:33 +01:00
<-s.startCh
2017-03-08 19:03:47 +01:00
}
func (s *Shutdown) Done() {
s.mu.Lock()
defer s.mu.Unlock()
if !s.ending {
s.ending = true
2018-01-16 18:09:33 +01:00
close(s.doneCh)
2017-03-08 19:03:47 +01:00
}
}
2018-01-16 18:09:33 +01:00
func (s *Shutdown) WaitDone() {
<-s.doneCh
2017-03-08 19:03:47 +01:00
}