Class: Zold::ThreadPool
- Inherits:
-
Object
- Object
- Zold::ThreadPool
- Defined in:
- lib/zold/thread_pool.rb
Overview
Thread pool
Instance Method Summary collapse
-
#add ⇒ Object
Add a new thread.
-
#count ⇒ Object
How many threads are in there.
-
#empty? ⇒ Boolean
Is it empty and has no threads?.
-
#exists?(name) ⇒ Boolean
A thread with this name exists?.
-
#initialize(title, log: Log::NULL) ⇒ ThreadPool
constructor
A new instance of ThreadPool.
- #join(sec) ⇒ Object
-
#kill ⇒ Object
Kill them all immediately and close the pool.
-
#to_json ⇒ Object
As a hash map.
-
#to_s ⇒ Object
As a text.
Constructor Details
#initialize(title, log: Log::NULL) ⇒ ThreadPool
Returns a new instance of ThreadPool.
34 35 36 37 38 39 |
# File 'lib/zold/thread_pool.rb', line 34 def initialize(title, log: Log::NULL) @title = title @log = log @threads = [] @start = Time.now end |
Instance Method Details
#add ⇒ Object
Add a new thread
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/zold/thread_pool.rb', line 42 def add raise 'Block must be given to start()' unless block_given? latch = Concurrent::CountDownLatch.new(1) thread = Thread.start do Thread.current.name = @title VerboseThread.new(@log).run do latch.count_down yield end end latch.wait Thread.current.thread_variable_set( :kids, (Thread.current.thread_variable_get(:kids) || []) + [thread] ) @threads << thread end |
#count ⇒ Object
How many threads are in there
97 98 99 |
# File 'lib/zold/thread_pool.rb', line 97 def count @threads.count end |
#empty? ⇒ Boolean
Is it empty and has no threads?
92 93 94 |
# File 'lib/zold/thread_pool.rb', line 92 def empty? @threads.empty? end |
#exists?(name) ⇒ Boolean
A thread with this name exists?
102 103 104 |
# File 'lib/zold/thread_pool.rb', line 102 def exists?(name) !@threads.find { |t| t.name == name }.nil? end |
#join(sec) ⇒ Object
60 61 62 |
# File 'lib/zold/thread_pool.rb', line 60 def join(sec) @threads.each { |t| t.join(sec) } end |
#kill ⇒ Object
Kill them all immediately and close the pool
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 |
# File 'lib/zold/thread_pool.rb', line 65 def kill if @threads.empty? @log.debug("Thread pool \"#{@title}\" terminated with no threads") return end @log.debug("Stopping \"#{@title}\" thread pool with #{@threads.count} threads: \ #{@threads.map { |t| "#{t.name}/#{t.status}" }.join(', ')}...") start = Time.new begin join(0.1) ensure @threads.each do |t| (t.thread_variable_get(:kids) || []).each(&:kill) t.kill sleep(0.001) while t.alive? # I believe it's a bug in Ruby, this line fixes it Thread.current.thread_variable_set( :kids, (Thread.current.thread_variable_get(:kids) || []) - [t] ) end @log.debug("Thread pool \"#{@title}\" terminated all threads in #{Age.new(start)}, \ it was alive for #{Age.new(@start)}: #{@threads.map { |t| "#{t.name}/#{t.status}" }.join(', ')}") @threads.clear end end |
#to_json ⇒ Object
As a hash map
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/zold/thread_pool.rb', line 107 def to_json @threads.map do |t| { name: t.name, status: t.status, alive: t.alive?, vars: Hash[t.thread_variables.map { |v| [v.to_s, t.thread_variable_get(v)] }] } end end |
#to_s ⇒ Object
As a text
119 120 121 122 123 124 125 126 127 |
# File 'lib/zold/thread_pool.rb', line 119 def to_s @threads.map do |t| [ "#{t.name}: status=#{t.status}; alive=#{t.alive?}", 'Vars: ' + t.thread_variables.map { |v| "#{v}=\"#{t.thread_variable_get(v)}\"" }.join('; '), t.backtrace.nil? ? 'NO BACKTRACE' : " #{t.backtrace.join("\n ")}" ].join("\n") end end |