sleepy_penguin.git  about / heads / tags
Linux I/O events for Ruby
blob 8155c4ad49c9ffcd9012f63a6b8ecb0697a16220 8470 bytes (raw)
$ git show pu:ext/sleepy_penguin/epoll.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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
 
#include "sleepy_penguin.h"
#ifdef HAVE_SYS_EPOLL_H
#include <sys/epoll.h>
#include <time.h>
#include "missing_clock_gettime.h"
#include "missing_epoll.h"
#include "missing_rb_thread_fd_close.h"
#include "missing_rb_update_max_fd.h"

static ID id_for_fd;
static VALUE cEpoll;

static uint64_t now_ms(void)
{
	struct timespec now;

	clock_gettime(CLOCK_MONOTONIC, &now);

	return now.tv_sec * 1000 + (now.tv_nsec + 500000) / 1000000;
}

static void pack_event_data(struct epoll_event *event, VALUE obj)
{
	event->data.ptr = (void *)obj;
}

static VALUE unpack_event_data(struct epoll_event *event)
{
	return (VALUE)event->data.ptr;
}

struct ep_per_thread {
	VALUE io;
	int fd;
	int timeout;
	int maxevents;
	int capa;
	struct epoll_event events[FLEX_ARRAY];
};

/* this will raise if the IO is closed */
static int ep_fd_check(struct ep_per_thread *ept)
{
	int save_errno = errno;

	ept->fd = rb_sp_fileno(ept->io);
	errno = save_errno;

	return 1;
}

static struct ep_per_thread *ept_get(int maxevents)
{
	struct ep_per_thread *ept;
	size_t size;

	/* error check here to prevent OOM from posix_memalign */
	if (maxevents <= 0) {
		errno = EINVAL;
		rb_sys_fail("epoll_wait maxevents <= 0");
	}

	size = sizeof(struct ep_per_thread) +
	       sizeof(struct epoll_event) * maxevents;

	ept = rb_sp_gettlsbuf(&size);
	ept->capa = maxevents;
	ept->maxevents = maxevents;

	return ept;
}

/*
 * call-seq:
 *	SleepyPenguin::Epoll::IO.new(flags)	-> Epoll::IO object
 *
 * Creates a new Epoll::IO object with the given +flags+ argument.
 * +flags+ may currently be +CLOEXEC+ or +0+.
 */
static VALUE s_new(VALUE klass, VALUE _flags)
{
	int default_flags = RB_SP_CLOEXEC(EPOLL_CLOEXEC);
	int flags = rb_sp_get_flags(cEpoll, _flags, default_flags);
	int fd = epoll_create1(flags);
	VALUE rv;

	if (fd < 0) {
		if (rb_sp_gc_for_fd(errno))
			fd = epoll_create1(flags);
		if (fd < 0)
			rb_sys_fail("epoll_create1");
	}

	rv = INT2FIX(fd);
	return rb_call_super(1, &rv);
}

/*
 * call-seq:
 * 	epoll_io.epoll_ctl(op, io, events)	-> nil
 *
 * Register, modify, or register a watch for a given +io+ for events.
 *
 * +op+ may be one of +EPOLL_CTL_ADD+, +EPOLL_CTL_MOD+, or +EPOLL_CTL_DEL+
 * +io+ is an IO object or one which proxies via the +to_io+ method.
 * +events+ is an integer mask of events to watch for.
 *
 * Returns nil on success.
 */
static VALUE epctl(VALUE self, VALUE _op, VALUE io, VALUE events)
{
	struct epoll_event event;
	int epfd = rb_sp_fileno(self);
	int fd = rb_sp_fileno(io);
	int op = NUM2INT(_op);
	int rv;

	event.events = NUM2UINT(events);
	pack_event_data(&event, io);

	rv = epoll_ctl(epfd, op, fd, &event);
	if (rv < 0)
		rb_sys_fail("epoll_ctl");

	return Qnil;
}

static VALUE epwait_result(struct ep_per_thread *ept, int n)
{
	int i;
	struct epoll_event *epoll_event = ept->events;
	VALUE obj_events, obj;

	if (n < 0) {
		if (errno == EINTR)
			n = 0;
		else
			rb_sys_fail("epoll_wait");
	}

	for (i = n; --i >= 0; epoll_event++) {
		obj_events = UINT2NUM(epoll_event->events);
		obj = unpack_event_data(epoll_event);
		rb_yield_values(2, obj_events, obj);
	}

	return INT2NUM(n);
}

static int epoll_resume_p(uint64_t expire_at, struct ep_per_thread *ept)
{
	uint64_t now;

	ep_fd_check(ept); /* may raise IOError */

	if (errno != EINTR)
		return 0;
	if (ept->timeout < 0)
		return 1;
	now = now_ms();
	ept->timeout = now > expire_at ? 0 : (int)(expire_at - now);
	return 1;
}

static VALUE nogvl_wait(void *args)
{
	struct ep_per_thread *ept = args;
	int n = epoll_wait(ept->fd, ept->events, ept->maxevents, ept->timeout);

	return (VALUE)n;
}

static VALUE real_epwait(struct ep_per_thread *ept)
{
	long n;
	uint64_t expire_at = ept->timeout > 0 ? now_ms() + ept->timeout : 0;

	ept->fd = rb_sp_fileno(ept->io);
	do {
		n = (long)rb_sp_fd_region(nogvl_wait, ept, ept->fd);
	} while (n < 0 && epoll_resume_p(expire_at, ept));

	return epwait_result(ept, (int)n);
}

/*
 * call-seq:
 *	ep_io.epoll_wait([maxevents[, timeout]]) { |events, io| ... }
 *
 * Calls epoll_wait(2) and yields Integer +events+ and IO objects watched
 * for.  +maxevents+ is the maximum number of events to process at once,
 * lower numbers may prevent starvation when used by epoll_wait in multiple
 * threads.  Larger +maxevents+ reduces syscall overhead for
 * single-threaded applications. +maxevents+ defaults to 64 events.
 * +timeout+ is specified in milliseconds, +nil+
 * (the default) meaning it will block and wait indefinitely.
 */
static VALUE epwait(int argc, VALUE *argv, VALUE self)
{
	VALUE timeout, maxevents;
	struct ep_per_thread *ept;
	int t;

	rb_need_block();
	rb_scan_args(argc, argv, "02", &maxevents, &timeout);
	t = NIL_P(timeout) ? -1 : NUM2INT(timeout);

	ept = ept_get(NIL_P(maxevents) ? 64 : NUM2INT(maxevents));
	ept->timeout = t;
	ept->io = self;

	return rb_ensure(real_epwait, (VALUE)ept, rb_sp_puttlsbuf, (VALUE)ept);
}

/* :nodoc: */
static VALUE event_flags(VALUE self, VALUE flags)
{
	return UINT2NUM(rb_sp_get_uflags(self, flags));
}

void sleepy_penguin_init_epoll(void)
{
	VALUE mSleepyPenguin, cEpoll_IO;

	/*
	 * Document-module: SleepyPenguin
	 *
	 *	require "sleepy_penguin"
	 *	include SleepyPenguin
	 *
	 * The SleepyPenguin namespace includes the Epoll, Inotify,
	 * TimerFD, EventFD classes in its top level and no other constants.
	 *
	 * If you are uncomfortable including SleepyPenguin, you may also
	 * use the "SP" alias if it doesn't conflict with existing code:
	 *
	 *	require "sleepy_penguin/sp"
	 *
	 * And then access classes via:
	 *
	 * - SP::Epoll
	 * - SP::Epoll::IO
	 * - SP::EventFD
	 * - SP::Inotify
	 * - SP::TimerFD
	 */
	mSleepyPenguin = rb_define_module("SleepyPenguin");

	/*
	 * Document-class: SleepyPenguin::Epoll
	 *
	 * The Epoll class provides high-level access to epoll(7)
	 * functionality in the Linux 2.6 and later kernels.  It provides
	 * fork and GC-safety for Ruby objects stored within the IO object
	 * and may be passed as an argument to IO.select.
	 */
	cEpoll = rb_define_class_under(mSleepyPenguin, "Epoll", rb_cObject);

	/*
	 * Document-class: SleepyPenguin::Epoll::IO
	 *
	 * Epoll::IO is a low-level class.  It does not provide fork nor
	 * GC-safety, so Ruby IO objects added via epoll_ctl must be retained
	 * by the application until IO#close is called.
	 */
	cEpoll_IO = rb_define_class_under(cEpoll, "IO", rb_cIO);
	rb_define_singleton_method(cEpoll_IO, "new", s_new, 1);

	rb_define_method(cEpoll_IO, "epoll_ctl", epctl, 3);
	rb_define_method(cEpoll_IO, "epoll_wait", epwait, -1);

	rb_define_method(cEpoll, "__event_flags", event_flags, 1);

	/* registers an IO object via epoll_ctl */
	rb_define_const(cEpoll, "CTL_ADD", INT2NUM(EPOLL_CTL_ADD));

	/* unregisters an IO object via epoll_ctl */
	rb_define_const(cEpoll, "CTL_DEL", INT2NUM(EPOLL_CTL_DEL));

	/* modifies the registration of an IO object via epoll_ctl */
	rb_define_const(cEpoll, "CTL_MOD", INT2NUM(EPOLL_CTL_MOD));

	/* specifies whether close-on-exec flag is set for Epoll.new */
	rb_define_const(cEpoll, "CLOEXEC", INT2NUM(EPOLL_CLOEXEC));

	/* watch for read/recv operations */
	rb_define_const(cEpoll, "IN", UINT2NUM(EPOLLIN));

	/* watch for write/send operations */
	rb_define_const(cEpoll, "OUT", UINT2NUM(EPOLLOUT));

#ifdef EPOLLRDHUP
	/*
	 * Watch a specified io for shutdown(SHUT_WR) on the remote-end.
	 * Available since Linux 2.6.17.
	 */
	rb_define_const(cEpoll, "RDHUP", UINT2NUM(EPOLLRDHUP));
#endif

#ifdef EPOLLWAKEUP
	/*
	 * This prevents system suspend while event is ready.
	 * This requires the caller to have the CAP_BLOCK_SUSPEND capability
	 * Available since Linux 3.5
	 */
	rb_define_const(cEpoll, "WAKEUP", UINT2NUM(EPOLLWAKEUP));
#endif

	/* watch for urgent read(2) data */
	rb_define_const(cEpoll, "PRI", UINT2NUM(EPOLLPRI));

	/*
	 * watch for errors, there is no need to specify this,
	 * it is always monitored when an IO is watched
	 */
	rb_define_const(cEpoll, "ERR", UINT2NUM(EPOLLERR));

	/*
	 * watch for hangups, there is no need to specify this,
	 * it is always monitored when an IO is watched
	 */
	rb_define_const(cEpoll, "HUP", UINT2NUM(EPOLLHUP));

	/* notifications are only Edge Triggered, see epoll(7) */
	rb_define_const(cEpoll, "ET", UINT2NUM((uint32_t)EPOLLET));

	/* unwatch the descriptor once any event has fired */
	rb_define_const(cEpoll, "ONESHOT", UINT2NUM(EPOLLONESHOT));

	id_for_fd = rb_intern("for_fd");

	/*
	 * the high-level interface is implemented in Ruby,
	 * see lib/sleepy_penguin/epoll.rb
	 */
}
#endif /* HAVE_SYS_EPOLL_H */

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