avoid errno side-effects in kgio_wait_*able
[kgio.git] / ext / kgio / wait.c
blobc6ed610b2732f6aa517c26059f03f79615e2abef
1 #include "kgio.h"
3 static ID id_wait_rd, id_wait_wr;
5 /*
6 * avoiding rb_thread_select() or similar since rb_io_wait_*able can be
7 * made to use poll() later on. It's highly unlikely Ruby will move to
8 * use an edge-triggered event notification, so assigning EAGAIN is
9 * probably safe...
14 * Blocks the running Thread indefinitely until +self+ IO object is readable.
15 * This method is automatically called by default whenever kgio_read needs
16 * to block on input.
18 * Users of alternative threading/fiber libraries are
19 * encouraged to override this method in their subclasses or modules to
20 * work with their threading/blocking methods.
22 static VALUE kgio_wait_readable(VALUE self)
24 int fd = my_fileno(self);
26 errno = EAGAIN;
27 if (!rb_io_wait_readable(fd))
28 rb_sys_fail("kgio_wait_readable");
30 return self;
34 * Blocks the running Thread indefinitely until +self+ IO object is writable.
35 * This method is automatically called whenever kgio_write needs to
36 * block on output.
38 * Users of alternative threading/fiber libraries are
39 * encouraged to override this method in their subclasses or modules to
40 * work with their threading/blocking methods.
42 static VALUE kgio_wait_writable(VALUE self)
44 int fd = my_fileno(self);
46 errno = EAGAIN;
47 if (!rb_io_wait_writable(fd))
48 rb_sys_fail("kgio_wait_writable");
50 return self;
53 VALUE kgio_call_wait_writable(VALUE io)
55 return rb_funcall(io, id_wait_wr, 0, 0);
58 VALUE kgio_call_wait_readable(VALUE io)
60 return rb_funcall(io, id_wait_rd, 0, 0);
63 void init_kgio_wait(void)
65 VALUE mKgio = rb_define_module("Kgio");
68 * Document-module: Kgio::DefaultWaiters
70 * This module contains default kgio_wait_readable and
71 * kgio_wait_writable methods that block indefinitely (in a
72 * thread-safe manner) until an IO object is read or writable.
73 * This module is included in the Kgio::PipeMethods and
74 * Kgio::SocketMethods modules used by all bundled IO-derived
75 * objects.
77 VALUE mWaiters = rb_define_module_under(mKgio, "DefaultWaiters");
79 id_wait_rd = rb_intern("kgio_wait_readable");
80 id_wait_wr = rb_intern("kgio_wait_writable");
82 rb_define_method(mWaiters, "kgio_wait_readable",
83 kgio_wait_readable, 0);
84 rb_define_method(mWaiters, "kgio_wait_writable",
85 kgio_wait_writable, 0);