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.



63
64
65
66
67
68
69
70
71
72
# File 'lib/async/wrapper.rb', line 63

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.



98
99
100
# File 'lib/async/wrapper.rb', line 98

def io
  @io
end

#monitorObject (readonly)

The monitor for this wrapper, if any.



104
105
106
# File 'lib/async/wrapper.rb', line 104

def monitor
  @monitor
end

#reactorObject

The reactor this wrapper is associated with, if any.



101
102
103
# File 'lib/async/wrapper.rb', line 101

def reactor
  @reactor
end

Instance Method Details

#closeObject

Close the io and monitor.



163
164
165
166
167
# File 'lib/async/wrapper.rb', line 163

def close
	cancel_monitor
	
	@io.close
end

#closed?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/async/wrapper.rb', line 169

def closed?
	@io.closed?
end

#dupObject



74
75
76
# File 'lib/async/wrapper.rb', line 74

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

#resume(*arguments) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/async/wrapper.rb', line 78

def resume(*arguments)
	# 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(*arguments)
	end
	
	if @writable and (readiness == :w or readiness == :rw)
		@writable.resume(*arguments)
	end
	
	if @any
		@any.resume(*arguments)
	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:



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/async/wrapper.rb', line 148

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:



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/async/wrapper.rb', line 117

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:



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/async/wrapper.rb', line 132

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