代码拉取完成,页面将自动刷新
// Copyright 2022 Changkun Ou <changkun.de>. All rights reserved.
// Use of this source code is governed by a GPLv3 license that
// can be found in the LICENSE file.
package sched
import (
"runtime"
"sync/atomic"
)
// Pool is a worker pool.
type Pool struct {
running uint64
numWorkers int
numQueues int
tasks chan funcdata
done chan struct{}
}
type funcdata struct {
fn func()
fg func(interface{})
ar interface{}
}
// Option is a scheduler option.
type Option func(w *Pool)
// Workers is number of workers that can execute tasks concurrently.
func Workers(limit int) Option {
return func(w *Pool) {
if limit > 0 {
w.numWorkers = limit
}
}
}
// Queues is buffer capacity of the tasks channel.
func Queues(limit int) Option {
return func(w *Pool) {
if limit >= 0 {
w.numQueues = limit
}
}
}
// New creates a new task scheduler and returns a pool of workers.
func New(opts ...Option) *Pool {
n := runtime.NumCPU()
p := &Pool{
running: 0,
numWorkers: n,
numQueues: n * 100,
done: make(chan struct{}),
}
for _, opt := range opts {
opt(p)
}
p.tasks = make(chan funcdata, p.numQueues)
// Start workers
for i := 0; i < p.numWorkers; i++ {
go func() {
for d := range p.tasks {
if d.fn != nil {
d.fn()
} else {
d.fg(d.ar)
}
p.complete()
}
}()
}
return p
}
// Run runs f in the current pool.
func (p *Pool) Run(f ...func()) {
for i := range f {
p.tasks <- funcdata{fn: f[i]}
}
}
func (p *Pool) RunWithArgs(f func(args interface{}), args interface{}) {
p.tasks <- funcdata{fg: f, ar: args}
}
func (p *Pool) Add(numTasks int) int {
return int(atomic.AddUint64(&p.running, uint64(numTasks)))
}
func (p *Pool) Running() uint64 {
return atomic.LoadUint64(&p.running)
}
func (p *Pool) IsRunning() bool {
return p.Running() != 0
}
func (p *Pool) Wait() {
<-p.done
}
func (p *Pool) Release() {
close(p.tasks)
close(p.done)
}
func (p *Pool) WaitAndRelease() {
p.Wait()
p.Release()
}
func (p *Pool) complete() {
ret := atomic.AddUint64(&p.running, ^uint64(0))
if ret == 0 {
p.done <- struct{}{}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。