summaryrefslogtreecommitdiff
path: root/ext/io/nonblock
diff options
context:
space:
mode:
Diffstat (limited to 'ext/io/nonblock')
-rw-r--r--ext/io/nonblock/depend4
-rw-r--r--ext/io/nonblock/extconf.rb7
-rw-r--r--ext/io/nonblock/io-nonblock.gemspec2
-rw-r--r--ext/io/nonblock/nonblock.c71
4 files changed, 56 insertions, 28 deletions
diff --git a/ext/io/nonblock/depend b/ext/io/nonblock/depend
index 7f2db65732..20a96f1252 100644
--- a/ext/io/nonblock/depend
+++ b/ext/io/nonblock/depend
@@ -53,6 +53,7 @@ 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
@@ -121,7 +122,6 @@ 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/gc.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
@@ -152,12 +152,12 @@ 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/rgengc.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
diff --git a/ext/io/nonblock/extconf.rb b/ext/io/nonblock/extconf.rb
index d813a01e7c..a1e6075c9b 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")
+
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
index f81d4fda0a..6a16c8b03b 100644
--- a/ext/io/nonblock/io-nonblock.gemspec
+++ b/ext/io/nonblock/io-nonblock.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = "io-nonblock"
- spec.version = "0.1.1"
+ spec.version = "0.3.0"
spec.authors = ["Nobu Nakada"]
spec.email = ["nobu@ruby-lang.org"]
diff --git a/ext/io/nonblock/nonblock.c b/ext/io/nonblock/nonblock.c
index b8a40ff38e..d90538f735 100644
--- a/ext/io/nonblock/nonblock.c
+++ b/ext/io/nonblock/nonblock.c
@@ -17,6 +17,17 @@
#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
get_fcntl_flags(int fd)
@@ -39,10 +50,8 @@ get_fcntl_flags(int fd)
static VALUE
rb_io_nonblock_p(VALUE io)
{
- rb_io_t *fptr;
- GetOpenFile(io, fptr);
- if (get_fcntl_flags(fptr->fd) & O_NONBLOCK)
- return Qtrue;
+ if (get_fcntl_flags(rb_io_descriptor(io)) & O_NONBLOCK)
+ return Qtrue;
return Qfalse;
}
#else
@@ -57,6 +66,8 @@ set_fcntl_flags(int fd, int f)
rb_sys_fail(0);
}
+#ifndef RUBY_IO_NONBLOCK_METHODS
+
static int
io_nonblock_set(int fd, int f, int nb)
{
@@ -122,17 +133,23 @@ io_nonblock_set(int fd, int f, int nb)
*
*/
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, get_fcntl_flags(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)
{
@@ -152,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 = 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);
+
+ 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
@@ -179,7 +197,10 @@ rb_io_nonblock_block(int argc, VALUE *argv, VALUE io)
void
Init_nonblock(void)
{
+#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);
}