ants/pool.go

209 lines
3.7 KiB
Go
Raw Normal View History

2018-05-19 07:28:03 +03:00
package ants
import (
"runtime"
"sync/atomic"
"sync"
2018-05-20 10:28:27 +03:00
"math"
2018-05-19 07:28:03 +03:00
)
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
2018-05-20 10:28:27 +03:00
lock sync.Mutex
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),
2018-05-20 10:28:27 +03:00
freeSignal: make(chan sig, math.MaxInt32),
2018-05-19 21:52:39 +03:00
destroy: make(chan sig, runtime.GOMAXPROCS(-1)),
2018-05-19 07:28:03 +03:00
}
2018-05-20 06:51:14 +03:00
2018-05-19 07:28:03 +03:00
return p
}
//-------------------------------------------------------------------------
2018-05-20 06:51:14 +03:00
//func (p *Pool) loop() {
// for i := 0; i < runtime.GOMAXPROCS(-1); i++ {
// go func() {
// for {
// select {
// case <-p.launchSignal:
// p.getWorker().sendTask(p.tasks.pop().(f))
// case <-p.destroy:
// return
// }
// }
// }()
// }
//}
2018-05-19 07:28:03 +03:00
func (p *Pool) Push(task f) error {
if len(p.destroy) > 0 {
return nil
}
2018-05-19 21:52:39 +03:00
w := p.getWorker()
w.sendTask(task)
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) Destroy() error {
2018-05-20 10:28:27 +03:00
p.lock.Lock()
defer p.lock.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
}
2018-05-20 10:28:27 +03:00
//func (p *Pool) newWorker() *Worker {
// var w *Worker
// if p.reachLimit() {
// <-p.freeSignal
// return p.getWorker()
// }
// wp := p.workerPool.Get()
// if wp == nil {
// w = &Worker{
// pool: p,
// task: make(chan f),
// }
// } else {
// w = wp.(*Worker)
// }
// w.run()
// atomic.AddInt32(&p.running, 1)
// return w
//}
//
//func (p *Pool) getWorker() *Worker {
// var w *Worker
// p.lock.Lock()
// workers := p.workers
// n := len(workers) - 1
// if n < 0 {
// p.lock.Unlock()
// return p.newWorker()
// } else {
// w = workers[n]
// workers[n] = nil
// p.workers = workers[:n]
// //atomic.AddInt32(&p.running, 1)
// }
// p.lock.Unlock()
// return w
//}
//func (p *Pool) newWorker() *Worker {
// var w *Worker
// if p.reachLimit() {
// <-p.freeSignal
// return p.getWorker()
// }
// wp := p.workerPool.Get()
// if wp == nil {
// w = &Worker{
// pool: p,
// task: make(chan f),
// }
// } else {
// w = wp.(*Worker)
// }
// w.run()
// atomic.AddInt32(&p.running, 1)
// return w
//}
2018-05-19 07:28:03 +03:00
func (p *Pool) getWorker() *Worker {
2018-05-20 10:28:27 +03:00
//fmt.Printf("init running workers number:%d\n", p.running)
2018-05-19 22:35:31 +03:00
var w *Worker
2018-05-20 10:28:27 +03:00
waiting := false
p.lock.Lock()
2018-05-19 22:35:31 +03:00
workers := p.workers
n := len(workers) - 1
if n < 0 {
2018-05-20 10:28:27 +03:00
//fmt.Printf("running workers number:%d\n", p.running)
if p.running >= p.capacity {
waiting = true
}
2018-05-19 22:35:31 +03:00
} else {
w = workers[n]
workers[n] = nil
p.workers = workers[:n]
2018-05-20 10:28:27 +03:00
//atomic.AddInt32(&p.running, 1)
}
p.lock.Unlock()
if waiting {
<-p.freeSignal
//p.lock.Lock()
//fmt.Println("wait for a worker")
//fmt.Println("get for a worker")
for {
p.lock.Lock()
workers = p.workers
l := len(workers) - 1
if l < 0 {
p.lock.Unlock()
continue
}
w = workers[l]
workers[l] = nil
p.workers = workers[:l]
p.lock.Unlock()
break
}
//p.lock.Unlock()
} else {
wp := p.workerPool.Get()
if wp == nil {
w = &Worker{
pool: p,
task: make(chan f),
}
w.run()
atomic.AddInt32(&p.running, 1)
} else {
w = wp.(*Worker)
}
2018-05-19 13:24:36 +03:00
}
2018-05-19 22:35:31 +03:00
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)
2018-05-20 10:28:27 +03:00
p.lock.Lock()
2018-05-19 21:52:39 +03:00
p.workers = append(p.workers, worker)
2018-05-20 10:28:27 +03:00
p.lock.Unlock()
p.freeSignal <- sig{}
//fmt.Printf("put a worker, running worker number:%d\n", p.Running())
2018-05-19 07:28:03 +03:00
}