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

import (
Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
4
	"fmt"
Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
5
	"git.barsukov.pro/barsukov/zgo/zatomic"
Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
6
	"git.barsukov.pro/barsukov/zgo/zquit"
Vladimir Barsukov's avatar
zq buff    
Vladimir Barsukov committed
7
	"runtime"
Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
8
9
10
11
12
)

type ZQ struct {
	WorkerCounter *zatomic.Int
	WorkerFn      func(any)
Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
13
	ZQuit         *zquit.ZQuit
Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
14
15

	complete chan bool
Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
16
	Jobs     chan any
Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
17
18
}

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

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

	return q
}
Vladimir Barsukov's avatar
zq buff    
Vladimir Barsukov committed
34
35
36
37
38
func Default(fn func(any)) *ZQ {
	return DefaultN(runtime.NumCPU(), fn)
}

func DefaultN(qtyWorkers int, fn func(any)) *ZQ {
Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
39
	return New(qtyWorkers, qtyWorkers*2, fn)
Vladimir Barsukov's avatar
zq buff    
Vladimir Barsukov committed
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
70
}
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
71

Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
72
73
74
75
76
func (q *ZQ) WithZQuit(quit *zquit.ZQuit) *ZQ {
	q.ZQuit = quit
	return q
}

Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
96
				case j := <-q.Jobs:
Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
97
					q.WorkerFn(j)
Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
98
					q.ZQuit.Done()
Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
99
100
101
102
103
104
105
106
107
108
				}
			}
		}()
	}
}

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

Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
109
110
111
112
113
114
func (q *ZQ) AddJob(a any) error {
	if q.ZQuit.IsQuit() {
		return fmt.Errorf("quit")
	}

	q.ZQuit.Inc()
Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
115
	q.Jobs <- a
Vladimir Barsukov's avatar
zgo    
Vladimir Barsukov committed
116

Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
117
	return nil
Vladimir Barsukov's avatar
zgo    
Vladimir Barsukov committed
118
119
}

Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
120
121
func (q *ZQ) AddJobForce(a any) error {
	q.ZQuit.Inc()
Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
122
	q.Jobs <- a
Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
123
124
125
126

	return nil
}

Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
127
func (q *ZQ) Wait() {
Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
128
	q.ZQuit.Wait()
Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
129
130
131
132
133
134
}

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