diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-11-21 13:40:00 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-11-21 13:40:00 +0000 |
commit | e786aa711da8e64734d20acfd9c6b8df0b4515c5 (patch) | |
tree | de792c01c3a6f363bc09ec0c3ba4a9c7b709970b /ext | |
parent | 7d0cabb8f881798da1332becf60096a09fbb6c01 (diff) | |
download | ruby-e786aa711da8e64734d20acfd9c6b8df0b4515c5.tar.gz |
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]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37785 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r-- | ext/io/wait/wait.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/ext/io/wait/wait.c b/ext/io/wait/wait.c index 197d9b6173..531b7efef2 100644 --- a/ext/io/wait/wait.c +++ b/ext/io/wait/wait.c @@ -133,6 +133,43 @@ io_wait(int argc, VALUE *argv, VALUE io) } /* + * 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 @@ Init_wait() 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); } |