about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-12-16 02:16:23 -0800
committerEric Wong <normalperson@yhbt.net>2009-12-16 02:16:23 -0800
commit52ad3f270e758e5bfdfe965cbecbd20d5048062f (patch)
treee95f01d3f0666aef3c1c994568ee34f4c49b2766
parent2ccce76d94046d017a9b02a0b033e3fff9483e98 (diff)
downloadrainbows-52ad3f270e758e5bfdfe965cbecbd20d5048062f.tar.gz
This makes them easier to override in subclasses.
-rw-r--r--lib/rainbows/fiber/io.rb24
1 files changed, 15 insertions, 9 deletions
diff --git a/lib/rainbows/fiber/io.rb b/lib/rainbows/fiber/io.rb
index 5c51cb9..339ac0a 100644
--- a/lib/rainbows/fiber/io.rb
+++ b/lib/rainbows/fiber/io.rb
@@ -22,14 +22,24 @@ module Rainbows
         to_io.close
       end
 
+      def wait_readable
+        RD[self] = false
+        ::Fiber.yield
+        RD.delete(self)
+      end
+
+      def wait_writable
+        WR[self] = false
+        ::Fiber.yield
+        WR.delete(self)
+      end
+
       def write(buf)
         begin
           (w = to_io.write_nonblock(buf)) == buf.size and return
           buf = buf[w..-1]
         rescue Errno::EAGAIN
-          WR[self] = false
-          ::Fiber.yield
-          WR.delete(self)
+          wait_writable
           retry
         end while true
       end
@@ -41,10 +51,8 @@ module Rainbows
           to_io.read_nonblock(16384)
         rescue Errno::EAGAIN
           return if expire && expire < Time.now
-          RD[self] = false
           expire ||= Time.now + G.kato
-          ::Fiber.yield
-          RD.delete(self)
+          wait_readable
           retry
         end
       end
@@ -53,9 +61,7 @@ module Rainbows
         begin
           to_io.read_nonblock(length, buf)
         rescue Errno::EAGAIN
-          RD[self] = false
-          ::Fiber.yield
-          RD.delete(self)
+          wait_readable
           retry
         end
       end