Files
sealos/pkg/utils/sync/pool.go
T
2022-01-11 16:16:18 +08:00

52 lines
1.0 KiB
Go

// Copyright © 2021 sealos.
//
// 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 sync
import "sync"
type Pool struct {
queue chan int
wg *sync.WaitGroup
}
func NewPool(size int) *Pool {
if size <= 1 {
size = 1
}
return &Pool{
queue: make(chan int, size),
wg: &sync.WaitGroup{},
}
}
func (p *Pool) Add(delta int) {
for i := 0; i < delta; i++ {
p.queue <- 1
}
for i := 0; i > delta; i-- {
<-p.queue
}
p.wg.Add(delta)
}
func (p *Pool) Done() {
<-p.queue
p.wg.Done()
}
func (p *Pool) Wait() {
p.wg.Wait()
}