summaryrefslogtreecommitdiff
path: root/ext/io/wait/wait.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-04-12 00:24:12 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-04-12 00:24:12 +0000
commitf7dca1d7cd5ca9e84ae33f9a8f7217ae9ae085a1 (patch)
tree9977eee5f79195d6ba7605dc4f505e6acd4b9f11 /ext/io/wait/wait.c
parent2c292588ba7575193942e3fe4af71f8fe5d555b2 (diff)
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
Diffstat (limited to 'ext/io/wait/wait.c')
-rw-r--r--ext/io/wait/wait.c36
1 files changed, 16 insertions, 20 deletions
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);