Class: Async::Task

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/async/task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ios, reactor, &block) ⇒ Task

Returns a new instance of Task.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/async/task.rb', line 31

def initialize(ios, reactor, &block)
	@ios = Hash[
		ios.collect{|io| [io.fileno, reactor.wrap(io, self)]}
	]
	
	@reactor = reactor
	
	@fiber = Fiber.new do
		set!
		
		begin
			yield(*@ios.values, self)
			# Async.logger.debug("Task #{self} completed normally.")
		rescue Interrupt
			# Async.logger.debug("Task #{self} interrupted: #{$!}")
		ensure
			close
		end
	end
end

Instance Attribute Details

#iosObject (readonly)

Returns the value of attribute ios.



67
68
69
# File 'lib/async/task.rb', line 67

def ios
  @ios
end

#reactorObject (readonly)

Returns the value of attribute reactor.



68
69
70
# File 'lib/async/task.rb', line 68

def reactor
  @reactor
end

Class Method Details

.currentObject



87
88
89
# File 'lib/async/task.rb', line 87

def self.current
	Thread.current[:async_task] or raise RuntimeError, "No async task available!"
end

Instance Method Details

#bind(io) ⇒ Object



79
80
81
# File 'lib/async/task.rb', line 79

def bind(io)
	@ios[io.fileno] ||= reactor.wrap(io, self)
end

#register(io, interests) ⇒ Object



83
84
85
# File 'lib/async/task.rb', line 83

def register(io, interests)
	@reactor.register(io, interests)
end

#runObject



54
55
56
57
58
# File 'lib/async/task.rb', line 54

def run
	@fiber.resume
		
	return @fiber
end

#stop!Object



60
61
62
63
64
65
# File 'lib/async/task.rb', line 60

def stop!
	if @fiber.alive?
		exception = Interrupt.new("Stop right now!")
		@fiber.resume(exception)
	end
end

#with(io) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/async/task.rb', line 70

def with(io)
	wrapper = @reactor.wrap(io, self)
	
	yield wrapper
ensure
	wrapper.close
	io.close
end