-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskManager.rb
372 lines (323 loc) · 6.51 KB
/
TaskManager.rb
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# Copyright (C) 2021, 2022 hidenorly
#
# 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.
# TaskManager and Task definitions
class Task
attr_accessor :description
def initialize(description)
@description = description
end
def execute
end
end
class TaskManager
def initialize()
@tasks = []
end
def addTask(aTask)
@tasks.push( aTask )
end
def executeAll
while [email protected]? do
aTask = @tasks.pop()
aTask.execute()
end
end
end
# --- TaskAsync and TaskManagerAsync -------------
class TaskAsync < Task
attr_accessor :description
attr_accessor :taskCompleCallback
attr_accessor :running
def initialize(description)
super( description )
@running = false
@taskCompleCallback = nil
end
def execute
_doneTask()
end
def finalize
@running = false
end
def _doneTask
if nil != @taskCompleCallback then
@taskCompleCallback.call(self) # completion callback
end
end
end
class TaskManagerAsync < TaskManager
def initialize( numOfThread = TaskManagerAsync.getNumberOfProcessor() )
@tasks = []
@numOfThread = numOfThread
@currentRunningTasks = 0
@threads = []
@criticalSection = Mutex.new
end
def addTask(aTaskAsync)
aTaskAsync.taskCompleCallback = method(:_onTaskCompletion)
@criticalSection.synchronize {
super( aTaskAsync )
}
end
def cancelTask( aTask )
# @tasks.delete( aTask )
@criticalSection.synchronize {
@currentRunningTasks = @currentRunningTasks - 1
}
aTask.running = false
aTask.finalize()
end
def executeAll
candidateTasks = []
@criticalSection.synchronize {
for aTask in @tasks do
if( false == aTask.running ) then
if ( @currentRunningTasks < @numOfThread ) then
@currentRunningTasks = @currentRunningTasks + 1
aTask.running = true
candidateTasks.push( aTask )
end
end
end
for aTask in candidateTasks do
@tasks.delete( aTask )
end
}
for aTask in candidateTasks do
@threads << Thread.new(aTask) do |task|
task.execute()
end
end
end
def isRunning
@criticalSection.synchronize {
if @currentRunningTasks > 0 then return true end
}
return false
end
def isRemainingTasks
@criticalSection.synchronize {
if @tasks.count > 0 then return true end
}
return false
end
def _onTaskCompletion( task )
# puts "done : #{task.description}"
cancelTask( task )
executeAll()
end
def finalize
while isRemainingTasks() || isRunning() do
if ( isRunning() ) then
sleep 0.1
# puts "waiting ... done #{@currentRunningTasks}"
@threads.each { |t| t.join }
end
end
end
def self.getNumberOfProcessor
# try as Windows
numOfProcessor = ENV['NUMBER_OF_PROCESSORS']
if !numOfProcessor then
# try as Linux
exec_cmd = "cat /proc/cpuinfo 2> /dev/null | grep bogomips | wc -l"
IO.popen(exec_cmd, "r") {|io|
while !io.eof? do
numOfProcessor= io.readline.strip.to_i
end
io.close()
}
end
if !numOfProcessor || 0 == numOfProcessor then
# try as MacOS
exec_cmd = "sysctl -n hw.ncpu 2> /dev/null"
IO.popen(exec_cmd, "r") {|io|
while !io.eof? do
numOfProcessor= io.readline.strip.to_i
end
io.close()
}
end
numOfProcessor=4 if !numOfProcessor || 0 == numOfProcessor
return numOfProcessor
end
end
class TaskPool
def initialize
@criticalSection = Mutex.new
@tasks = []
end
def enqueue(task)
@criticalSection.synchronize {
@tasks << task
}
end
def dequeue
result = nil
@criticalSection.synchronize {
result = @tasks.shift
}
return result
end
def erase(task)
@criticalSection.synchronize {
@tasks.delete(task)
}
end
def clear
@criticalSection.synchronize {
@tasks = []
}
end
def isEmpty
return @tasks.empty?
end
end
class TaskExecutor
def initialize(taskPool)
@taskPool = taskPool
@thread = nil
@criticalSection = Mutex.new
@isRunnable = false
end
def execute
@isRunnable = true
if @thread == nil then
@criticalSection.synchronize {
@thread = Thread.new do
while(@isRunnable) do
@task = @taskPool.dequeue()
if @task!=nil then
@task.execute()
else
sleep 0.1
end
end
@thread = nil
end
}
@task = nil
end
end
def terminate
@isRunnable = false
@criticalSection.synchronize {
if @thread != nil then
@thread.join
@thread = nil
end
}
end
def cancelTaskIfRunning(task)
if @task==task then
isRunnable = @isRunnable
terminate()
execute() if isRunnable
end
end
def isRunning
return @thread != nil
end
end
class ThreadPool
def initialize(numOfThreads=TaskManagerAsync.getNumberOfProcessor())
@taskPool = TaskPool.new()
@threads = []
for i in 0..numOfThreads do
@threads << TaskExecutor.new(@taskPool)
end
end
def addTask(task)
@taskPool.enqueue(task)
end
def cancelTask(task)
@taskPool.erase(task)
end
def executeAll
@threads.each do |aTaskExecutor|
aTaskExecutor.execute()
end
end
def isRemainingTasks
return [email protected]()
end
def isRunning
result = false
@threads.each do |aTaskExecutor|
result = result | aTaskExecutor.isRunning()
end
return result
end
def finalize
while isRemainingTasks() do
if ( isRunning() ) then
sleep 0.1
end
end
terminate()
end
def terminate
@taskPool.clear()
@threads.each do |aTaskExecutor|
aTaskExecutor.terminate()
end
end
end
class ResultCollector
def initialize( )
@result = []
@_mutex = Mutex.new
end
def onResult( id, result )
@_mutex.synchronize {
if result.kind_of?(Array) then
@result = @result | result
else
@result << result
end
}
end
def report()
@_mutex.synchronize {
@result.each do | aResult |
puts "#{aResult}"
end
}
end
def getResult()
result = nil
@_mutex.synchronize {
result = @result.clone()
}
return result
end
end
class ResultCollectorHash < ResultCollector
def initialize( )
super()
@result = {}
end
def onResult( id, result )
@_mutex.synchronize {
@result[ id ] = result
}
end
def report()
@_mutex.synchronize {
@result.each do | id, aResult |
puts "#{id} : #{aResult}"
end
}
end
end