forked from vearch/vearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutine_pool.go
More file actions
154 lines (132 loc) · 2.52 KB
/
routine_pool.go
File metadata and controls
154 lines (132 loc) · 2.52 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2019 The Vearch Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package routine
import (
"runtime"
"sync"
"sync/atomic"
"time"
)
const (
idle int32 = 0
use int32 = 1
dead int32 = 2
)
var (
capacity int
mask int64
count int64
poolMap []*pool
)
func init() {
n := runtime.NumCPU()
if (n & (n - 1)) == 0 {
capacity = n
} else {
switch {
case n > 32:
capacity = 32
case n > 16:
capacity = 16
case n > 8:
capacity = 8
default:
capacity = 4
}
}
mask = int64(capacity - 1)
poolMap = make([]*pool, capacity)
for i := 0; i < capacity; i++ {
poolMap[i] = newPool()
}
}
// GoWork execute the f function in a routine,
// The routine is reused.
func GoWork(f func()) {
idx := atomic.AddInt64(&count, 1)
poolMap[idx&mask].goWork(f)
}
type pool struct {
sync.Mutex
head routine
tail *routine
idleDuration time.Duration
}
func newPool() *pool {
p := &pool{
idleDuration: 10 * time.Minute,
}
p.tail = &p.head
return p
}
func (p *pool) goWork(f func()) {
for {
r := p.get()
if atomic.CompareAndSwapInt32(&r.status, idle, use) {
r.queue <- f
return
}
}
}
func (p *pool) get() *routine {
p.Lock()
if p.head.next == nil {
p.Unlock()
return p.createRoutine()
}
head := &p.head
ret := head.next
head.next = ret.next
if ret == p.tail {
p.tail = head
}
p.Unlock()
ret.next = nil
return ret
}
func (p *pool) createRoutine() *routine {
r := &routine{
queue: make(chan func()),
}
go r.routineLoop(p)
return r
}
type routine struct {
queue chan func()
next *routine
status int32
}
func (r *routine) put(pool *pool) {
atomic.StoreInt32(&r.status, idle)
pool.Lock()
pool.tail.next = r
pool.tail = r
pool.Unlock()
atomic.AddInt64(&count, -1)
}
func (r *routine) routineLoop(pool *pool) {
timer := time.NewTimer(pool.idleDuration)
for {
select {
case work := <-r.queue:
work()
r.put(pool)
case <-timer.C:
if ok := atomic.CompareAndSwapInt32(&r.status, idle, dead); ok {
return
}
}
timer.Reset(pool.idleDuration)
}
}