blob: 61070f3af547752719bb29b88ab4356bbadb96a7 (
plain)
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
|
module Killjoy
class ThreadPool
include Enumerable
def initialize(max = Facter.value('processors')['count'].to_i)
@threads = []
@jobs = Queue.new
Thread.abort_on_exception = true
max.times do |n|
@threads << Thread.new do
loop do
job = @jobs.deq
Killjoy.logger.debug("[#{Thread.current.object_id}] invoking job")
job.call
Killjoy.logger.debug("[#{Thread.current.object_id}] finish job")
end
end
end
end
def run(&block)
@jobs << block
Killjoy.logger.debug("[#{Thread.current.object_id}] queue up a job. count: #{@jobs.size}")
end
def stop
@jobs.clear
if block_given?
each do |thread|
yield thread
Thread.kill(thread)
end
end
end
def each(&block)
@threads.each(&block)
end
end
end
|