Class: Zold::ThreadPool

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/thread_pool.rb

Overview

Thread pool

Instance Method Summary collapse

Constructor Details

#initialize(title, log: Loog::NULL) ⇒ ThreadPool

Returns a new instance of ThreadPool.



17
18
19
20
21
22
# File 'lib/zold/thread_pool.rb', line 17

def initialize(title, log: Loog::NULL)
  @title = title
  @log = log
  @threads = []
  @start = Time.now
end

Instance Method Details

#addObject

Add a new thread



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/zold/thread_pool.rb', line 25

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

#countObject

How many threads are in there



80
81
82
# File 'lib/zold/thread_pool.rb', line 80

def count
  @threads.count
end

#empty?Boolean

Is it empty and has no threads?

Returns:

  • (Boolean)


75
76
77
# File 'lib/zold/thread_pool.rb', line 75

def empty?
  @threads.empty?
end

#exists?(name) ⇒ Boolean

A thread with this name exists?

Returns:

  • (Boolean)


85
86
87
# File 'lib/zold/thread_pool.rb', line 85

def exists?(name)
  !@threads.find { |t| t.name == name }.nil?
end

#join(sec) ⇒ Object



43
44
45
# File 'lib/zold/thread_pool.rb', line 43

def join(sec)
  @threads.each { |t| t.join(sec) }
end

#killObject

Kill them all immediately and close the pool



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
# File 'lib/zold/thread_pool.rb', line 48

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_jsonObject

As a hash map



90
91
92
93
94
95
96
97
98
99
# File 'lib/zold/thread_pool.rb', line 90

def to_json
  @threads.map do |t|
    {
      name: t.name,
      status: t.status,
      alive: t.alive?,
      vars: t.thread_variables.map { |v| [v.to_s, t.thread_variable_get(v)] }.to_h
    }
  end
end

#to_sObject

As a text



102
103
104
105
106
107
108
109
110
# File 'lib/zold/thread_pool.rb', line 102

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