Class: Asynchronous::Thread

Inherits:
Object
  • Object
show all
Defined in:
lib/asynchronous/thread.rb

Instance Method Summary collapse

Constructor Details

#initialize(&callable) ⇒ Thread

Returns a new instance of Thread.



2
3
4
5
6
7
8
9
# File 'lib/asynchronous/thread.rb', line 2

def initialize(&callable)
  @value = nil
  @value_received = false
  @read, @write = ::IO.pipe
  read_buffer

  @pid = fork(&callable)
end

Instance Method Details

#joinObject



11
12
13
14
15
16
# File 'lib/asynchronous/thread.rb', line 11

def join
  wait
  receive_value! unless @value_received
  raise(@value.wrapped_error) if @value.is_a?(::Asynchronous::Error)
  self
end

#killObject



18
19
20
21
22
23
24
# File 'lib/asynchronous/thread.rb', line 18

def kill
  ::Process.kill('KILL', @pid)

  wait
rescue Errno::ESRCH
  nil
end

#statusObject



26
27
28
# File 'lib/asynchronous/thread.rb', line 26

def status
  alive? ? 'run' : false
end

#valueObject



30
31
32
# File 'lib/asynchronous/thread.rb', line 30

def value
  join; @value
end