From 55393645a12435305bb88907a628bedab69606d4 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sun, 28 May 2023 17:01:40 +0900 Subject: [ruby/io-nonblock] Revert "Remove usage of IO internals." This reverts commit https://github.com/ruby/io-nonblock/commit/9772b6dcb63c. https://github.com/ruby/io-nonblock/commit/08cabf78e1 --- ext/io/nonblock/extconf.rb | 2 -- ext/io/nonblock/nonblock.c | 54 ++++++++++++++++++++-------------------------- 2 files changed, 23 insertions(+), 33 deletions(-) (limited to 'ext') diff --git a/ext/io/nonblock/extconf.rb b/ext/io/nonblock/extconf.rb index d911fdca75..d813a01e7c 100644 --- a/ext/io/nonblock/extconf.rb +++ b/ext/io/nonblock/extconf.rb @@ -7,5 +7,3 @@ if have_macro("O_NONBLOCK", hdr) and (have_macro("F_GETFL", hdr) or have_macro("F_SETFL", hdr)) create_makefile(target) end - -have_function("rb_io_descriptor") diff --git a/ext/io/nonblock/nonblock.c b/ext/io/nonblock/nonblock.c index 3fc35e1a64..b8a40ff38e 100644 --- a/ext/io/nonblock/nonblock.c +++ b/ext/io/nonblock/nonblock.c @@ -17,16 +17,6 @@ #endif #include -#ifndef HAVE_RB_IO_DESCRIPTOR -static int -rb_io_descriptor(VALUE io) -{ - rb_io_t *fptr; - GetOpenFile(io, fptr); - return fptr->fd; -} -#endif - #ifdef F_GETFL static int get_fcntl_flags(int fd) @@ -49,8 +39,10 @@ get_fcntl_flags(int fd) static VALUE rb_io_nonblock_p(VALUE io) { - if (get_fcntl_flags(rb_io_descriptor(io)) & O_NONBLOCK) - return Qtrue; + rb_io_t *fptr; + GetOpenFile(io, fptr); + if (get_fcntl_flags(fptr->fd) & O_NONBLOCK) + return Qtrue; return Qfalse; } #else @@ -130,13 +122,15 @@ io_nonblock_set(int fd, int f, int nb) * */ static VALUE -rb_io_nonblock_set(VALUE self, VALUE value) +rb_io_nonblock_set(VALUE io, VALUE nb) { - int descriptor = rb_io_descriptor(self); - - io_nonblock_set(rb_io_descriptor(descriptor), get_fcntl_flags(descriptor), RTEST(value)); - - return self; + rb_io_t *fptr; + GetOpenFile(io, fptr); + if (RTEST(nb)) + rb_io_set_nonblock(fptr); + else + io_nonblock_set(fptr->fd, get_fcntl_flags(fptr->fd), RTEST(nb)); + return io; } static VALUE @@ -158,25 +152,23 @@ io_nonblock_restore(VALUE arg) * The original mode is restored after the block is executed. */ static VALUE -rb_io_nonblock_block(int argc, VALUE *argv, VALUE self) +rb_io_nonblock_block(int argc, VALUE *argv, VALUE io) { int nb = 1; rb_io_t *fptr; + int f, restore[2]; - int descriptor = rb_io_descriptor(self); - + GetOpenFile(io, fptr); if (argc > 0) { - VALUE v; - rb_scan_args(argc, argv, "01", &v); - nb = RTEST(v); + VALUE v; + rb_scan_args(argc, argv, "01", &v); + nb = RTEST(v); } - - int current_flags = get_fcntl_flags(descriptor); - int restore[2] = {descriptor, current_flags}; - - if (!io_nonblock_set(descriptor, current_flags, nb)) - return rb_yield(io); - + f = get_fcntl_flags(fptr->fd); + restore[0] = fptr->fd; + restore[1] = f; + if (!io_nonblock_set(fptr->fd, f, nb)) + return rb_yield(io); return rb_ensure(rb_yield, io, io_nonblock_restore, (VALUE)restore); } #else -- cgit v1.2.3