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 {
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 {
return int(i.v.Load())
}
......
......@@ -9,63 +9,63 @@ import (
type ZQ struct {
WorkerCounter *zatomic.Int
WorkerFn func(any)
WorkerFn func(int, any)
ZQuit *zquit.ZQuit
Jobs chan any
workerId *zatomic.Int
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{
WorkerCounter: zatomic.New(),
WorkerFn: fn,
ZQuit: zquit.Default(),
Jobs: make(chan any, buff),
complete: make(chan bool),
workerId: zatomic.New(),
}
for i := 0; i < qtyWorkers; i++ {
q.AddWorker(1)
}
q.AddWorker(qtyWorkers)
return q
}
func Default(fn func(any)) *ZQ {
func Default(fn func(int, any)) *ZQ {
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)
}
func Default2(fn func(any)) *ZQ {
func Default2(fn func(int, any)) *ZQ {
return DefaultN(2, fn)
}
func Default4(fn func(any)) *ZQ {
func Default4(fn func(int, any)) *ZQ {
return DefaultN(4, fn)
}
func Default6(fn func(any)) *ZQ {
func Default6(fn func(int, any)) *ZQ {
return DefaultN(6, fn)
}
func Default8(fn func(any)) *ZQ {
func Default8(fn func(int, any)) *ZQ {
return DefaultN(8, fn)
}
func Default12(fn func(any)) *ZQ {
func Default12(fn func(int, any)) *ZQ {
return DefaultN(12, fn)
}
func Default16(fn func(any)) *ZQ {
func Default16(fn func(int, any)) *ZQ {
return DefaultN(16, fn)
}
func Default24(fn func(any)) *ZQ {
func Default24(fn func(int, any)) *ZQ {
return DefaultN(24, fn)
}
func Default32(fn func(any)) *ZQ {
func Default32(fn func(int, any)) *ZQ {
return DefaultN(32, fn)
}
func Default48(fn func(any)) *ZQ {
func Default48(fn func(int, any)) *ZQ {
return DefaultN(48, fn)
}
func Default64(fn func(any)) *ZQ {
func Default64(fn func(int, any)) *ZQ {
return DefaultN(64, fn)
}
......@@ -84,17 +84,18 @@ func (q *ZQ) AddWorker(delta int) {
}
for i := 0; i < delta; i++ {
q.WorkerCounter.Add(1)
q.WorkerCounter.Inc()
go func() {
defer q.WorkerCounter.Add(-1)
defer q.WorkerCounter.Dec()
workerId := q.workerId.Inc()
for {
select {
case <-q.complete:
return
case j := <-q.Jobs:
q.WorkerFn(j)
q.WorkerFn(workerId, j)
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