Ruby 3.2.2p53 (2023-03-30 revision e51014f9c05aa65cbf203442d37fef7c12390015)
scheduler.c
1/**********************************************************************
2
3 scheduler.c
4
5 $Author$
6
7 Copyright (C) 2020 Samuel Grant Dawson Williams
8
9**********************************************************************/
10
11#include "vm_core.h"
13#include "ruby/io.h"
14#include "ruby/io/buffer.h"
15
16#include "internal/thread.h"
17
18static ID id_close;
19static ID id_scheduler_close;
20
21static ID id_block;
22static ID id_unblock;
23
24static ID id_timeout_after;
25static ID id_kernel_sleep;
26static ID id_process_wait;
27
28static ID id_io_read, id_io_pread;
29static ID id_io_write, id_io_pwrite;
30static ID id_io_wait;
31static ID id_io_select;
32static ID id_io_close;
33
34static ID id_address_resolve;
35
36static ID id_fiber_schedule;
37
38/*
39 * Document-class: Fiber::Scheduler
40 *
41 * This is not an existing class, but documentation of the interface that Scheduler
42 * object should comply to in order to be used as argument to Fiber.scheduler and handle non-blocking
43 * fibers. See also the "Non-blocking fibers" section in Fiber class docs for explanations
44 * of some concepts.
45 *
46 * Scheduler's behavior and usage are expected to be as follows:
47 *
48 * * When the execution in the non-blocking Fiber reaches some blocking operation (like
49 * sleep, wait for a process, or a non-ready I/O), it calls some of the scheduler's
50 * hook methods, listed below.
51 * * Scheduler somehow registers what the current fiber is waiting on, and yields control
52 * to other fibers with Fiber.yield (so the fiber would be suspended while expecting its
53 * wait to end, and other fibers in the same thread can perform)
54 * * At the end of the current thread execution, the scheduler's method #scheduler_close is called
55 * * The scheduler runs into a wait loop, checking all the blocked fibers (which it has
56 * registered on hook calls) and resuming them when the awaited resource is ready
57 * (e.g. I/O ready or sleep time elapsed).
58 *
59 * This way concurrent execution will be achieved transparently for every
60 * individual Fiber's code.
61 *
62 * Scheduler implementations are provided by gems, like
63 * Async[https://github.com/socketry/async].
64 *
65 * Hook methods are:
66 *
67 * * #io_wait, #io_read, #io_write, #io_pread, #io_pwrite, and #io_select, #io_close
68 * * #process_wait
69 * * #kernel_sleep
70 * * #timeout_after
71 * * #address_resolve
72 * * #block and #unblock
73 * * (the list is expanded as Ruby developers make more methods having non-blocking calls)
74 *
75 * When not specified otherwise, the hook implementations are mandatory: if they are not
76 * implemented, the methods trying to call hook will fail. To provide backward compatibility,
77 * in the future hooks will be optional (if they are not implemented, due to the scheduler
78 * being created for the older Ruby version, the code which needs this hook will not fail,
79 * and will just behave in a blocking fashion).
80 *
81 * It is also strongly recommended that the scheduler implements the #fiber method, which is
82 * delegated to by Fiber.schedule.
83 *
84 * Sample _toy_ implementation of the scheduler can be found in Ruby's code, in
85 * <tt>test/fiber/scheduler.rb</tt>
86 *
87 */
88void
89Init_Fiber_Scheduler(void)
90{
91 id_close = rb_intern_const("close");
92 id_scheduler_close = rb_intern_const("scheduler_close");
93
94 id_block = rb_intern_const("block");
95 id_unblock = rb_intern_const("unblock");
96
97 id_timeout_after = rb_intern_const("timeout_after");
98 id_kernel_sleep = rb_intern_const("kernel_sleep");
99 id_process_wait = rb_intern_const("process_wait");
100
101 id_io_read = rb_intern_const("io_read");
102 id_io_pread = rb_intern_const("io_pread");
103 id_io_write = rb_intern_const("io_write");
104 id_io_pwrite = rb_intern_const("io_pwrite");
105
106 id_io_wait = rb_intern_const("io_wait");
107 id_io_select = rb_intern_const("io_select");
108 id_io_close = rb_intern_const("io_close");
109
110 id_address_resolve = rb_intern_const("address_resolve");
111
112 id_fiber_schedule = rb_intern_const("fiber");
113
114#if 0 /* for RDoc */
115 rb_cFiberScheduler = rb_define_class_under(rb_cFiber, "Scheduler", rb_cObject);
116 rb_define_method(rb_cFiberScheduler, "close", rb_fiber_scheduler_close, 0);
117 rb_define_method(rb_cFiberScheduler, "process_wait", rb_fiber_scheduler_process_wait, 2);
118 rb_define_method(rb_cFiberScheduler, "io_wait", rb_fiber_scheduler_io_wait, 3);
119 rb_define_method(rb_cFiberScheduler, "io_read", rb_fiber_scheduler_io_read, 4);
120 rb_define_method(rb_cFiberScheduler, "io_write", rb_fiber_scheduler_io_write, 4);
121 rb_define_method(rb_cFiberScheduler, "io_pread", rb_fiber_scheduler_io_pread, 5);
122 rb_define_method(rb_cFiberScheduler, "io_pwrite", rb_fiber_scheduler_io_pwrite, 5);
123 rb_define_method(rb_cFiberScheduler, "io_select", rb_fiber_scheduler_io_select, 4);
124 rb_define_method(rb_cFiberScheduler, "kernel_sleep", rb_fiber_scheduler_kernel_sleep, 1);
125 rb_define_method(rb_cFiberScheduler, "address_resolve", rb_fiber_scheduler_address_resolve, 1);
126 rb_define_method(rb_cFiberScheduler, "timeout_after", rb_fiber_scheduler_timeout_after, 3);
127 rb_define_method(rb_cFiberScheduler, "block", rb_fiber_scheduler_block, 2);
128 rb_define_method(rb_cFiberScheduler, "unblock", rb_fiber_scheduler_unblock, 2);
129 rb_define_method(rb_cFiberScheduler, "fiber", rb_fiber_scheduler, -2);
130#endif
131}
132
133VALUE
135{
136 VM_ASSERT(ruby_thread_has_gvl_p());
137
138 rb_thread_t *thread = GET_THREAD();
139 VM_ASSERT(thread);
140
141 return thread->scheduler;
142}
143
144static void
145verify_interface(VALUE scheduler)
146{
147 if (!rb_respond_to(scheduler, id_block)) {
148 rb_raise(rb_eArgError, "Scheduler must implement #block");
149 }
150
151 if (!rb_respond_to(scheduler, id_unblock)) {
152 rb_raise(rb_eArgError, "Scheduler must implement #unblock");
153 }
154
155 if (!rb_respond_to(scheduler, id_kernel_sleep)) {
156 rb_raise(rb_eArgError, "Scheduler must implement #kernel_sleep");
157 }
158
159 if (!rb_respond_to(scheduler, id_io_wait)) {
160 rb_raise(rb_eArgError, "Scheduler must implement #io_wait");
161 }
162}
163
164VALUE
166{
167 VM_ASSERT(ruby_thread_has_gvl_p());
168
169 rb_thread_t *thread = GET_THREAD();
170 VM_ASSERT(thread);
171
172 if (scheduler != Qnil) {
173 verify_interface(scheduler);
174 }
175
176 // We invoke Scheduler#close when setting it to something else, to ensure
177 // the previous scheduler runs to completion before changing the scheduler.
178 // That way, we do not need to consider interactions, e.g., of a Fiber from
179 // the previous scheduler with the new scheduler.
180 if (thread->scheduler != Qnil) {
181 rb_fiber_scheduler_close(thread->scheduler);
182 }
183
184 thread->scheduler = scheduler;
185
186 return thread->scheduler;
187}
188
189static VALUE
190rb_fiber_scheduler_current_for_threadptr(rb_thread_t *thread)
191{
192 VM_ASSERT(thread);
193
194 if (thread->blocking == 0) {
195 return thread->scheduler;
196 }
197 else {
198 return Qnil;
199 }
200}
201
202VALUE
204{
205 return rb_fiber_scheduler_current_for_threadptr(GET_THREAD());
206}
207
209{
210 return rb_fiber_scheduler_current_for_threadptr(rb_thread_ptr(thread));
211}
212
213/*
214 *
215 * Document-method: Fiber::Scheduler#close
216 *
217 * Called when the current thread exits. The scheduler is expected to implement this
218 * method in order to allow all waiting fibers to finalize their execution.
219 *
220 * The suggested pattern is to implement the main event loop in the #close method.
221 *
222 */
223VALUE
225{
226 VM_ASSERT(ruby_thread_has_gvl_p());
227
228 VALUE result;
229
230 // The reason for calling `scheduler_close` before calling `close` is for
231 // legacy schedulers which implement `close` and expect the user to call
232 // it. Subsequently, that method would call `Fiber.set_scheduler(nil)`
233 // which should call `scheduler_close`. If it were to call `close`, it
234 // would create an infinite loop.
235
236 result = rb_check_funcall(scheduler, id_scheduler_close, 0, NULL);
237 if (!UNDEF_P(result)) return result;
238
239 result = rb_check_funcall(scheduler, id_close, 0, NULL);
240 if (!UNDEF_P(result)) return result;
241
242 return Qnil;
243}
244
245VALUE
247{
248 if (timeout) {
249 return rb_float_new((double)timeout->tv_sec + (0.000001f * timeout->tv_usec));
250 }
251
252 return Qnil;
253}
254
255/*
256 * Document-method: Fiber::Scheduler#kernel_sleep
257 * call-seq: kernel_sleep(duration = nil)
258 *
259 * Invoked by Kernel#sleep and Mutex#sleep and is expected to provide
260 * an implementation of sleeping in a non-blocking way. Implementation might
261 * register the current fiber in some list of "which fiber wait until what
262 * moment", call Fiber.yield to pass control, and then in #close resume
263 * the fibers whose wait period has elapsed.
264 *
265 */
266VALUE
268{
269 return rb_funcall(scheduler, id_kernel_sleep, 1, timeout);
270}
271
272VALUE
273rb_fiber_scheduler_kernel_sleepv(VALUE scheduler, int argc, VALUE * argv)
274{
275 return rb_funcallv(scheduler, id_kernel_sleep, argc, argv);
276}
277
278#if 0
279/*
280 * Document-method: Fiber::Scheduler#timeout_after
281 * call-seq: timeout_after(duration, exception_class, *exception_arguments, &block) -> result of block
282 *
283 * Invoked by Timeout.timeout to execute the given +block+ within the given
284 * +duration+. It can also be invoked directly by the scheduler or user code.
285 *
286 * Attempt to limit the execution time of a given +block+ to the given
287 * +duration+ if possible. When a non-blocking operation causes the +block+'s
288 * execution time to exceed the specified +duration+, that non-blocking
289 * operation should be interrupted by raising the specified +exception_class+
290 * constructed with the given +exception_arguments+.
291 *
292 * General execution timeouts are often considered risky. This implementation
293 * will only interrupt non-blocking operations. This is by design because it's
294 * expected that non-blocking operations can fail for a variety of
295 * unpredictable reasons, so applications should already be robust in handling
296 * these conditions and by implication timeouts.
297 *
298 * However, as a result of this design, if the +block+ does not invoke any
299 * non-blocking operations, it will be impossible to interrupt it. If you
300 * desire to provide predictable points for timeouts, consider adding
301 * +sleep(0)+.
302 *
303 * If the block is executed successfully, its result will be returned.
304 *
305 * The exception will typically be raised using Fiber#raise.
306 */
307VALUE
308rb_fiber_scheduler_timeout_after(VALUE scheduler, VALUE timeout, VALUE exception, VALUE message)
309{
310 VALUE arguments[] = {
311 timeout, exception, message
312 };
313
314 return rb_check_funcall(scheduler, id_timeout_after, 3, arguments);
315}
316
317VALUE
318rb_fiber_scheduler_timeout_afterv(VALUE scheduler, int argc, VALUE * argv)
319{
320 return rb_check_funcall(scheduler, id_timeout_after, argc, argv);
321}
322#endif
323
324/*
325 * Document-method: Fiber::Scheduler#process_wait
326 * call-seq: process_wait(pid, flags)
327 *
328 * Invoked by Process::Status.wait in order to wait for a specified process.
329 * See that method description for arguments description.
330 *
331 * Suggested minimal implementation:
332 *
333 * Thread.new do
334 * Process::Status.wait(pid, flags)
335 * end.value
336 *
337 * This hook is optional: if it is not present in the current scheduler,
338 * Process::Status.wait will behave as a blocking method.
339 *
340 * Expected to return a Process::Status instance.
341 */
342VALUE
343rb_fiber_scheduler_process_wait(VALUE scheduler, rb_pid_t pid, int flags)
344{
345 VALUE arguments[] = {
346 PIDT2NUM(pid), RB_INT2NUM(flags)
347 };
348
349 return rb_check_funcall(scheduler, id_process_wait, 2, arguments);
350}
351
352/*
353 * Document-method: Fiber::Scheduler#block
354 * call-seq: block(blocker, timeout = nil)
355 *
356 * Invoked by methods like Thread.join, and by Mutex, to signify that current
357 * Fiber is blocked until further notice (e.g. #unblock) or until +timeout+ has
358 * elapsed.
359 *
360 * +blocker+ is what we are waiting on, informational only (for debugging and
361 * logging). There are no guarantee about its value.
362 *
363 * Expected to return boolean, specifying whether the blocking operation was
364 * successful or not.
365 */
366VALUE
367rb_fiber_scheduler_block(VALUE scheduler, VALUE blocker, VALUE timeout)
368{
369 return rb_funcall(scheduler, id_block, 2, blocker, timeout);
370}
371
372/*
373 * Document-method: Fiber::Scheduler#unblock
374 * call-seq: unblock(blocker, fiber)
375 *
376 * Invoked to wake up Fiber previously blocked with #block (for example, Mutex#lock
377 * calls #block and Mutex#unlock calls #unblock). The scheduler should use
378 * the +fiber+ parameter to understand which fiber is unblocked.
379 *
380 * +blocker+ is what was awaited for, but it is informational only (for debugging
381 * and logging), and it is not guaranteed to be the same value as the +blocker+ for
382 * #block.
383 *
384 */
385VALUE
386rb_fiber_scheduler_unblock(VALUE scheduler, VALUE blocker, VALUE fiber)
387{
388 VM_ASSERT(rb_obj_is_fiber(fiber));
389
390 return rb_funcall(scheduler, id_unblock, 2, blocker, fiber);
391}
392
393/*
394 * Document-method: Fiber::Scheduler#io_wait
395 * call-seq: io_wait(io, events, timeout)
396 *
397 * Invoked by IO#wait, IO#wait_readable, IO#wait_writable to ask whether the
398 * specified descriptor is ready for specified events within
399 * the specified +timeout+.
400 *
401 * +events+ is a bit mask of <tt>IO::READABLE</tt>, <tt>IO::WRITABLE</tt>, and
402 * <tt>IO::PRIORITY</tt>.
403 *
404 * Suggested implementation should register which Fiber is waiting for which
405 * resources and immediately calling Fiber.yield to pass control to other
406 * fibers. Then, in the #close method, the scheduler might dispatch all the
407 * I/O resources to fibers waiting for it.
408 *
409 * Expected to return the subset of events that are ready immediately.
410 *
411 */
412VALUE
413rb_fiber_scheduler_io_wait(VALUE scheduler, VALUE io, VALUE events, VALUE timeout)
414{
415 return rb_funcall(scheduler, id_io_wait, 3, io, events, timeout);
416}
417
418VALUE
423
424VALUE
429
430/*
431 * Document-method: Fiber::Scheduler#io_select
432 * call-seq: io_select(readables, writables, exceptables, timeout)
433 *
434 * Invoked by IO.select to ask whether the specified descriptors are ready for
435 * specified events within the specified +timeout+.
436 *
437 * Expected to return the 3-tuple of Array of IOs that are ready.
438 *
439 */
440VALUE rb_fiber_scheduler_io_select(VALUE scheduler, VALUE readables, VALUE writables, VALUE exceptables, VALUE timeout)
441{
442 VALUE arguments[] = {
443 readables, writables, exceptables, timeout
444 };
445
446 return rb_fiber_scheduler_io_selectv(scheduler, 4, arguments);
447}
448
450{
451 // I wondered about extracting argv, and checking if there is only a single
452 // IO instance, and instead calling `io_wait`. However, it would require a
453 // decent amount of work and it would be hard to preserve the exact
454 // semantics of IO.select.
455
456 return rb_check_funcall(scheduler, id_io_select, argc, argv);
457}
458
459/*
460 * Document-method: Fiber::Scheduler#io_read
461 * call-seq: io_read(io, buffer, length) -> read length or -errno
462 *
463 * Invoked by IO#read to read +length+ bytes from +io+ into a specified
464 * +buffer+ (see IO::Buffer).
465 *
466 * The +length+ argument is the "minimum length to be read".
467 * If the IO buffer size is 8KiB, but the +length+ is +1024+ (1KiB), up to
468 * 8KiB might be read, but at least 1KiB will be.
469 * Generally, the only case where less data than +length+ will be read is if
470 * there is an error reading the data.
471 *
472 * Specifying a +length+ of 0 is valid and means try reading at least once
473 * and return any available data.
474 *
475 * Suggested implementation should try to read from +io+ in a non-blocking
476 * manner and call #io_wait if the +io+ is not ready (which will yield control
477 * to other fibers).
478 *
479 * See IO::Buffer for an interface available to return data.
480 *
481 * Expected to return number of bytes read, or, in case of an error, <tt>-errno</tt>
482 * (negated number corresponding to system's error code).
483 *
484 * The method should be considered _experimental_.
485 */
486VALUE
487rb_fiber_scheduler_io_read(VALUE scheduler, VALUE io, VALUE buffer, size_t length, size_t offset)
488{
489 VALUE arguments[] = {
490 io, buffer, SIZET2NUM(length), SIZET2NUM(offset)
491 };
492
493 return rb_check_funcall(scheduler, id_io_read, 4, arguments);
494}
495
496
497/*
498 * Document-method: Fiber::Scheduler#io_read
499 * call-seq: io_pread(io, buffer, from, length, offset) -> read length or -errno
500 *
501 * Invoked by IO::Buffer#pread. See that method for description of arguments.
502 *
503 */
504VALUE
505rb_fiber_scheduler_io_pread(VALUE scheduler, VALUE io, rb_off_t from, VALUE buffer, size_t length, size_t offset)
506{
507 VALUE arguments[] = {
508 io, buffer, OFFT2NUM(from), SIZET2NUM(length), SIZET2NUM(offset)
509 };
510
511 return rb_check_funcall(scheduler, id_io_pread, 5, arguments);
512}
513
514/*
515 * Document-method: Scheduler#io_write
516 * call-seq: io_write(io, buffer, length) -> written length or -errno
517 *
518 * Invoked by IO#write to write +length+ bytes to +io+ from
519 * from a specified +buffer+ (see IO::Buffer).
520 *
521 * The +length+ argument is the "(minimum) length to be written".
522 * If the IO buffer size is 8KiB, but the +length+ specified is 1024 (1KiB),
523 * at most 8KiB will be written, but at least 1KiB will be.
524 * Generally, the only case where less data than +length+ will be written is if
525 * there is an error writing the data.
526 *
527 * Specifying a +length+ of 0 is valid and means try writing at least once,
528 * as much data as possible.
529 *
530 * Suggested implementation should try to write to +io+ in a non-blocking
531 * manner and call #io_wait if the +io+ is not ready (which will yield control
532 * to other fibers).
533 *
534 * See IO::Buffer for an interface available to get data from buffer efficiently.
535 *
536 * Expected to return number of bytes written, or, in case of an error, <tt>-errno</tt>
537 * (negated number corresponding to system's error code).
538 *
539 * The method should be considered _experimental_.
540 */
541VALUE
542rb_fiber_scheduler_io_write(VALUE scheduler, VALUE io, VALUE buffer, size_t length, size_t offset)
543{
544 VALUE arguments[] = {
545 io, buffer, SIZET2NUM(length), SIZET2NUM(offset)
546 };
547
548 return rb_check_funcall(scheduler, id_io_write, 4, arguments);
549}
550
551/*
552 * Document-method: Fiber::Scheduler#io_pwrite
553 * call-seq: io_pwrite(io, buffer, from, length, offset) -> written length or -errno
554 *
555 * Invoked by IO::Buffer#pwrite. See that method for description of arguments.
556 *
557 */
558VALUE
559rb_fiber_scheduler_io_pwrite(VALUE scheduler, VALUE io, rb_off_t from, VALUE buffer, size_t length, size_t offset)
560{
561 VALUE arguments[] = {
562 io, buffer, OFFT2NUM(from), SIZET2NUM(length), SIZET2NUM(offset)
563 };
564
565 return rb_check_funcall(scheduler, id_io_pwrite, 5, arguments);
566}
567
568VALUE
569rb_fiber_scheduler_io_read_memory(VALUE scheduler, VALUE io, void *base, size_t size, size_t length)
570{
571 VALUE buffer = rb_io_buffer_new(base, size, RB_IO_BUFFER_LOCKED);
572
573 VALUE result = rb_fiber_scheduler_io_read(scheduler, io, buffer, length, 0);
574
575 rb_io_buffer_unlock(buffer);
576 rb_io_buffer_free(buffer);
577
578 return result;
579}
580
581VALUE
582rb_fiber_scheduler_io_write_memory(VALUE scheduler, VALUE io, const void *base, size_t size, size_t length)
583{
584 VALUE buffer = rb_io_buffer_new((void*)base, size, RB_IO_BUFFER_LOCKED|RB_IO_BUFFER_READONLY);
585
586 VALUE result = rb_fiber_scheduler_io_write(scheduler, io, buffer, length, 0);
587
588 rb_io_buffer_unlock(buffer);
589 rb_io_buffer_free(buffer);
590
591 return result;
592}
593
594VALUE
596{
597 VALUE arguments[] = {io};
598
599 return rb_check_funcall(scheduler, id_io_close, 1, arguments);
600}
601
602/*
603 * Document-method: Fiber::Scheduler#address_resolve
604 * call-seq: address_resolve(hostname) -> array_of_strings or nil
605 *
606 * Invoked by any method that performs a non-reverse DNS lookup. The most
607 * notable method is Addrinfo.getaddrinfo, but there are many other.
608 *
609 * The method is expected to return an array of strings corresponding to ip
610 * addresses the +hostname+ is resolved to, or +nil+ if it can not be resolved.
611 *
612 * Fairly exhaustive list of all possible call-sites:
613 *
614 * - Addrinfo.getaddrinfo
615 * - Addrinfo.tcp
616 * - Addrinfo.udp
617 * - Addrinfo.ip
618 * - Addrinfo.new
619 * - Addrinfo.marshal_load
620 * - SOCKSSocket.new
621 * - TCPServer.new
622 * - TCPSocket.new
623 * - IPSocket.getaddress
624 * - TCPSocket.gethostbyname
625 * - UDPSocket#connect
626 * - UDPSocket#bind
627 * - UDPSocket#send
628 * - Socket.getaddrinfo
629 * - Socket.gethostbyname
630 * - Socket.pack_sockaddr_in
631 * - Socket.sockaddr_in
632 * - Socket.unpack_sockaddr_in
633 */
634VALUE
636{
637 VALUE arguments[] = {
638 hostname
639 };
640
641 return rb_check_funcall(scheduler, id_address_resolve, 1, arguments);
642}
643
644/*
645 * Document-method: Fiber::Scheduler#fiber
646 * call-seq: fiber(&block)
647 *
648 * Implementation of the Fiber.schedule. The method is <em>expected</em> to immediately
649 * run the given block of code in a separate non-blocking fiber, and to return that Fiber.
650 *
651 * Minimal suggested implementation is:
652 *
653 * def fiber(&block)
654 * fiber = Fiber.new(blocking: false, &block)
655 * fiber.resume
656 * fiber
657 * end
658 */
659VALUE
660rb_fiber_scheduler_fiber(VALUE scheduler, int argc, VALUE *argv, int kw_splat)
661{
662 return rb_funcall_passing_block_kw(scheduler, id_fiber_schedule, argc, argv, kw_splat);
663}
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:920
#define SIZET2NUM
Old name of RB_SIZE2NUM.
Definition size_t.h:62
#define Qnil
Old name of RUBY_Qnil.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition error.c:3148
VALUE rb_eArgError
ArgumentError exception.
Definition error.c:1092
VALUE rb_funcall_passing_block_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
Identical to rb_funcallv_passing_block(), except you can specify how to handle the last element of th...
Definition vm_eval.c:1173
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:2823
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
Definition vm_eval.c:665
VALUE rb_io_timeout(VALUE io)
Get the timeout associated with the specified io object.
Definition io.c:843
@ RUBY_IO_READABLE
IO::READABLE
Definition io.h:82
@ RUBY_IO_WRITABLE
IO::WRITABLE
Definition io.h:83
#define RB_UINT2NUM
Just another name of rb_uint2num_inline.
Definition int.h:39
#define RB_INT2NUM
Just another name of rb_int2num_inline.
Definition int.h:37
#define OFFT2NUM
Converts a C's off_t into an instance of rb_cInteger.
Definition off_t.h:33
#define PIDT2NUM
Converts a C's pid_t into an instance of rb_cInteger.
Definition pid_t.h:28
Scheduler APIs.
VALUE rb_fiber_scheduler_current(void)
Identical to rb_fiber_scheduler_get(), except it also returns RUBY_Qnil in case of a blocking fiber.
Definition scheduler.c:203
VALUE rb_fiber_scheduler_make_timeout(struct timeval *timeout)
Converts the passed timeout to an expression that rb_fiber_scheduler_block() etc.
Definition scheduler.c:246
VALUE rb_fiber_scheduler_io_wait_readable(VALUE scheduler, VALUE io)
Non-blocking wait until the passed IO is ready for reading.
Definition scheduler.c:419
VALUE rb_fiber_scheduler_io_pwrite(VALUE scheduler, VALUE io, rb_off_t from, VALUE buffer, size_t length, size_t offset)
Non-blocking write to the passed IO at the specified offset.
Definition scheduler.c:559
VALUE rb_fiber_scheduler_kernel_sleepv(VALUE scheduler, int argc, VALUE *argv)
Identical to rb_fiber_scheduler_kernel_sleep(), except it can pass multiple arguments.
Definition scheduler.c:273
VALUE rb_fiber_scheduler_io_wait(VALUE scheduler, VALUE io, VALUE events, VALUE timeout)
Non-blocking version of rb_io_wait().
Definition scheduler.c:413
VALUE rb_fiber_scheduler_io_select(VALUE scheduler, VALUE readables, VALUE writables, VALUE exceptables, VALUE timeout)
Non-blocking version of IO.select.
Definition scheduler.c:440
VALUE rb_fiber_scheduler_io_read(VALUE scheduler, VALUE io, VALUE buffer, size_t length, size_t offset)
Non-blocking read from the passed IO.
Definition scheduler.c:487
VALUE rb_fiber_scheduler_io_selectv(VALUE scheduler, int argc, VALUE *argv)
Non-blocking version of IO.select, argv variant.
Definition scheduler.c:449
VALUE rb_fiber_scheduler_process_wait(VALUE scheduler, rb_pid_t pid, int flags)
Non-blocking waitpid.
Definition scheduler.c:343
VALUE rb_fiber_scheduler_block(VALUE scheduler, VALUE blocker, VALUE timeout)
Non-blocking wait for the passed "blocker", which is for instance Thread.join or Mutex....
Definition scheduler.c:367
VALUE rb_fiber_scheduler_io_pread(VALUE scheduler, VALUE io, rb_off_t from, VALUE buffer, size_t length, size_t offset)
Non-blocking read from the passed IO at the specified offset.
Definition scheduler.c:505
VALUE rb_fiber_scheduler_io_write(VALUE scheduler, VALUE io, VALUE buffer, size_t length, size_t offset)
Non-blocking write to the passed IO.
Definition scheduler.c:542
VALUE rb_fiber_scheduler_close(VALUE scheduler)
Closes the passed scheduler object.
Definition scheduler.c:224
VALUE rb_fiber_scheduler_current_for_thread(VALUE thread)
Identical to rb_fiber_scheduler_current(), except it queries for that of the passed thread instead of...
Definition scheduler.c:208
VALUE rb_fiber_scheduler_kernel_sleep(VALUE scheduler, VALUE duration)
Non-blocking sleep.
Definition scheduler.c:267
VALUE rb_fiber_scheduler_address_resolve(VALUE scheduler, VALUE hostname)
Non-blocking DNS lookup.
Definition scheduler.c:635
VALUE rb_fiber_scheduler_set(VALUE scheduler)
Destructively assigns the passed scheduler to that of the current thread that is calling this functio...
Definition scheduler.c:165
VALUE rb_fiber_scheduler_io_wait_writable(VALUE scheduler, VALUE io)
Non-blocking wait until the passed IO is ready for writing.
Definition scheduler.c:425
VALUE rb_fiber_scheduler_io_write_memory(VALUE scheduler, VALUE io, const void *buffer, size_t size, size_t length)
Non-blocking write to the passed IO using a native buffer.
Definition scheduler.c:582
VALUE rb_fiber_scheduler_io_read_memory(VALUE scheduler, VALUE io, void *buffer, size_t size, size_t length)
Non-blocking read from the passed IO using a native buffer.
Definition scheduler.c:569
VALUE rb_fiber_scheduler_io_close(VALUE scheduler, VALUE io)
Non-blocking close the given IO.
Definition scheduler.c:595
VALUE rb_fiber_scheduler_get(void)
Queries the current scheduler of the current thread that is calling this function.
Definition scheduler.c:134
VALUE rb_fiber_scheduler_unblock(VALUE scheduler, VALUE blocker, VALUE fiber)
Wakes up a fiber previously blocked using rb_fiber_scheduler_block().
Definition scheduler.c:386
VALUE rb_fiber_scheduler_fiber(VALUE scheduler, int argc, VALUE *argv, int kw_splat)
Create and schedule a non-blocking fiber.
Definition scheduler.c:660
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52