-
Notifications
You must be signed in to change notification settings - Fork 13
/
bmk_pm_legacy.bmx
244 lines (173 loc) · 3.97 KB
/
bmk_pm_legacy.bmx
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
SuperStrict
Import "bmk_ng_utils.bmx"
Type TProcessManager
Field pool:TThreadPool
Field cpuCount:Int
Field threads:TList = New TList
Method New()
cpuCount = GetCoreCount()
pool = TThreadPool.Create(Max(1, cpuCount - 1), cpuCount * 6)
End Method
Method CheckTasks()
While pool.count() = pool.Size()
Delay 5
Wend
End Method
Method WaitForTasks()
While pool.Count() Or pool.Running()
Delay 5
Wend
End Method
Method DoSystem(cmd:String, src:String, obj:String, supp:String)
CheckTasks()
pool.AddTask(TProcessTask._DoTasks, CreateProcessTask(cmd, src, obj, supp))
End Method
Method AddTask:Int(func:Object(data:Object), data:Object)
CheckTasks()
pool.AddTask(func, data)
End Method
End Type
Rem
bbdoc: A thread pool.
End Rem
Type TThreadPool
Field _threads:TThread[]
Field _queue:TThreadPoolTask[]
Field _lock:TMutex
Field _waitVar:TCondVar
Field _count:Int
Field _head:Int
Field _tail:Int
Field _running:Int
Field _shutdown:Int
Rem
bbdoc: Creates a new thread pool of @threadCount threads and a queue size of @queueSize.
End Rem
Function Create:TThreadPool(threadCount:Int, queueSize:Int)
Local pool:TThreadPool = New TThreadPool
pool._threads = New TThread[threadCount]
pool._queue = New TThreadPoolTask[queueSize]
pool._lock = TMutex.Create()
pool._waitVar = TCondVar.Create()
For Local i:Int = 0 Until threadCount
pool._threads[i] = TThread.Create(_ThreadPoolThread, pool)
Next
' cache tasks
For Local i:Int = 0 Until queueSize
pool._queue[i] = New TThreadPoolTask
Next
Return pool
End Function
Rem
bbdoc: Returns the number of tasks in the queue.
End Rem
Method Count:Int()
Return _count
End Method
Rem
bbdoc: Returns the size of the queue.
End Rem
Method Size:Int()
Return _queue.length
End Method
Rem
bbdoc: Returns the number of busy/running threads.
End Rem
Method Running:Int()
Return _running
End Method
Rem
bbdoc: Adds a task to the queue.
End Rem
Method AddTask:Int(func:Object(data:Object), data:Object)
Local result:Int = True
_lock.Lock()
Local slot:Int = _tail + 1
If slot = _queue.length Then
slot = 0
End If
While True
If _count = _queue.length Then
result = False
Exit
End If
If _shutdown Then
result = False
Exit
End If
_queue[_tail].func = func
_queue[_tail].data = data
_tail = slot
_count :+ 1
_waitVar.Broadcast()
Exit
Wend
_lock.Unlock()
Return result
End Method
Rem
bbdoc: Shutdown the pool.
about: If @immediately is False, the queue will be processed to the end.
End Rem
Method Shutdown(immediately:Int = False)
_lock.Lock()
While True
If _shutdown Then
Return
End If
If immediately Then
_shutdown = 2
Else
_shutdown = 1
End If
_waitVar.Broadcast()
_lock.Unlock()
For Local i:Int = 0 Until _threads.length
_threads[i].Wait()
Next
Exit
Wend
_lock.Lock()
_lock.Close()
_waitVar.Close()
End Method
Function _ThreadPoolThread:Object(data:Object)
Local pool:TThreadPool = TThreadPool(data)
While True
pool._lock.Lock()
While pool._count = 0 And Not pool._shutdown
pool._waitVar.Wait(pool._lock)
Delay 5
Wend
If pool._shutdown And pool._count = 0 Then
' time to finish
Exit
End If
Local task:TThreadPoolTask = pool._queue[pool._head]
Local func:Object(data:Object) = task.func
Local data:Object = task.data
pool._head :+ 1
If pool._head = pool._queue.length Then
pool._head = 0
End If
' less queue
pool._count :- 1
' more running threads
pool._running :+ 1
pool._lock.Unlock()
' perform a task
func(data)
pool._lock.Lock()
pool._running :- 1
pool._lock.Unlock()
Wend
pool._lock.Unlock()
End Function
Method Delete()
Shutdown()
End Method
End Type
Type TThreadPoolTask
Field func:Object(data:Object)
Field data:Object
End Type