Class: IO

Inherits:
Object
  • Object
show all
Defined in:
wait/lib/nonblock.rb

Instance Method Summary collapse

Instance Method Details

#nonblock(nb = true) ⇒ Object



17
18
19
20
21
22
# File 'wait/lib/nonblock.rb', line 17

def nonblock(nb = true)
  nb, self.nonblock = nonblock?, nb
  yield
ensure
  self.nonblock = nb
end

#nonblock=(nb) ⇒ Object



7
8
9
10
11
12
13
14
15
# File '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

#nonblock?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'wait/lib/nonblock.rb', line 3

def nonblock?
  (fcntl(Fcntl::F_GETFL) & File::NONBLOCK) != 0
end

#ready?Object

begin

--- IO#ready?

returns non-nil if input available without blocking, or nil.

end



# File 'wait/wait.c'

/*
=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;
}

#waitObject

begin

--- IO#wait()

waits until input available or timed out and returns self, or nil
when EOF reached.

end



# File 'wait/wait.c'

/*
=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;
}