Commit f8faa7da authored by Vladimir Barsukov's avatar Vladimir Barsukov
Browse files

fix

parent a4ab9603
...@@ -18,6 +18,14 @@ func (i *Int) Add(delta int) int { ...@@ -18,6 +18,14 @@ func (i *Int) Add(delta int) int {
return int(i.v.Add(int32(delta))) return int(i.v.Add(int32(delta)))
} }
func (i *Int) Inc() int {
return i.Add(1)
}
func (i *Int) Dec() int {
return i.Add(-1)
}
func (i *Int) Load() int { func (i *Int) Load() int {
return int(i.v.Load()) return int(i.v.Load())
} }
......
...@@ -9,63 +9,63 @@ import ( ...@@ -9,63 +9,63 @@ import (
type ZQ struct { type ZQ struct {
WorkerCounter *zatomic.Int WorkerCounter *zatomic.Int
WorkerFn func(any) WorkerFn func(int, any)
ZQuit *zquit.ZQuit ZQuit *zquit.ZQuit
Jobs chan any
workerId *zatomic.Int
complete chan bool complete chan bool
Jobs chan any
} }
func New(qtyWorkers int, buff int, fn func(any)) *ZQ { func New(qtyWorkers int, buff int, fn func(int, any)) *ZQ {
q := &ZQ{ q := &ZQ{
WorkerCounter: zatomic.New(), WorkerCounter: zatomic.New(),
WorkerFn: fn, WorkerFn: fn,
ZQuit: zquit.Default(), ZQuit: zquit.Default(),
Jobs: make(chan any, buff), Jobs: make(chan any, buff),
complete: make(chan bool), complete: make(chan bool),
workerId: zatomic.New(),
} }
for i := 0; i < qtyWorkers; i++ { q.AddWorker(qtyWorkers)
q.AddWorker(1)
}
return q return q
} }
func Default(fn func(any)) *ZQ { func Default(fn func(int, any)) *ZQ {
return DefaultN(runtime.NumCPU(), fn) return DefaultN(runtime.NumCPU(), fn)
} }
func DefaultN(qtyWorkers int, fn func(any)) *ZQ { func DefaultN(qtyWorkers int, fn func(int, any)) *ZQ {
return New(qtyWorkers, qtyWorkers*2, fn) return New(qtyWorkers, qtyWorkers*2, fn)
} }
func Default2(fn func(any)) *ZQ { func Default2(fn func(int, any)) *ZQ {
return DefaultN(2, fn) return DefaultN(2, fn)
} }
func Default4(fn func(any)) *ZQ { func Default4(fn func(int, any)) *ZQ {
return DefaultN(4, fn) return DefaultN(4, fn)
} }
func Default6(fn func(any)) *ZQ { func Default6(fn func(int, any)) *ZQ {
return DefaultN(6, fn) return DefaultN(6, fn)
} }
func Default8(fn func(any)) *ZQ { func Default8(fn func(int, any)) *ZQ {
return DefaultN(8, fn) return DefaultN(8, fn)
} }
func Default12(fn func(any)) *ZQ { func Default12(fn func(int, any)) *ZQ {
return DefaultN(12, fn) return DefaultN(12, fn)
} }
func Default16(fn func(any)) *ZQ { func Default16(fn func(int, any)) *ZQ {
return DefaultN(16, fn) return DefaultN(16, fn)
} }
func Default24(fn func(any)) *ZQ { func Default24(fn func(int, any)) *ZQ {
return DefaultN(24, fn) return DefaultN(24, fn)
} }
func Default32(fn func(any)) *ZQ { func Default32(fn func(int, any)) *ZQ {
return DefaultN(32, fn) return DefaultN(32, fn)
} }
func Default48(fn func(any)) *ZQ { func Default48(fn func(int, any)) *ZQ {
return DefaultN(48, fn) return DefaultN(48, fn)
} }
func Default64(fn func(any)) *ZQ { func Default64(fn func(int, any)) *ZQ {
return DefaultN(64, fn) return DefaultN(64, fn)
} }
...@@ -84,17 +84,18 @@ func (q *ZQ) AddWorker(delta int) { ...@@ -84,17 +84,18 @@ func (q *ZQ) AddWorker(delta int) {
} }
for i := 0; i < delta; i++ { for i := 0; i < delta; i++ {
q.WorkerCounter.Add(1) q.WorkerCounter.Inc()
go func() { go func() {
defer q.WorkerCounter.Add(-1) defer q.WorkerCounter.Dec()
workerId := q.workerId.Inc()
for { for {
select { select {
case <-q.complete: case <-q.complete:
return return
case j := <-q.Jobs: case j := <-q.Jobs:
q.WorkerFn(j) q.WorkerFn(workerId, j)
q.ZQuit.Done() q.ZQuit.Done()
} }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment