zq.go 1.89 KB
Newer Older
Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
1
2
3
4
5
package zq

import (
	"git.barsukov.pro/barsukov/zgo/zatomic"
	"git.barsukov.pro/barsukov/zgo/zwg"
Vladimir Barsukov's avatar
zq buff    
Vladimir Barsukov committed
6
	"runtime"
Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
7
8
9
10
11
12
13
14
15
16
17
)

type ZQ struct {
	WorkerCounter *zatomic.Int
	WorkerFn      func(any)
	JobsWg        *zwg.Zwg

	complete chan bool
	jobs     chan any
}

Vladimir Barsukov's avatar
zq buff    
Vladimir Barsukov committed
18
func New(qtyWorkers int, buff int, fn func(any)) *ZQ {
Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
19
20
	q := &ZQ{
		WorkerCounter: zatomic.New(),
Vladimir Barsukov's avatar
zq buff    
Vladimir Barsukov committed
21
		WorkerFn:      fn,
Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
22
		JobsWg:        zwg.New(),
Vladimir Barsukov's avatar
zq buff    
Vladimir Barsukov committed
23
		jobs:          make(chan any, buff),
Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
24
25
26
27
28
29
30
31
32
		complete:      make(chan bool),
	}

	for i := 0; i < qtyWorkers; i++ {
		q.AddWorker(1)
	}

	return q
}
Vladimir Barsukov's avatar
zq buff    
Vladimir Barsukov committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
func Default(fn func(any)) *ZQ {
	return DefaultN(runtime.NumCPU(), fn)
}

func DefaultN(qtyWorkers int, fn func(any)) *ZQ {
	return New(qtyWorkers, 0, fn)
}
func Default2(fn func(any)) *ZQ {
	return DefaultN(2, fn)
}
func Default4(fn func(any)) *ZQ {
	return DefaultN(4, fn)
}
func Default6(fn func(any)) *ZQ {
	return DefaultN(6, fn)
}
func Default8(fn func(any)) *ZQ {
	return DefaultN(8, fn)
}
func Default12(fn func(any)) *ZQ {
	return DefaultN(12, fn)
}
func Default16(fn func(any)) *ZQ {
	return DefaultN(16, fn)
}
func Default24(fn func(any)) *ZQ {
	return DefaultN(24, fn)
}
func Default32(fn func(any)) *ZQ {
	return DefaultN(32, fn)
}
func Default48(fn func(any)) *ZQ {
	return DefaultN(48, fn)
}
func Default64(fn func(any)) *ZQ {
	return DefaultN(64, fn)
}
Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

func (q *ZQ) AddWorker(delta int) {
	if delta < 1 {
		for i := 0; i < delta*-1; i++ {
			q.complete <- true
		}

		return
	}

	for i := 0; i < delta; i++ {
		q.WorkerCounter.Add(1)

		go func() {
			defer q.WorkerCounter.Add(-1)

			for {
				select {
				case <-q.complete:
					return
				case j := <-q.jobs:
					q.WorkerFn(j)
					q.JobsWg.Done()
				}
			}
		}()
	}
}

func (q *ZQ) RemoveWorker() {
	q.AddWorker(-1)
}

Vladimir Barsukov's avatar
zgo    
Vladimir Barsukov committed
103
func (q *ZQ) AddJob(a any) *ZQ {
Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
104
	q.JobsWg.Inc()
Vladimir Barsukov's avatar
zgo    
Vladimir Barsukov committed
105
106
107
108
109
	q.jobs <- a

	return q
}

Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
110
111
112
113
114
115
116
117
func (q *ZQ) Wait() {
	q.JobsWg.Wait()
}

func (q *ZQ) Quit() {
	q.Wait()
	q.AddWorker(q.WorkerCounter.LoadInverse())
}