diff options
Diffstat (limited to 'ext/io/nonblock')
| -rw-r--r-- | ext/io/nonblock/depend | 161 | ||||
| -rw-r--r-- | ext/io/nonblock/extconf.rb | 7 | ||||
| -rw-r--r-- | ext/io/nonblock/io-nonblock.gemspec | 25 | ||||
| -rw-r--r-- | ext/io/nonblock/nonblock.c | 136 |
4 files changed, 295 insertions, 34 deletions
diff --git a/ext/io/nonblock/depend b/ext/io/nonblock/depend index 4402898de6..e78a05c316 100644 --- a/ext/io/nonblock/depend +++ b/ext/io/nonblock/depend @@ -1,10 +1,170 @@ # AUTOGENERATED DEPENDENCIES START nonblock.o: $(RUBY_EXTCONF_H) nonblock.o: $(arch_hdrdir)/ruby/config.h +nonblock.o: $(hdrdir)/ruby.h +nonblock.o: $(hdrdir)/ruby/assert.h nonblock.o: $(hdrdir)/ruby/backward.h +nonblock.o: $(hdrdir)/ruby/backward/2/assume.h +nonblock.o: $(hdrdir)/ruby/backward/2/attributes.h +nonblock.o: $(hdrdir)/ruby/backward/2/bool.h +nonblock.o: $(hdrdir)/ruby/backward/2/inttypes.h +nonblock.o: $(hdrdir)/ruby/backward/2/limits.h +nonblock.o: $(hdrdir)/ruby/backward/2/long_long.h +nonblock.o: $(hdrdir)/ruby/backward/2/stdalign.h +nonblock.o: $(hdrdir)/ruby/backward/2/stdarg.h nonblock.o: $(hdrdir)/ruby/defines.h nonblock.o: $(hdrdir)/ruby/encoding.h nonblock.o: $(hdrdir)/ruby/intern.h +nonblock.o: $(hdrdir)/ruby/internal/abi.h +nonblock.o: $(hdrdir)/ruby/internal/anyargs.h +nonblock.o: $(hdrdir)/ruby/internal/arithmetic.h +nonblock.o: $(hdrdir)/ruby/internal/arithmetic/char.h +nonblock.o: $(hdrdir)/ruby/internal/arithmetic/double.h +nonblock.o: $(hdrdir)/ruby/internal/arithmetic/fixnum.h +nonblock.o: $(hdrdir)/ruby/internal/arithmetic/gid_t.h +nonblock.o: $(hdrdir)/ruby/internal/arithmetic/int.h +nonblock.o: $(hdrdir)/ruby/internal/arithmetic/intptr_t.h +nonblock.o: $(hdrdir)/ruby/internal/arithmetic/long.h +nonblock.o: $(hdrdir)/ruby/internal/arithmetic/long_long.h +nonblock.o: $(hdrdir)/ruby/internal/arithmetic/mode_t.h +nonblock.o: $(hdrdir)/ruby/internal/arithmetic/off_t.h +nonblock.o: $(hdrdir)/ruby/internal/arithmetic/pid_t.h +nonblock.o: $(hdrdir)/ruby/internal/arithmetic/short.h +nonblock.o: $(hdrdir)/ruby/internal/arithmetic/size_t.h +nonblock.o: $(hdrdir)/ruby/internal/arithmetic/st_data_t.h +nonblock.o: $(hdrdir)/ruby/internal/arithmetic/uid_t.h +nonblock.o: $(hdrdir)/ruby/internal/assume.h +nonblock.o: $(hdrdir)/ruby/internal/attr/alloc_size.h +nonblock.o: $(hdrdir)/ruby/internal/attr/artificial.h +nonblock.o: $(hdrdir)/ruby/internal/attr/cold.h +nonblock.o: $(hdrdir)/ruby/internal/attr/const.h +nonblock.o: $(hdrdir)/ruby/internal/attr/constexpr.h +nonblock.o: $(hdrdir)/ruby/internal/attr/deprecated.h +nonblock.o: $(hdrdir)/ruby/internal/attr/diagnose_if.h +nonblock.o: $(hdrdir)/ruby/internal/attr/enum_extensibility.h +nonblock.o: $(hdrdir)/ruby/internal/attr/error.h +nonblock.o: $(hdrdir)/ruby/internal/attr/flag_enum.h +nonblock.o: $(hdrdir)/ruby/internal/attr/forceinline.h +nonblock.o: $(hdrdir)/ruby/internal/attr/format.h +nonblock.o: $(hdrdir)/ruby/internal/attr/maybe_unused.h +nonblock.o: $(hdrdir)/ruby/internal/attr/noalias.h +nonblock.o: $(hdrdir)/ruby/internal/attr/nodiscard.h +nonblock.o: $(hdrdir)/ruby/internal/attr/noexcept.h +nonblock.o: $(hdrdir)/ruby/internal/attr/noinline.h +nonblock.o: $(hdrdir)/ruby/internal/attr/nonnull.h +nonblock.o: $(hdrdir)/ruby/internal/attr/noreturn.h +nonblock.o: $(hdrdir)/ruby/internal/attr/packed_struct.h +nonblock.o: $(hdrdir)/ruby/internal/attr/pure.h +nonblock.o: $(hdrdir)/ruby/internal/attr/restrict.h +nonblock.o: $(hdrdir)/ruby/internal/attr/returns_nonnull.h +nonblock.o: $(hdrdir)/ruby/internal/attr/warning.h +nonblock.o: $(hdrdir)/ruby/internal/attr/weakref.h +nonblock.o: $(hdrdir)/ruby/internal/cast.h +nonblock.o: $(hdrdir)/ruby/internal/compiler_is.h +nonblock.o: $(hdrdir)/ruby/internal/compiler_is/apple.h +nonblock.o: $(hdrdir)/ruby/internal/compiler_is/clang.h +nonblock.o: $(hdrdir)/ruby/internal/compiler_is/gcc.h +nonblock.o: $(hdrdir)/ruby/internal/compiler_is/intel.h +nonblock.o: $(hdrdir)/ruby/internal/compiler_is/msvc.h +nonblock.o: $(hdrdir)/ruby/internal/compiler_is/sunpro.h +nonblock.o: $(hdrdir)/ruby/internal/compiler_since.h +nonblock.o: $(hdrdir)/ruby/internal/config.h +nonblock.o: $(hdrdir)/ruby/internal/constant_p.h +nonblock.o: $(hdrdir)/ruby/internal/core.h +nonblock.o: $(hdrdir)/ruby/internal/core/rarray.h +nonblock.o: $(hdrdir)/ruby/internal/core/rbasic.h +nonblock.o: $(hdrdir)/ruby/internal/core/rbignum.h +nonblock.o: $(hdrdir)/ruby/internal/core/rclass.h +nonblock.o: $(hdrdir)/ruby/internal/core/rdata.h +nonblock.o: $(hdrdir)/ruby/internal/core/rfile.h +nonblock.o: $(hdrdir)/ruby/internal/core/rhash.h +nonblock.o: $(hdrdir)/ruby/internal/core/robject.h +nonblock.o: $(hdrdir)/ruby/internal/core/rregexp.h +nonblock.o: $(hdrdir)/ruby/internal/core/rstring.h +nonblock.o: $(hdrdir)/ruby/internal/core/rstruct.h +nonblock.o: $(hdrdir)/ruby/internal/core/rtypeddata.h +nonblock.o: $(hdrdir)/ruby/internal/ctype.h +nonblock.o: $(hdrdir)/ruby/internal/dllexport.h +nonblock.o: $(hdrdir)/ruby/internal/dosish.h +nonblock.o: $(hdrdir)/ruby/internal/encoding/coderange.h +nonblock.o: $(hdrdir)/ruby/internal/encoding/ctype.h +nonblock.o: $(hdrdir)/ruby/internal/encoding/encoding.h +nonblock.o: $(hdrdir)/ruby/internal/encoding/pathname.h +nonblock.o: $(hdrdir)/ruby/internal/encoding/re.h +nonblock.o: $(hdrdir)/ruby/internal/encoding/sprintf.h +nonblock.o: $(hdrdir)/ruby/internal/encoding/string.h +nonblock.o: $(hdrdir)/ruby/internal/encoding/symbol.h +nonblock.o: $(hdrdir)/ruby/internal/encoding/transcode.h +nonblock.o: $(hdrdir)/ruby/internal/error.h +nonblock.o: $(hdrdir)/ruby/internal/eval.h +nonblock.o: $(hdrdir)/ruby/internal/event.h +nonblock.o: $(hdrdir)/ruby/internal/fl_type.h +nonblock.o: $(hdrdir)/ruby/internal/gc.h +nonblock.o: $(hdrdir)/ruby/internal/glob.h +nonblock.o: $(hdrdir)/ruby/internal/globals.h +nonblock.o: $(hdrdir)/ruby/internal/has/attribute.h +nonblock.o: $(hdrdir)/ruby/internal/has/builtin.h +nonblock.o: $(hdrdir)/ruby/internal/has/c_attribute.h +nonblock.o: $(hdrdir)/ruby/internal/has/cpp_attribute.h +nonblock.o: $(hdrdir)/ruby/internal/has/declspec_attribute.h +nonblock.o: $(hdrdir)/ruby/internal/has/extension.h +nonblock.o: $(hdrdir)/ruby/internal/has/feature.h +nonblock.o: $(hdrdir)/ruby/internal/has/warning.h +nonblock.o: $(hdrdir)/ruby/internal/intern/array.h +nonblock.o: $(hdrdir)/ruby/internal/intern/bignum.h +nonblock.o: $(hdrdir)/ruby/internal/intern/class.h +nonblock.o: $(hdrdir)/ruby/internal/intern/compar.h +nonblock.o: $(hdrdir)/ruby/internal/intern/complex.h +nonblock.o: $(hdrdir)/ruby/internal/intern/cont.h +nonblock.o: $(hdrdir)/ruby/internal/intern/dir.h +nonblock.o: $(hdrdir)/ruby/internal/intern/enum.h +nonblock.o: $(hdrdir)/ruby/internal/intern/enumerator.h +nonblock.o: $(hdrdir)/ruby/internal/intern/error.h +nonblock.o: $(hdrdir)/ruby/internal/intern/eval.h +nonblock.o: $(hdrdir)/ruby/internal/intern/file.h +nonblock.o: $(hdrdir)/ruby/internal/intern/hash.h +nonblock.o: $(hdrdir)/ruby/internal/intern/io.h +nonblock.o: $(hdrdir)/ruby/internal/intern/load.h +nonblock.o: $(hdrdir)/ruby/internal/intern/marshal.h +nonblock.o: $(hdrdir)/ruby/internal/intern/numeric.h +nonblock.o: $(hdrdir)/ruby/internal/intern/object.h +nonblock.o: $(hdrdir)/ruby/internal/intern/parse.h +nonblock.o: $(hdrdir)/ruby/internal/intern/proc.h +nonblock.o: $(hdrdir)/ruby/internal/intern/process.h +nonblock.o: $(hdrdir)/ruby/internal/intern/random.h +nonblock.o: $(hdrdir)/ruby/internal/intern/range.h +nonblock.o: $(hdrdir)/ruby/internal/intern/rational.h +nonblock.o: $(hdrdir)/ruby/internal/intern/re.h +nonblock.o: $(hdrdir)/ruby/internal/intern/ruby.h +nonblock.o: $(hdrdir)/ruby/internal/intern/select.h +nonblock.o: $(hdrdir)/ruby/internal/intern/select/largesize.h +nonblock.o: $(hdrdir)/ruby/internal/intern/set.h +nonblock.o: $(hdrdir)/ruby/internal/intern/signal.h +nonblock.o: $(hdrdir)/ruby/internal/intern/sprintf.h +nonblock.o: $(hdrdir)/ruby/internal/intern/string.h +nonblock.o: $(hdrdir)/ruby/internal/intern/struct.h +nonblock.o: $(hdrdir)/ruby/internal/intern/thread.h +nonblock.o: $(hdrdir)/ruby/internal/intern/time.h +nonblock.o: $(hdrdir)/ruby/internal/intern/variable.h +nonblock.o: $(hdrdir)/ruby/internal/intern/vm.h +nonblock.o: $(hdrdir)/ruby/internal/interpreter.h +nonblock.o: $(hdrdir)/ruby/internal/iterator.h +nonblock.o: $(hdrdir)/ruby/internal/memory.h +nonblock.o: $(hdrdir)/ruby/internal/method.h +nonblock.o: $(hdrdir)/ruby/internal/module.h +nonblock.o: $(hdrdir)/ruby/internal/newobj.h +nonblock.o: $(hdrdir)/ruby/internal/scan_args.h +nonblock.o: $(hdrdir)/ruby/internal/special_consts.h +nonblock.o: $(hdrdir)/ruby/internal/static_assert.h +nonblock.o: $(hdrdir)/ruby/internal/stdalign.h +nonblock.o: $(hdrdir)/ruby/internal/stdbool.h +nonblock.o: $(hdrdir)/ruby/internal/stdckdint.h +nonblock.o: $(hdrdir)/ruby/internal/symbol.h +nonblock.o: $(hdrdir)/ruby/internal/value.h +nonblock.o: $(hdrdir)/ruby/internal/value_type.h +nonblock.o: $(hdrdir)/ruby/internal/variable.h +nonblock.o: $(hdrdir)/ruby/internal/warning_push.h +nonblock.o: $(hdrdir)/ruby/internal/xmalloc.h nonblock.o: $(hdrdir)/ruby/io.h nonblock.o: $(hdrdir)/ruby/missing.h nonblock.o: $(hdrdir)/ruby/onigmo.h @@ -12,6 +172,5 @@ nonblock.o: $(hdrdir)/ruby/oniguruma.h nonblock.o: $(hdrdir)/ruby/ruby.h nonblock.o: $(hdrdir)/ruby/st.h nonblock.o: $(hdrdir)/ruby/subst.h -nonblock.o: $(top_srcdir)/include/ruby.h nonblock.o: nonblock.c # AUTOGENERATED DEPENDENCIES END diff --git a/ext/io/nonblock/extconf.rb b/ext/io/nonblock/extconf.rb index d813a01e7c..505c9e6252 100644 --- a/ext/io/nonblock/extconf.rb +++ b/ext/io/nonblock/extconf.rb @@ -2,6 +2,13 @@ require 'mkmf' target = "io/nonblock" +unless RUBY_ENGINE == 'ruby' + File.write("Makefile", dummy_makefile($srcdir).join("")) + return +end + +have_func("rb_io_descriptor", "ruby/io.h") + hdr = %w"fcntl.h" if have_macro("O_NONBLOCK", hdr) and (have_macro("F_GETFL", hdr) or have_macro("F_SETFL", hdr)) diff --git a/ext/io/nonblock/io-nonblock.gemspec b/ext/io/nonblock/io-nonblock.gemspec new file mode 100644 index 0000000000..4f5b8cef6f --- /dev/null +++ b/ext/io/nonblock/io-nonblock.gemspec @@ -0,0 +1,25 @@ +Gem::Specification.new do |spec| + spec.name = "io-nonblock" + spec.version = "0.3.2" + spec.authors = ["Nobu Nakada"] + spec.email = ["nobu@ruby-lang.org"] + + spec.summary = %q{Enables non-blocking mode with IO class} + spec.description = %q{Enables non-blocking mode with IO class} + spec.homepage = "https://github.com/ruby/io-nonblock" + spec.licenses = ["Ruby", "BSD-2-Clause"] + spec.required_ruby_version = Gem::Requirement.new(">= 3.0") + + spec.metadata["homepage_uri"] = spec.homepage + spec.metadata["source_code_uri"] = spec.homepage + + spec.files = %w[ + COPYING + README.md + ext/io/nonblock/depend + ext/io/nonblock/extconf.rb + ext/io/nonblock/nonblock.c + ] + spec.extensions = %w[ext/io/nonblock/extconf.rb] + spec.require_paths = ["lib"] +end diff --git a/ext/io/nonblock/nonblock.c b/ext/io/nonblock/nonblock.c index 1c0bdc68e7..cd40ea3335 100644 --- a/ext/io/nonblock/nonblock.c +++ b/ext/io/nonblock/nonblock.c @@ -17,16 +17,27 @@ #endif #include <fcntl.h> +#ifndef HAVE_RB_IO_DESCRIPTOR +static int +io_descriptor_fallback(VALUE io) +{ + rb_io_t *fptr; + GetOpenFile(io, fptr); + return fptr->fd; +} +#define rb_io_descriptor io_descriptor_fallback +#endif + #ifdef F_GETFL static int -io_nonblock_mode(int fd) +get_fcntl_flags(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) +#define get_fcntl_flags(fd) ((void)(fd), 0) #endif #ifdef F_GETFL @@ -39,10 +50,8 @@ io_nonblock_mode(int fd) 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; + if (get_fcntl_flags(rb_io_descriptor(io)) & O_NONBLOCK) + return Qtrue; return Qfalse; } #else @@ -50,6 +59,15 @@ rb_io_nonblock_p(VALUE io) #endif #ifdef F_SETFL +static void +set_fcntl_flags(int fd, int f) +{ + if (fcntl(fd, F_SETFL, f) == -1) + rb_sys_fail(0); +} + +#ifndef RUBY_IO_NONBLOCK_METHODS + static int io_nonblock_set(int fd, int f, int nb) { @@ -63,8 +81,7 @@ io_nonblock_set(int fd, int f, int nb) return 0; f &= ~O_NONBLOCK; } - if (fcntl(fd, F_SETFL, f) == -1) - rb_sys_fail(0); + set_fcntl_flags(fd, f); return 1; } @@ -74,32 +91,77 @@ io_nonblock_set(int fd, int f, int nb) * * Enables non-blocking mode on a stream when set to * +true+, and blocking mode when set to +false+. + * + * This method set or clear O_NONBLOCK flag for the file descriptor + * in <em>ios</em>. + * + * The behavior of most IO methods is not affected by this flag + * because they retry system calls to complete their task + * after EAGAIN and partial read/write. + * (An exception is IO#syswrite which doesn't retry.) + * + * This method can be used to clear non-blocking mode of standard I/O. + * Since nonblocking methods (read_nonblock, etc.) set non-blocking mode but + * they doesn't clear it, this method is usable as follows. + * + * END { STDOUT.nonblock = false } + * STDOUT.write_nonblock("foo") + * + * Since the flag is shared across processes and + * many non-Ruby commands doesn't expect standard I/O with non-blocking mode, + * it would be safe to clear the flag before Ruby program exits. + * + * For example following Ruby program leaves STDIN/STDOUT/STDER non-blocking mode. + * (STDIN, STDOUT and STDERR are connected to a terminal. + * So making one of them nonblocking-mode effects other two.) + * Thus cat command try to read from standard input and + * it causes "Resource temporarily unavailable" error (EAGAIN). + * + * % ruby -e ' + * STDOUT.write_nonblock("foo\n")'; cat + * foo + * cat: -: Resource temporarily unavailable + * + * Clearing the flag makes the behavior of cat command normal. + * (cat command waits input from standard input.) + * + * % ruby -rio/nonblock -e ' + * END { STDOUT.nonblock = false } + * STDOUT.write_nonblock("foo") + * '; cat + * foo + * */ static VALUE -rb_io_nonblock_set(VALUE io, VALUE nb) +rb_io_nonblock_set(VALUE self, VALUE value) { - rb_io_t *fptr; - GetOpenFile(io, fptr); - if (RTEST(nb)) - rb_io_set_nonblock(fptr); - else - io_nonblock_set(fptr->fd, io_nonblock_mode(fptr->fd), RTEST(nb)); - return io; + if (RTEST(value)) { + rb_io_t *fptr; + GetOpenFile(self, fptr); + rb_io_set_nonblock(fptr); + } + else { + int descriptor = rb_io_descriptor(self); + io_nonblock_set(descriptor, get_fcntl_flags(descriptor), RTEST(value)); + } + + return self; } +#endif /* RUBY_IO_NONBLOCK_METHODS */ + static VALUE io_nonblock_restore(VALUE arg) { int *restore = (int *)arg; - if (fcntl(restore[0], F_SETFL, restore[1]) == -1) - rb_sys_fail(0); + set_fcntl_flags(restore[0], restore[1]); return Qnil; } /* * call-seq: - * io.nonblock {|io| } -> io - * io.nonblock(boolean) {|io| } -> io + * io.nonblock {|io| } -> object + * io.nonblock(boolean) {|io| } -> object * * Yields +self+ in non-blocking mode. * @@ -107,24 +169,25 @@ 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 io) +rb_io_nonblock_block(int argc, VALUE *argv, VALUE self) { int nb = 1; - rb_io_t *fptr; - int f, restore[2]; - GetOpenFile(io, fptr); + int descriptor = rb_io_descriptor(self); + 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); } - f = io_nonblock_mode(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); + + int current_flags = get_fcntl_flags(descriptor); + int restore[2] = {descriptor, current_flags}; + + if (!io_nonblock_set(descriptor, current_flags, nb)) + return rb_yield(self); + + return rb_ensure(rb_yield, self, io_nonblock_restore, (VALUE)restore); } #else #define rb_io_nonblock_set rb_f_notimplement @@ -134,7 +197,14 @@ rb_io_nonblock_block(int argc, VALUE *argv, VALUE io) void Init_nonblock(void) { +#ifdef HAVE_RB_EXT_RACTOR_SAFE + rb_ext_ractor_safe(true); +#endif + +#ifndef RUBY_IO_NONBLOCK_METHODS rb_define_method(rb_cIO, "nonblock?", rb_io_nonblock_p, 0); rb_define_method(rb_cIO, "nonblock=", rb_io_nonblock_set, 1); +#endif + rb_define_method(rb_cIO, "nonblock", rb_io_nonblock_block, -1); } |
