-
Notifications
You must be signed in to change notification settings - Fork 4
/
threads.go
109 lines (104 loc) · 2.13 KB
/
threads.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
103
104
105
106
107
108
109
package dads
import (
"runtime"
"sync"
)
var (
// MT - are we running in multiple threading mode?
MT = false
thrN = 0
thrNMtx = &sync.Mutex{}
)
// SetMT - we're in multithreaded mode, setup global caches mutexes
func SetMT() {
if uuidsNonEmptyCacheMtx == nil {
uuidsNonEmptyCacheMtx = &sync.RWMutex{}
}
if uuidsAffsCacheMtx == nil {
uuidsAffsCacheMtx = &sync.RWMutex{}
}
if identityCacheMtx == nil {
identityCacheMtx = &sync.RWMutex{}
}
if rollsCacheMtx == nil {
rollsCacheMtx = &sync.RWMutex{}
}
if i2uCacheMtx == nil {
i2uCacheMtx = &sync.RWMutex{}
}
if emailsCacheMtx == nil {
emailsCacheMtx = &sync.RWMutex{}
}
if esCacheMtx == nil {
esCacheMtx = &sync.RWMutex{}
}
if memCacheMtx == nil {
memCacheMtx = &sync.RWMutex{}
}
if parseDateCacheMtx == nil {
parseDateCacheMtx = &sync.RWMutex{}
}
if gTokenEnvMtx == nil {
gTokenEnvMtx = &sync.Mutex{}
}
if gTokenMtx == nil {
gTokenMtx = &sync.Mutex{}
}
MT = true
}
// ResetThreadsNum - allows clearing current setting so the new one can be applied
func ResetThreadsNum(ctx *Ctx) {
thrNMtx.Lock()
defer thrNMtx.Unlock()
thrN = 0
MT = false
uuidsNonEmptyCacheMtx = nil
uuidsAffsCacheMtx = nil
identityCacheMtx = nil
rollsCacheMtx = nil
i2uCacheMtx = nil
emailsCacheMtx = nil
esCacheMtx = nil
memCacheMtx = nil
parseDateCacheMtx = nil
gTokenEnvMtx = nil
gTokenMtx = nil
}
// GetThreadsNum returns the number of available CPUs
// If environment variable DA_DS_ST is set it retuns 1
// It can be used to debug single threaded verion
func GetThreadsNum(ctx *Ctx) int {
thrNMtx.Lock()
defer thrNMtx.Unlock()
if thrN > 0 {
return thrN
}
defer func() {
if ctx.Debug > 0 {
Printf("using %d threads\n", thrN)
}
}()
if ctx.ST {
thrN = 1
return thrN
}
// Use environment variable to have singlethreaded version
if ctx.NCPUs > 0 {
n := int(float64(runtime.NumCPU()) * ctx.NCPUsScale)
if ctx.NCPUs > n {
ctx.NCPUs = n
}
runtime.GOMAXPROCS(ctx.NCPUs)
thrN = ctx.NCPUs
if thrN > 1 {
SetMT()
}
return thrN
}
thrN = int(float64(runtime.NumCPU()) * ctx.NCPUsScale)
runtime.GOMAXPROCS(thrN)
if thrN > 1 {
SetMT()
}
return thrN
}