From c749e4fca225cd00f5be15f6fe6e4538a6c29413 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 14 Jul 2009 12:53:23 +0000 Subject: [PATCH] * ext/io/nonblock: moved from ext/io/wait/lib. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24106 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 4 ++ ext/io/nonblock/extconf.rb | 8 +++ ext/io/nonblock/nonblock.c | 108 ++++++++++++++++++++++++++++++++++++ ext/io/wait/lib/nonblock.rb | 23 -------- 4 files changed, 120 insertions(+), 23 deletions(-) create mode 100644 ext/io/nonblock/extconf.rb create mode 100644 ext/io/nonblock/nonblock.c delete mode 100644 ext/io/wait/lib/nonblock.rb diff --git a/ChangeLog b/ChangeLog index 750bd06c03..cdd34abb93 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Tue Jul 14 21:53:18 2009 Nobuyoshi Nakada + + * ext/io/nonblock: moved from ext/io/wait/lib. + Tue Jul 14 17:29:20 2009 NARUSE, Yui * string.c (rb_str_index_m): return nil if pos is out of string. diff --git a/ext/io/nonblock/extconf.rb b/ext/io/nonblock/extconf.rb new file mode 100644 index 0000000000..aecdc16cea --- /dev/null +++ b/ext/io/nonblock/extconf.rb @@ -0,0 +1,8 @@ +require 'mkmf' +target = "io/nonblock" + +hdr = %w"fcntl.h" +if have_macro("O_NONBLOCK", hdr) and + (have_macro("F_GETFL", hdr) or have_macro("F_SETFL", hdr)) + create_makefile(target) +end diff --git a/ext/io/nonblock/nonblock.c b/ext/io/nonblock/nonblock.c new file mode 100644 index 0000000000..d7e1ac8e01 --- /dev/null +++ b/ext/io/nonblock/nonblock.c @@ -0,0 +1,108 @@ +/********************************************************************** + + io/wait.c - + + $Author$ + created at: Tue Jul 14 21:53:18 2009 + + All the files in this distribution are covered under the Ruby's + license (see the file COPYING). + +**********************************************************************/ + +#include "ruby.h" +#include "ruby/io.h" +#ifdef HAVE_UNISTD_H +#include +#endif +#include + +#ifdef F_GETFL +static int +io_nonblock_mode(int fd) +{ + int f = fcntl(fd, F_GETFL); + if (f == -1) rb_sys_fail(0); + return f; +} +#else +#define io_nonblock_mode(fd) ((void)(fd), 0) +#endif + +#ifdef F_GETFL +static VALUE +rb_io_nonblock_p(VALUE io) +{ + rb_io_t *fptr; + GetOpenFile(io, fptr); + if (io_nonblock_mode(fptr->fd) & O_NONBLOCK) + return Qtrue; + return Qfalse; +} +#else +#define rb_io_nonblock_p rb_f_notimplement +#endif + +#ifdef F_SETFL +static void +io_nonblock_set(int fd, int f, int nb) +{ + if (nb) + f |= O_NONBLOCK; + else + f &= ~O_NONBLOCK; + if (fcntl(fd, F_SETFL, f) == -1) + rb_sys_fail(0); +} + +static VALUE +rb_io_nonblock_set(VALUE io, VALUE nb) +{ + rb_io_t *fptr; + GetOpenFile(io, fptr); + io_nonblock_set(fptr->fd, io_nonblock_mode(fptr->fd), RTEST(nb)); + return io; +} + +static VALUE +io_nonblock_restore(VALUE arg) +{ + int *restore = (int *)arg; + if (fcntl(restore[0], F_SETFL, restore[1]) == -1) + rb_sys_fail(0); + return Qnil; +} + +static VALUE +rb_io_nonblock_block(int argc, VALUE *argv, VALUE io) +{ + int nb = 1; + rb_io_t *fptr; + int f, restore[2]; + + GetOpenFile(io, fptr); + if (argc > 0) { + VALUE v; + rb_scan_args(argc, argv, "01", &v); + nb = RTEST(v); + } + f = io_nonblock_mode(fptr->fd); + restore[0] = fptr->fd; + restore[1] = f; + io_nonblock_set(fptr->fd, f, nb); + return rb_ensure(rb_yield, io, io_nonblock_restore, (VALUE)restore); +} +#else +#define rb_io_nonblock_set rb_f_notimplement +#define rb_io_nonblock_block rb_f_notimplement +#endif + +void +Init_nonblock(void) +{ + VALUE io = rb_cIO; + + rb_define_method(io, "nonblock?", rb_io_nonblock_p, 0); + rb_define_method(io, "nonblock=", rb_io_nonblock_set, 1); + rb_define_method(io, "nonblock", rb_io_nonblock_block, -1); +} diff --git a/ext/io/wait/lib/nonblock.rb b/ext/io/wait/lib/nonblock.rb deleted file mode 100644 index 2103fdf25b..0000000000 --- a/ext/io/wait/lib/nonblock.rb +++ /dev/null @@ -1,23 +0,0 @@ -require "fcntl" -class IO - def nonblock? - (fcntl(Fcntl::F_GETFL) & File::NONBLOCK) != 0 - end - - def nonblock=(nb) - f = fcntl(Fcntl::F_GETFL) - if nb - f |= File::NONBLOCK - else - f &= ~File::NONBLOCK - end - fcntl(Fcntl::F_SETFL, f) - end - - def nonblock(nb = true) - nb, self.nonblock = nonblock?, nb - yield - ensure - self.nonblock = nb - end -end if defined?(Fcntl::F_GETFL)