ants/pool.go

125 lines
2.1 KiB
Go
Raw Normal View History

2018-05-19 07:28:03 +03:00
package ants
import (
"runtime"
"sync/atomic"
"sync"
)
type sig struct{}
type f func()
type Pool struct {
2018-05-19 21:52:39 +03:00
capacity int32
running int32
freeSignal chan sig
workers []*Worker
workerPool sync.Pool
destroy chan sig
m sync.Mutex
wg *sync.WaitGroup
2018-05-19 07:28:03 +03:00
}
func NewPool(size int) *Pool {
p := &Pool{
2018-05-19 21:52:39 +03:00
capacity: int32(size),
freeSignal: make(chan sig, size),
destroy: make(chan sig, runtime.GOMAXPROCS(-1)),
wg: &sync.WaitGroup{},
2018-05-19 07:28:03 +03:00
}
return p
}
//-------------------------------------------------------------------------
func (p *Pool) Push(task f) error {
if len(p.destroy) > 0 {
return nil
}
p.wg.Add(1)
2018-05-19 21:52:39 +03:00
w := p.getWorker()
w.sendTask(task)
//p.launchSignal <- sig{}
2018-05-19 07:28:03 +03:00
return nil
}
2018-05-19 21:52:39 +03:00
2018-05-19 10:22:14 +03:00
func (p *Pool) Running() int {
return int(atomic.LoadInt32(&p.running))
2018-05-19 07:57:01 +03:00
}
2018-05-19 10:22:14 +03:00
func (p *Pool) Free() int {
return int(atomic.LoadInt32(&p.capacity) - atomic.LoadInt32(&p.running))
2018-05-19 07:28:03 +03:00
}
2018-05-19 10:22:14 +03:00
func (p *Pool) Cap() int {
return int(atomic.LoadInt32(&p.capacity))
2018-05-19 07:28:03 +03:00
}
func (p *Pool) Wait() {
p.wg.Wait()
}
2018-05-19 13:24:36 +03:00
2018-05-19 07:28:03 +03:00
func (p *Pool) Destroy() error {
p.m.Lock()
defer p.m.Unlock()
2018-05-19 07:57:01 +03:00
for i := 0; i < runtime.GOMAXPROCS(-1)+1; i++ {
2018-05-19 07:28:03 +03:00
p.destroy <- sig{}
}
return nil
}
//-------------------------------------------------------------------------
func (p *Pool) reachLimit() bool {
2018-05-19 07:57:01 +03:00
return p.Running() >= p.Cap()
2018-05-19 07:28:03 +03:00
}
func (p *Pool) newWorker() *Worker {
2018-05-19 22:35:31 +03:00
var w *Worker
2018-05-19 13:24:36 +03:00
if p.reachLimit() {
<-p.freeSignal
return p.getWorker()
}
2018-05-19 22:35:31 +03:00
wp := p.workerPool.Get()
if wp == nil {
w = &Worker{
pool: p,
task: make(chan f),
}
} else {
w = wp.(*Worker)
2018-05-19 07:28:03 +03:00
}
2018-05-19 22:35:31 +03:00
w.run()
2018-05-19 20:38:50 +03:00
atomic.AddInt32(&p.running, 1)
2018-05-19 22:35:31 +03:00
return w
2018-05-19 07:28:03 +03:00
}
func (p *Pool) getWorker() *Worker {
2018-05-19 22:35:31 +03:00
var w *Worker
p.m.Lock()
workers := p.workers
n := len(workers) - 1
if n < 0 {
p.m.Unlock()
return p.newWorker()
} else {
w = workers[n]
workers[n] = nil
p.workers = workers[:n]
atomic.AddInt32(&p.running, 1)
2018-05-19 13:24:36 +03:00
}
2018-05-19 22:35:31 +03:00
p.m.Unlock()
return w
2018-05-19 13:24:36 +03:00
}
2018-05-19 20:38:50 +03:00
func (p *Pool) putWorker(worker *Worker) {
2018-05-19 21:52:39 +03:00
p.workerPool.Put(worker)
p.m.Lock()
p.workers = append(p.workers, worker)
2018-05-19 08:09:44 +03:00
if p.reachLimit() {
2018-05-19 13:24:36 +03:00
p.freeSignal <- sig{}
2018-05-19 07:28:03 +03:00
}
2018-05-19 21:52:39 +03:00
p.m.Unlock()
2018-05-19 07:28:03 +03:00
}