forked from mirror/ants
106 lines
1.8 KiB
Go
106 lines
1.8 KiB
Go
package ants
|
|
|
|
import (
|
|
"runtime"
|
|
"sync/atomic"
|
|
"math"
|
|
"sync"
|
|
)
|
|
|
|
type sig struct{}
|
|
|
|
type f func()
|
|
|
|
type Pool struct {
|
|
capacity int32
|
|
free int32
|
|
tasks chan f
|
|
workers chan *Worker
|
|
destroy chan sig
|
|
m sync.Mutex
|
|
}
|
|
|
|
func NewPool(size int) *Pool {
|
|
p := &Pool{
|
|
capacity: int32(size),
|
|
free: int32(size),
|
|
tasks: make(chan f, math.MaxInt32),
|
|
//workers: &sync.Pool{New: func() interface{} { return &Worker{} }},
|
|
workers: make(chan *Worker, size),
|
|
destroy: make(chan sig, runtime.GOMAXPROCS(-1)),
|
|
}
|
|
p.loop()
|
|
return p
|
|
}
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
func (p *Pool) loop() {
|
|
for i := 0; i < runtime.GOMAXPROCS(-1); i++ {
|
|
go func() {
|
|
for {
|
|
select {
|
|
case task := <-p.tasks:
|
|
p.getWorker().sendTask(task)
|
|
case <-p.destroy:
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
}
|
|
|
|
func (p *Pool) Push(task f) error {
|
|
if len(p.destroy) > 0 {
|
|
return nil
|
|
}
|
|
p.tasks <- task
|
|
return nil
|
|
}
|
|
func (p *Pool) Running() int32 {
|
|
return atomic.LoadInt32(&p.capacity) - atomic.LoadInt32(&p.free)
|
|
}
|
|
|
|
func (p *Pool) Free() int32 {
|
|
return atomic.LoadInt32(&p.free)
|
|
}
|
|
|
|
func (p *Pool) Cap() int32 {
|
|
return atomic.LoadInt32(&p.capacity)
|
|
}
|
|
|
|
func (p *Pool) Destroy() error {
|
|
p.m.Lock()
|
|
defer p.m.Unlock()
|
|
for i := 0; i < runtime.GOMAXPROCS(-1)+1; i++ {
|
|
p.destroy <- sig{}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
func (p *Pool) reachLimit() bool {
|
|
return p.Running() >= p.Cap()
|
|
}
|
|
|
|
func (p *Pool) newWorker() *Worker {
|
|
worker := &Worker{
|
|
pool: p,
|
|
task: make(chan f),
|
|
exit: make(chan sig),
|
|
}
|
|
worker.run()
|
|
atomic.AddInt32(&p.free, -1)
|
|
return worker
|
|
}
|
|
|
|
func (p *Pool) getWorker() *Worker {
|
|
var worker *Worker
|
|
if p.reachLimit() || p.Free() > 0 {
|
|
worker = <-p.workers
|
|
}
|
|
worker = p.newWorker()
|
|
return worker
|
|
}
|