Class | IO |
In: |
io/wait/lib/nonblock.rb
|
# File io/wait/lib/nonblock.rb, line 17 def nonblock(nb = true) nb, self.nonblock = nonblock?, nb yield ensure self.nonblock = nb end
# File io/wait/lib/nonblock.rb, line 7 def nonblock=(nb) f = fcntl(Fcntl::F_GETFL) if nb f |= File::NONBLOCK else f &= ~File::NONBLOCK end fcntl(Fcntl::F_SETFL, f) end
# File io/wait/lib/nonblock.rb, line 3 def nonblock? (fcntl(Fcntl::F_GETFL) & File::NONBLOCK) != 0 end
— IO#ready?
returns non-nil if input available without blocking, or nil.
/* =begin --- IO#ready? returns non-nil if input available without blocking, or nil. =end */ static VALUE io_ready_p(io) VALUE io; { OpenFile *fptr; FILE *fp; ioctl_arg n; GetOpenFile(io, fptr); rb_io_check_readable(fptr); if (!FIONREAD_POSSIBLE_P(fileno(fptr->f))) return Qfalse; fp = fptr->f; if (feof(fp)) return Qfalse; if (rb_read_pending(fp)) return Qtrue; if (ioctl(fileno(fp), FIONREAD, &n)) rb_sys_fail(0); if (n > 0) return ioctl_arg2num(n); return Qnil; }
— IO#wait([timeout])
waits until input available or timed out and returns self, or nil when EOF reached.
/* =begin --- IO#wait([timeout]) waits until input available or timed out and returns self, or nil when EOF reached. =end */ static VALUE io_wait(argc, argv, io) int argc; VALUE *argv; VALUE io; { OpenFile *fptr; fd_set rd; FILE *fp; int fd; ioctl_arg n; VALUE timeout; struct timeval *tp, timerec; GetOpenFile(io, fptr); rb_io_check_readable(fptr); rb_scan_args(argc, argv, "01", &timeout); if (NIL_P(timeout)) { tp = 0; } else { timerec = rb_time_interval(timeout); tp = &timerec; } fp = fptr->f; if (feof(fp)) return Qfalse; if (rb_read_pending(fp)) return Qtrue; fd = fileno(fp); FD_ZERO(&rd); FD_SET(fd, &rd); if (rb_thread_select(fd + 1, &rd, NULL, NULL, tp) < 0) rb_sys_fail(0); rb_io_check_closed(fptr); if (!FIONREAD_POSSIBLE_P(fileno(fptr->f))) return Qfalse; if (ioctl(fileno(fp), FIONREAD, &n)) rb_sys_fail(0); if (n > 0) return io; return Qnil; }