lib/lightio/library/io.rb in lightio-0.3.0 vs lib/lightio/library/io.rb in lightio-0.3.1
- old
+ new
@@ -116,10 +116,14 @@
return
end
@io_watcher.wait_readable
end
+ def io_watcher
+ @io_watcher
+ end
+
class << self
def open(*args)
io = self.new(*args)
yield io
ensure
@@ -139,17 +143,21 @@
[IO._wrap(r), IO._wrap(w)]
end
def select(read_fds, write_fds=nil, _except_fds=nil, timeout=nil)
timer = timeout && Time.now
- # run once ioloop
- LightIO.sleep 0
+ read_fds ||= []
+ write_fds ||= []
loop do
- r_fds = (read_fds || []).select {|fd| fd.closed? ? raise(IOError, 'closed stream') : fd.instance_variable_get(:@io_watcher).readable?}
- w_fds = (write_fds || []).select {|fd| fd.closed? ? raise(IOError, 'closed stream') : fd.instance_variable_get(:@io_watcher).writable?}
+ # clear io watcher status
+ read_fds.each {|fd| fd.send(:io_watcher).clear_status}
+ write_fds.each {|fd| fd.send(:io_watcher).clear_status}
+ # run ioloop once
+ LightIO.sleep 0
+ r_fds = read_fds.select {|fd| fd.closed? ? raise(IOError, 'closed stream') : fd.send(:io_watcher).readable?}
+ w_fds = write_fds.select {|fd| fd.closed? ? raise(IOError, 'closed stream') : fd.send(:io_watcher).writable?}
e_fds = []
if r_fds.empty? && w_fds.empty?
- LightIO.sleep 0
if timeout && Time.now - timer > timeout
return nil
end
else
return [r_fds, w_fds, e_fds]