From f7dca1d7cd5ca9e84ae33f9a8f7217ae9ae085a1 Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 12 Apr 2015 00:24:12 +0000 Subject: wait.c: get_timeout * ext/io/wait/wait.c (get_timeout): extract function to get timeout value. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50245 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/io/wait/wait.c | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) (limited to 'ext/io/wait/wait.c') diff --git a/ext/io/wait/wait.c b/ext/io/wait/wait.c index fd32be79b1..ca37d8e84a 100644 --- a/ext/io/wait/wait.c +++ b/ext/io/wait/wait.c @@ -44,6 +44,20 @@ static VALUE io_wait_readable _((int argc, VALUE *argv, VALUE io)); static VALUE io_wait_writable _((int argc, VALUE *argv, VALUE io)); void Init_wait _((void)); +static struct timeval * +get_timeout(int argc, VALUE *argv, struct timeval *timerec) +{ + VALUE timeout = Qnil; + rb_check_arity(argc, 0, 1); + if (!argc || NIL_P(timeout = argv[0])) { + return NULL; + } + else { + *timerec = rb_time_interval(timeout); + return timerec; + } +} + /* * call-seq: * io.nread -> int @@ -109,21 +123,12 @@ io_wait_readable(int argc, VALUE *argv, VALUE io) rb_io_t *fptr; int i; ioctl_arg n; - VALUE timeout; struct timeval timerec; struct timeval *tv; GetOpenFile(io, fptr); rb_io_check_readable(fptr); - rb_scan_args(argc, argv, "01", &timeout); - if (NIL_P(timeout)) { - tv = NULL; - } - else { - timerec = rb_time_interval(timeout); - tv = &timerec; - } - + tv = get_timeout(argc, argv, &timerec); if (rb_io_read_pending(fptr)) return Qtrue; if (!FIONREAD_POSSIBLE_P(fptr->fd)) return Qfalse; i = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_IN, tv); @@ -148,21 +153,12 @@ 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; - } - + tv = get_timeout(argc, argv, &timerec); i = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_OUT, tv); if (i < 0) rb_sys_fail(0); -- cgit v1.2.3