Class: Steep::ThreadWaiter

Inherits:
Object show all
Defined in:
lib/steep/thread_waiter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(objects = nil) ⇒ ThreadWaiter

Returns a new instance of ThreadWaiter.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/steep/thread_waiter.rb', line 5

def initialize(objects = nil)
  @queue = Thread::Queue.new()
  @waiter_threads = Set[].compare_by_identity

  if objects
    objects.each do |object|
      thread = yield(object)
      self << thread
    end
  end
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



3
4
5
# File 'lib/steep/thread_waiter.rb', line 3

def queue
  @queue
end

#waiter_threadsObject (readonly)

Returns the value of attribute waiter_threads.



3
4
5
# File 'lib/steep/thread_waiter.rb', line 3

def waiter_threads
  @waiter_threads
end

Instance Method Details

#<<(thread) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/steep/thread_waiter.rb', line 17

def <<(thread)
  waiter_thread = Thread.new(thread) do |thread|
    # @type var thread: Thread

    Thread.current.report_on_exception = false

    begin
      thread.join
    ensure
      queue << thread
    end
  end

  waiter_threads << waiter_thread

  self
end

#wait_oneObject



35
36
37
38
39
40
41
# File 'lib/steep/thread_waiter.rb', line 35

def wait_one
  unless waiter_threads.empty?
    th = queue.pop() or raise
    waiter_threads.delete(th)
    th
  end
end