Class: Async::Wrapper

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

Overview

Represents an asynchronous IO within a reactor.

Defined Under Namespace

Classes: Cancelled, WaitError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, reactor = nil) ⇒ Wrapper

Returns a new instance of Wrapper.

Parameters:

  • io

    the native object to wrap.

  • reactor (Reactor) (defaults to: nil)

    the reactor that is managing this wrapper, or not specified, it's looked up by way of Task.current.



41
42
43
44
45
46
47
48
49
50
# File 'lib/async/wrapper.rb', line 41

def initialize(io, reactor = nil)
	@io = io
	
	@reactor = reactor
	@monitor = nil
	
	@readable = nil
	@writable = nil
	@any = nil
end

Instance Attribute Details

#ioObject (readonly)

The underlying native io.



76
77
78
# File 'lib/async/wrapper.rb', line 76

def io
  @io
end

#monitorObject (readonly)

The monitor for this wrapper, if any.



82
83
84
# File 'lib/async/wrapper.rb', line 82

def monitor
  @monitor
end

#reactorObject

The reactor this wrapper is associated with, if any.



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

def reactor
  @reactor
end

Instance Method Details

#closeObject

Close the io and monitor.



141
142
143
144
145
# File 'lib/async/wrapper.rb', line 141

def close
	cancel_monitor
	
	@io.close
end

#closed?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/async/wrapper.rb', line 147

def closed?
	@io.closed?
end

#dupObject



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

def dup
	self.class.new(@io.dup, @reactor)
end

#resume(*args) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/async/wrapper.rb', line 56

def resume(*args)
	# It's possible that the monitor was closed before calling resume.
	return unless @monitor
	
	readiness = @monitor.readiness
	
	if @readable and (readiness == :r or readiness == :rw)
		@readable.resume(*args)
	end
	
	if @writable and (readiness == :w or readiness == :rw)
		@writable.resume(*args)
	end
	
	if @any
		@any.resume(*args)
	end
end

#wait_any(timeout = nil) ⇒ Object

Wait fo the io to become either readable or writable.

Parameters:

  • duration (Float)

    timeout after the given duration if not nil.

Raises:



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/async/wrapper.rb', line 126

def wait_any(timeout = nil)
	raise WaitError if @any
	
	self.reactor = Task.current.reactor
	
	begin
		@any = Fiber.current
		wait_for(timeout)
	ensure
		@any = nil
		@monitor.interests = interests if @monitor
	end
end

#wait_readable(timeout = nil) ⇒ Object

Wait for the io to become readable.

Raises:



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/async/wrapper.rb', line 95

def wait_readable(timeout = nil)
	raise WaitError if @readable
	
	self.reactor = Task.current.reactor
	
	begin
		@readable = Fiber.current
		wait_for(timeout)
	ensure
		@readable = nil
		@monitor.interests = interests if @monitor
	end
end

#wait_writable(timeout = nil) ⇒ Object

Wait for the io to become writable.

Raises:



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/async/wrapper.rb', line 110

def wait_writable(timeout = nil)
	raise WaitError if @writable
	
	self.reactor = Task.current.reactor
	
	begin
		@writable = Fiber.current
		wait_for(timeout)
	ensure
		@writable = nil
		@monitor.interests = interests if @monitor
	end
end