Class: Async::Wrapper

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

Overview

Represents an asynchronous IO within a reactor.

Direct Known Subclasses

IO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, task) ⇒ Wrapper

Returns a new instance of Wrapper.



24
25
26
27
28
# File 'lib/async/wrapper.rb', line 24

def initialize(io, task)
	@io = io
	@task = task
	@monitor = nil
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



30
31
32
# File 'lib/async/wrapper.rb', line 30

def io
  @io
end

#taskObject (readonly)

Returns the value of attribute task.



31
32
33
# File 'lib/async/wrapper.rb', line 31

def task
  @task
end

Instance Method Details

#closeObject



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

def close
	@monitor.close if @monitor
	@monitor = nil
end

#monitor(interests) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/async/wrapper.rb', line 33

def monitor(interests)
	unless @monitor
		@monitor = @task.register(@io, interests)
	else
		@monitor.interests = interests
	end
	
	@monitor.value = Fiber.current
	
	yield
	
ensure
	@monitor.value = nil if @monitor
end

#wait_any(interests = :rw) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/async/wrapper.rb', line 56

def wait_any(interests = :rw)
	monitor(interests) do
		# Async.logger.debug "Fiber #{Fiber.current} yielding..."
		result = Fiber.yield
		
		# Async.logger.debug "Fiber #{Fiber.current} resuming with result #{result}..."
		raise result if result.is_a? Exception
	end
end

#wait_readableObject



48
49
50
# File 'lib/async/wrapper.rb', line 48

def wait_readable
	wait_any(:r)
end

#wait_writableObject



52
53
54
# File 'lib/async/wrapper.rb', line 52

def wait_writable
	wait_any(:w)
end