ext/io/nonblock/nonblock.c in io-nonblock-0.2.0 vs ext/io/nonblock/nonblock.c in io-nonblock-0.3.0
- old
+ new
@@ -15,10 +15,21 @@
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#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)
{
int f = fcntl(fd, F_GETFL);
@@ -37,14 +48,12 @@
* Returns +true+ if an IO object is in non-blocking mode.
*/
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
#define rb_io_nonblock_p rb_f_notimplement
#endif
@@ -55,10 +64,12 @@
{
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)
{
if (nb) {
if ((f & O_NONBLOCK) != 0)
@@ -120,21 +131,27 @@
* '; 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, 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)
{
int *restore = (int *)arg;
set_fcntl_flags(restore[0], restore[1]);
@@ -150,36 +167,40 @@
*
* When +false+ is given as an argument, +self+ is yielded in blocking mode.
* 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
#define rb_io_nonblock_block rb_f_notimplement
#endif
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);
}