zq.go 2.04 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
16
17
18

	complete chan bool
	jobs     chan any
}

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
zq buff    
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
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
70
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
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
96
97
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)
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
zgo    
Vladimir Barsukov committed
115
116
	q.jobs <- a

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
zgo  
Vladimir Barsukov committed
120
func (q *ZQ) Wait() {
Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
121
	q.ZQuit.Wait()
Vladimir Barsukov's avatar
zgo  
Vladimir Barsukov committed
122
123
124
125
126
127
}

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