kgio.git  about / heads / tags
kinder, gentler I/O for Ruby
blob c6ed610b2732f6aa517c26059f03f79615e2abef 2318 bytes (raw)
$ git show 2.4-stable:ext/kgio/wait.c	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 
#include "kgio.h"

static ID id_wait_rd, id_wait_wr;

/*
 * avoiding rb_thread_select() or similar since rb_io_wait_*able can be
 * made to use poll() later on.  It's highly unlikely Ruby will move to
 * use an edge-triggered event notification, so assigning EAGAIN is
 * probably safe...
 */


/*
 * Blocks the running Thread indefinitely until +self+ IO object is readable.
 * This method is automatically called by default whenever kgio_read needs
 * to block on input.
 *
 * Users of alternative threading/fiber libraries are
 * encouraged to override this method in their subclasses or modules to
 * work with their threading/blocking methods.
 */
static VALUE kgio_wait_readable(VALUE self)
{
	int fd = my_fileno(self);

	errno = EAGAIN;
	if (!rb_io_wait_readable(fd))
		rb_sys_fail("kgio_wait_readable");

	return self;
}

/*
 * Blocks the running Thread indefinitely until +self+ IO object is writable.
 * This method is automatically called whenever kgio_write needs to
 * block on output.
 *
 * Users of alternative threading/fiber libraries are
 * encouraged to override this method in their subclasses or modules to
 * work with their threading/blocking methods.
 */
static VALUE kgio_wait_writable(VALUE self)
{
	int fd = my_fileno(self);

	errno = EAGAIN;
	if (!rb_io_wait_writable(fd))
		rb_sys_fail("kgio_wait_writable");

	return self;
}

VALUE kgio_call_wait_writable(VALUE io)
{
	return rb_funcall(io, id_wait_wr, 0, 0);
}

VALUE kgio_call_wait_readable(VALUE io)
{
	return rb_funcall(io, id_wait_rd, 0, 0);
}

void init_kgio_wait(void)
{
	VALUE mKgio = rb_define_module("Kgio");

	/*
	 * Document-module: Kgio::DefaultWaiters
	 *
	 * This module contains default kgio_wait_readable and
	 * kgio_wait_writable methods that block indefinitely (in a
	 * thread-safe manner) until an IO object is read or writable.
	 * This module is included in the Kgio::PipeMethods and
	 * Kgio::SocketMethods modules used by all bundled IO-derived
	 * objects.
	 */
	VALUE mWaiters = rb_define_module_under(mKgio, "DefaultWaiters");

	id_wait_rd = rb_intern("kgio_wait_readable");
	id_wait_wr = rb_intern("kgio_wait_writable");

	rb_define_method(mWaiters, "kgio_wait_readable",
	                 kgio_wait_readable, 0);
	rb_define_method(mWaiters, "kgio_wait_writable",
	                 kgio_wait_writable, 0);
}

git clone git://yhbt.net/kgio.git
git clone https://yhbt.net/kgio.git