[前][次][番号順一覧][スレッド一覧]

ruby-changes:25728

From: nobu <ko1@a...>
Date: Wed, 21 Nov 2012 22:40:14 +0900 (JST)
Subject: [ruby-changes:25728] nobu:r37785 (trunk): io/wait: add IO#wait_writable method

nobu	2012-11-21 22:40:00 +0900 (Wed, 21 Nov 2012)

  New Revision: 37785

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37785

  Log:
    io/wait: add IO#wait_writable method
    
    * ext/io/wait/wait.c (io_wait_writable): this is easier to use than
      IO.select for a single IO object and is immune to the
      limitations/innefficiency of select() on platforms where poll/ppoll
      is available.  patched by Eric Wong.  [Feature #4646]

  Modified files:
    trunk/ChangeLog
    trunk/ext/io/wait/wait.c
    trunk/test/io/wait/test_io_wait.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 37784)
+++ ChangeLog	(revision 37785)
@@ -1,3 +1,10 @@
+Wed Nov 21 22:39:28 2012  Nobuyoshi Nakada  <nobu@r...>
+
+	* ext/io/wait/wait.c (io_wait_writable): this is easier to use than
+	  IO.select for a single IO object and is immune to the
+	  limitations/innefficiency of select() on platforms where poll/ppoll
+	  is available.  patched by Eric Wong.  [Feature #4646]
+
 Wed Nov 21 22:27:52 2012  Narihiro Nakamura  <authornari@g...>
 
 	* gc.c (garbage_collect): remove a duplicative probe.
Index: ext/io/wait/wait.c
===================================================================
--- ext/io/wait/wait.c	(revision 37784)
+++ ext/io/wait/wait.c	(revision 37785)
@@ -133,6 +133,43 @@
 }
 
 /*
+ * call-seq:
+ *   io.wait_writable          -> IO
+ *   io.wait_writable(timeout) -> IO or nil
+ *
+ * Waits until IO writable is available or times out and returns self or
+ * nil when EOF is reached.
+ */
+static VALUE
+io_wait_writable(int argc, VALUE *argv, VALUE io)
+{
+    rb_io_t *fptr;
+    int i;
+    VALUE timeout;
+    struct timeval timerec;
+    struct timeval *tv;
+
+    GetOpenFile(io, fptr);
+    rb_io_check_writable(fptr);
+    rb_scan_args(argc, argv, "01", &timeout);
+    if (NIL_P(timeout)) {
+	tv = NULL;
+    }
+    else {
+	timerec = rb_time_interval(timeout);
+	tv = &timerec;
+    }
+
+    i = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_OUT, tv);
+    if (i < 0)
+	rb_sys_fail(0);
+    rb_io_check_closed(fptr);
+    if (i & RB_WAITFD_OUT)
+	return io;
+    return Qnil;
+}
+
+/*
  * IO wait methods
  */
 
@@ -142,4 +179,5 @@
     rb_define_method(rb_cIO, "nread", io_nread, 0);
     rb_define_method(rb_cIO, "ready?", io_ready_p, 0);
     rb_define_method(rb_cIO, "wait", io_wait, -1);
+    rb_define_method(rb_cIO, "wait_writable", io_wait_writable, -1);
 }
Index: test/io/wait/test_io_wait.rb
===================================================================
--- test/io/wait/test_io_wait.rb	(revision 37784)
+++ test/io/wait/test_io_wait.rb	(revision 37785)
@@ -1,3 +1,4 @@
+# -*- coding: us-ascii -*-
 require 'test/unit'
 require 'timeout'
 require 'socket'
@@ -69,4 +70,39 @@
     Thread.new { sleep 0.01; @w.close }
     assert_nil @r.wait
   end
+
+  def test_wait_writable
+    assert_equal @w, @w.wait_writable
+  end
+
+  def test_wait_writable_timeout
+    assert_equal @w, @w.wait_writable(0.001)
+    written = fill_pipe
+    assert_nil @w.wait_writable(0.001)
+    @r.read(written)
+    assert_equal @w, @w.wait_writable(0.001)
+  end
+
+  def test_wait_writable_EPIPE
+    fill_pipe
+    @r.close
+    assert_equal @w, @w.wait_writable
+  end
+
+  def test_wait_writable_closed
+    @w.close
+    assert_raises(IOError) { @w.wait_writable }
+  end
+
+private
+
+  def fill_pipe
+    written = 0
+    buf = " " * 4096
+    begin
+      written += @w.write_nonblock(buf)
+    rescue Errno::EAGAIN
+      return written
+    end while true
+  end
 end if IO.method_defined?(:wait)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]