1 #include "sleepy_penguin.h"
3 static VALUE
klass_for(VALUE klass
)
5 return (TYPE(klass
) == T_CLASS
) ? klass
: CLASS_OF(klass
);
8 int rb_sp_get_flags(VALUE klass
, VALUE flags
, int default_flags
)
10 switch (TYPE(flags
)) {
11 case T_NIL
: return default_flags
;
12 case T_FIXNUM
: return FIX2INT(flags
);
13 case T_BIGNUM
: return NUM2INT(flags
);
15 return NUM2INT(rb_const_get(klass_for(klass
), SYM2ID(flags
)));
17 VALUE
*ptr
= RARRAY_PTR(flags
);
18 long len
= RARRAY_LEN(flags
);
21 klass
= klass_for(klass
);
25 Check_Type(tmp
, T_SYMBOL
);
26 tmp
= rb_const_get(klass
, SYM2ID(tmp
));
32 rb_raise(rb_eTypeError
, "invalid flags");
36 unsigned rb_sp_get_uflags(VALUE klass
, VALUE flags
)
38 switch (TYPE(flags
)) {
40 case T_FIXNUM
: return FIX2UINT(flags
);
41 case T_BIGNUM
: return NUM2UINT(flags
);
43 return NUM2UINT(rb_const_get(klass_for(klass
), SYM2ID(flags
)));
45 VALUE
*ptr
= RARRAY_PTR(flags
);
46 long len
= RARRAY_LEN(flags
);
49 klass
= klass_for(klass
);
53 Check_Type(tmp
, T_SYMBOL
);
54 tmp
= rb_const_get(klass
, SYM2ID(tmp
));
60 rb_raise(rb_eTypeError
, "invalid flags");
65 # define rb_io_t OpenFile
69 # define FPTR_TO_FD(fptr) (fileno(GetReadFile(fptr)))
71 # if !HAVE_RB_IO_T || (RUBY_VERSION_MAJOR == 1 && RUBY_VERSION_MINOR == 8)
72 # define FPTR_TO_FD(fptr) fileno(fptr->f)
74 # define FPTR_TO_FD(fptr) fptr->fd
78 static int fixint_closed_p(VALUE io
)
80 return (fcntl(FIX2INT(io
), F_GETFD
) == -1 && errno
== EBADF
);
83 #if defined(RFILE) && defined(HAVE_ST_FD)
84 static int my_rb_io_closed(VALUE io
)
86 return RFILE(io
)->fptr
->fd
< 0;
89 static int my_rb_io_closed(VALUE io
)
91 return rb_funcall(io
, rb_intern("closed?"), 0) == Qtrue
;
95 int rb_sp_io_closed(VALUE io
)
99 return fixint_closed_p(io
);
103 io
= rb_convert_type(io
, T_FILE
, "IO", "to_io");
106 return my_rb_io_closed(io
);
109 int rb_sp_fileno(VALUE io
)
113 io
= rb_convert_type(io
, T_FILE
, "IO", "to_io");
114 GetOpenFile(io
, fptr
);
115 return FPTR_TO_FD(fptr
);
118 void rb_sp_set_nonblock(int fd
)
120 int flags
= fcntl(fd
, F_GETFL
);
123 rb_sys_fail("fcntl(F_GETFL)");
124 if ((flags
& O_NONBLOCK
) == O_NONBLOCK
)
127 * Note: while this is Linux-only and we could safely rely on
128 * ioctl(FIONBIO), needing F_SETFL is an uncommon path, and
129 * F_GETFL is lockless. ioctl(FIONBIO) always acquires a spin
130 * lock, so it's more expensive even if we do not need to change
133 flags
= fcntl(fd
, F_SETFL
, flags
| O_NONBLOCK
);
135 rb_sys_fail("fcntl(F_SETFL)");
138 int rb_sp_wait(rb_sp_waitfn waiter
, VALUE obj
, int *fd
)
141 * we need to check the fileno before and after waiting, a close()
142 * could've happened at any time (especially when outside of GVL).
144 int rc
= waiter(rb_sp_fileno(obj
));
145 *fd
= rb_sp_fileno(obj
);