-
Notifications
You must be signed in to change notification settings - Fork 200
/
threadpool.go
164 lines (138 loc) · 4.58 KB
/
threadpool.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
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
155
156
157
158
159
160
161
162
163
164
/*
_____ _____ _____ ____ ______ _____ ------
| | | | | | | | | | | | |
| | | | | | | | | | | | |
| --- | | | | |-----| |---- | | |-----| |----- ------
| | | | | | | | | | | | |
| ____| |_____ | ____| | ____| | |_____| _____| |_____ |_____
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
Copyright © 2020-2024 Microsoft Corporation. All rights reserved.
Author : <blobfusedev@microsoft.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
*/
package block_cache
import (
"sync"
"github.com/Azure/azure-storage-fuse/v2/internal/handlemap"
)
// ThreadPool is a group of workers that can be used to execute a task
type ThreadPool struct {
// Number of workers running in this group
worker uint32
// Channel to close all the workers
close chan int
// Wait group to wait for all workers to finish
wg sync.WaitGroup
// Channel to hold pending requests
priorityCh chan *workItem
normalCh chan *workItem
// Reader method that will actually read the data
reader func(*workItem)
// Writer method that will actually write the data
writer func(*workItem)
}
// One workitem to be scheduled
type workItem struct {
handle *handlemap.Handle // Handle to which this item belongs
block *Block // Block to hold data for this item
prefetch bool // Flag marking this is a prefetch request or not
failCnt int32 // How many times this item has failed to download
upload bool // Flag marking this is a upload request or not
blockId string // BlockId of the block
}
// newThreadPool creates a new thread pool
func newThreadPool(count uint32, reader func(*workItem), writer func(*workItem)) *ThreadPool {
if count == 0 || reader == nil {
return nil
}
return &ThreadPool{
worker: count,
reader: reader,
writer: writer,
close: make(chan int, count),
priorityCh: make(chan *workItem, count*2),
normalCh: make(chan *workItem, count*5000),
}
}
// Start all the workers and wait till they start receiving requests
func (t *ThreadPool) Start() {
// 10% threads will listne only on high priority channel
highPriority := (t.worker * 10) / 100
for i := uint32(0); i < t.worker; i++ {
t.wg.Add(1)
go t.Do(i < highPriority)
}
}
// Stop all the workers threads
func (t *ThreadPool) Stop() {
for i := uint32(0); i < t.worker; i++ {
t.close <- 1
}
t.wg.Wait()
close(t.close)
close(t.priorityCh)
close(t.normalCh)
}
// Schedule the download of a block
func (t *ThreadPool) Schedule(urgent bool, item *workItem) {
// urgent specifies the priority of this task.
// true means high priority and false means low priority
if urgent {
t.priorityCh <- item
} else {
t.normalCh <- item
}
}
// Do is the core task to be executed by each worker thread
func (t *ThreadPool) Do(priority bool) {
defer t.wg.Done()
if priority {
// This thread will work only on high priority channel
for {
select {
case item := <-t.priorityCh:
if item.upload {
t.writer(item)
} else {
t.reader(item)
}
case <-t.close:
return
}
}
} else {
// This thread will work only on both high and low priority channel
for {
select {
case item := <-t.priorityCh:
if item.upload {
t.writer(item)
} else {
t.reader(item)
}
case item := <-t.normalCh:
if item.upload {
t.writer(item)
} else {
t.reader(item)
}
case <-t.close:
return
}
}
}
}