Class: Async::IO::Threads

Inherits:
Object
  • Object
show all
Defined in:
lib/async/io/threads.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent: nil) ⇒ Threads

Returns a new instance of Threads.



28
29
30
# File 'lib/async/io/threads.rb', line 28

def initialize(parent: nil)
  @parent = parent
end

Instance Method Details

#async(parent: (@parent or Task.current)) ⇒ Object



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
# File 'lib/async/io/threads.rb', line 32

def async(parent: (@parent or Task.current))
  parent.async do |task|
    notification = Async::IO::Notification.new
    
    thread = Thread.new do
      yield
    ensure
      notification.signal
    end
    
    task.annotate "Waiting for thread to finish..."
    
    notification.wait
    
    thread.value
  ensure
    if thread&.alive?
      thread.raise(Stop)
      
      begin
        thread.join
      rescue Stop
        # Ignore.
      end
    end
    
    notification&.close
  end
end