Class: IO::Event::Selector::KQueue
- Inherits:
-
Object
- Object
- IO::Event::Selector::KQueue
- Defined in:
- ext/io/event/selector/kqueue.c
Instance Method Summary collapse
- #close ⇒ Object
- #initialize(loop) ⇒ Object constructor
- #io_read(*args) ⇒ Object
- #io_wait(fiber, io, events) ⇒ Object
- #io_write(*args) ⇒ Object
- #loop ⇒ Object
- #process_wait(fiber, _pid, _flags) ⇒ Object
- #push(fiber) ⇒ Object
- #raise(*args) ⇒ Object
- #ready? ⇒ Boolean
- #resume(*args) ⇒ Object
- #select(duration) ⇒ Object
- #transfer ⇒ Object
- #wakeup ⇒ Object
- #yield ⇒ Object
Constructor Details
#initialize(loop) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'ext/io/event/selector/kqueue.c', line 337
VALUE IO_Event_Selector_KQueue_initialize(VALUE self, VALUE loop) {
struct IO_Event_Selector_KQueue *selector = NULL;
TypedData_Get_Struct(self, struct IO_Event_Selector_KQueue, &IO_Event_Selector_KQueue_Type, selector);
IO_Event_Selector_initialize(&selector->backend, loop);
int result = kqueue();
if (result == -1) {
rb_sys_fail("IO_Event_Selector_KQueue_initialize:kqueue");
} else {
// Make sure the descriptor is closed on exec.
ioctl(result, FIOCLEX);
selector->descriptor = result;
rb_update_max_fd(selector->descriptor);
}
#ifdef IO_EVENT_SELECTOR_KQUEUE_USE_INTERRUPT
IO_Event_Interrupt_open(&selector->interrupt);
IO_Event_Interrupt_add(&selector->interrupt, selector);
#endif
return self;
}
|
Instance Method Details
#close ⇒ Object
370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'ext/io/event/selector/kqueue.c', line 370
VALUE IO_Event_Selector_KQueue_close(VALUE self) {
struct IO_Event_Selector_KQueue *selector = NULL;
TypedData_Get_Struct(self, struct IO_Event_Selector_KQueue, &IO_Event_Selector_KQueue_Type, selector);
close_internal(selector);
#ifdef IO_EVENT_SELECTOR_KQUEUE_USE_INTERRUPT
IO_Event_Interrupt_close(&selector->interrupt);
#endif
return Qnil;
}
|
#io_read(*args) ⇒ Object
661 662 663 664 665 666 667 668 669 670 671 672 |
# File 'ext/io/event/selector/kqueue.c', line 661
static VALUE IO_Event_Selector_KQueue_io_read_compatible(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 4, 5);
VALUE _offset = SIZET2NUM(0);
if (argc == 5) {
_offset = argv[4];
}
return IO_Event_Selector_KQueue_io_read(self, argv[0], argv[1], argv[2], argv[3], _offset);
}
|
#io_wait(fiber, io, events) ⇒ Object
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
# File 'ext/io/event/selector/kqueue.c', line 543
VALUE IO_Event_Selector_KQueue_io_wait(VALUE self, VALUE fiber, VALUE io, VALUE events) {
struct IO_Event_Selector_KQueue *selector = NULL;
TypedData_Get_Struct(self, struct IO_Event_Selector_KQueue, &IO_Event_Selector_KQueue_Type, selector);
int descriptor = IO_Event_Selector_io_descriptor(io);
struct IO_Event_Selector_KQueue_Waiting waiting = {
.list = {.type = &IO_Event_Selector_KQueue_io_wait_list_type},
.fiber = fiber,
.events = RB_NUM2INT(events),
};
int result = IO_Event_Selector_KQueue_Waiting_register(selector, descriptor, &waiting);
if (result == -1) {
rb_sys_fail("IO_Event_Selector_KQueue_io_wait:IO_Event_Selector_KQueue_Waiting_register");
}
struct io_wait_arguments io_wait_arguments = {
.selector = selector,
.waiting = &waiting,
};
if (DEBUG_IO_WAIT) fprintf(stderr, "IO_Event_Selector_KQueue_io_wait descriptor=%d\n", descriptor);
return rb_ensure(io_wait_transfer, (VALUE)&io_wait_arguments, io_wait_ensure, (VALUE)&io_wait_arguments);
}
|
#io_write(*args) ⇒ Object
767 768 769 770 771 772 773 774 775 776 777 778 |
# File 'ext/io/event/selector/kqueue.c', line 767
static VALUE IO_Event_Selector_KQueue_io_write_compatible(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 4, 5);
VALUE _offset = SIZET2NUM(0);
if (argc == 5) {
_offset = argv[4];
}
return IO_Event_Selector_KQueue_io_write(self, argv[0], argv[1], argv[2], argv[3], _offset);
}
|
#loop ⇒ Object
363 364 365 366 367 368 |
# File 'ext/io/event/selector/kqueue.c', line 363
VALUE IO_Event_Selector_KQueue_loop(VALUE self) {
struct IO_Event_Selector_KQueue *selector = NULL;
TypedData_Get_Struct(self, struct IO_Event_Selector_KQueue, &IO_Event_Selector_KQueue_Type, selector);
return selector->backend.loop;
}
|
#process_wait(fiber, _pid, _flags) ⇒ Object
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
# File 'ext/io/event/selector/kqueue.c', line 481
VALUE IO_Event_Selector_KQueue_process_wait(VALUE self, VALUE fiber, VALUE _pid, VALUE _flags) {
struct IO_Event_Selector_KQueue *selector = NULL;
TypedData_Get_Struct(self, struct IO_Event_Selector_KQueue, &IO_Event_Selector_KQueue_Type, selector);
pid_t pid = NUM2PIDT(_pid);
struct IO_Event_Selector_KQueue_Waiting waiting = {
.list = {.type = &IO_Event_Selector_KQueue_process_wait_list_type},
.fiber = fiber,
.events = IO_EVENT_EXIT,
};
struct process_wait_arguments process_wait_arguments = {
.selector = selector,
.waiting = &waiting,
.pid = pid,
};
int result = IO_Event_Selector_KQueue_Waiting_register(selector, pid, &waiting);
if (result == -1) {
// OpenBSD/NetBSD return ESRCH when attempting to register an EVFILT_PROC event for a zombie process.
if (errno == ESRCH) {
process_prewait(pid);
return IO_Event_Selector_process_status_wait(pid);
}
rb_sys_fail("IO_Event_Selector_KQueue_process_wait:IO_Event_Selector_KQueue_Waiting_register");
}
return rb_ensure(process_wait_transfer, (VALUE)&process_wait_arguments, process_wait_ensure, (VALUE)&process_wait_arguments);
}
|
#push(fiber) ⇒ Object
407 408 409 410 411 412 413 414 415 |
# File 'ext/io/event/selector/kqueue.c', line 407
VALUE IO_Event_Selector_KQueue_push(VALUE self, VALUE fiber)
{
struct IO_Event_Selector_KQueue *selector = NULL;
TypedData_Get_Struct(self, struct IO_Event_Selector_KQueue, &IO_Event_Selector_KQueue_Type, selector);
IO_Event_Selector_queue_push(&selector->backend, fiber);
return Qnil;
}
|
#raise(*args) ⇒ Object
417 418 419 420 421 422 423 |
# File 'ext/io/event/selector/kqueue.c', line 417
VALUE IO_Event_Selector_KQueue_raise(int argc, VALUE *argv, VALUE self)
{
struct IO_Event_Selector_KQueue *selector = NULL;
TypedData_Get_Struct(self, struct IO_Event_Selector_KQueue, &IO_Event_Selector_KQueue_Type, selector);
return IO_Event_Selector_raise(&selector->backend, argc, argv);
}
|
#ready? ⇒ Boolean
425 426 427 428 429 430 |
# File 'ext/io/event/selector/kqueue.c', line 425
VALUE IO_Event_Selector_KQueue_ready_p(VALUE self) {
struct IO_Event_Selector_KQueue *selector = NULL;
TypedData_Get_Struct(self, struct IO_Event_Selector_KQueue, &IO_Event_Selector_KQueue_Type, selector);
return selector->backend.ready ? Qtrue : Qfalse;
}
|
#resume(*args) ⇒ Object
391 392 393 394 395 396 397 |
# File 'ext/io/event/selector/kqueue.c', line 391
VALUE IO_Event_Selector_KQueue_resume(int argc, VALUE *argv, VALUE self)
{
struct IO_Event_Selector_KQueue *selector = NULL;
TypedData_Get_Struct(self, struct IO_Event_Selector_KQueue, &IO_Event_Selector_KQueue_Type, selector);
return IO_Event_Selector_resume(&selector->backend, argc, argv);
}
|
#select(duration) ⇒ Object
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 |
# File 'ext/io/event/selector/kqueue.c', line 907
VALUE IO_Event_Selector_KQueue_select(VALUE self, VALUE duration) {
struct IO_Event_Selector_KQueue *selector = NULL;
TypedData_Get_Struct(self, struct IO_Event_Selector_KQueue, &IO_Event_Selector_KQueue_Type, selector);
int ready = IO_Event_Selector_queue_flush(&selector->backend);
struct select_arguments arguments = {
.selector = selector,
.count = KQUEUE_MAX_EVENTS,
.storage = {
.tv_sec = 0,
.tv_nsec = 0
}
};
arguments.timeout = &arguments.storage;
// We break this implementation into two parts.
// (1) count = kevent(..., timeout = 0)
// (2) without gvl: kevent(..., timeout = 0) if count == 0 and timeout != 0
// This allows us to avoid releasing and reacquiring the GVL.
// Non-comprehensive testing shows this gives a 1.5x speedup.
// First do the syscall with no timeout to get any immediately available events:
if (DEBUG) fprintf(stderr, "\r\nselect_internal_with_gvl timeout=" PRINTF_TIMESPEC "\r\n", PRINTF_TIMESPEC_ARGS(arguments.storage));
select_internal_with_gvl(&arguments);
if (DEBUG) fprintf(stderr, "\r\nselect_internal_with_gvl done\r\n");
// If we:
// 1. Didn't process any ready fibers, and
// 2. Didn't process any events from non-blocking select (above), and
// 3. There are no items in the ready list,
// then we can perform a blocking select.
if (!ready && !arguments.count && !selector->backend.ready) {
arguments.timeout = make_timeout(duration, &arguments.storage);
if (!timeout_nonblocking(arguments.timeout)) {
arguments.count = KQUEUE_MAX_EVENTS;
if (DEBUG) fprintf(stderr, "IO_Event_Selector_KQueue_select timeout=" PRINTF_TIMESPEC "\n", PRINTF_TIMESPEC_ARGS(arguments.storage));
select_internal_without_gvl(&arguments);
}
}
for (int i = 0; i < arguments.count; i += 1) {
if (arguments.events[i].udata) {
struct IO_Event_Selector_KQueue_Descriptor *kqueue_descriptor = arguments.events[i].udata;
kqueue_descriptor->ready_events |= events_from_kevent_filter(arguments.events[i].filter);
}
}
for (int i = 0; i < arguments.count; i += 1) {
if (arguments.events[i].udata) {
struct IO_Event_Selector_KQueue_Descriptor *kqueue_descriptor = arguments.events[i].udata;
IO_Event_Selector_KQueue_handle(selector, arguments.events[i].ident, kqueue_descriptor);
} else {
#ifdef IO_EVENT_SELECTOR_KQUEUE_USE_INTERRUPT
IO_Event_Interrupt_clear(&selector->interrupt);
#endif
}
}
return RB_INT2NUM(arguments.count);
}
|
#transfer ⇒ Object
383 384 385 386 387 388 389 |
# File 'ext/io/event/selector/kqueue.c', line 383
VALUE IO_Event_Selector_KQueue_transfer(VALUE self)
{
struct IO_Event_Selector_KQueue *selector = NULL;
TypedData_Get_Struct(self, struct IO_Event_Selector_KQueue, &IO_Event_Selector_KQueue_Type, selector);
return IO_Event_Selector_fiber_transfer(selector->backend.loop, 0, NULL);
}
|
#wakeup ⇒ Object
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 |
# File 'ext/io/event/selector/kqueue.c', line 972
VALUE IO_Event_Selector_KQueue_wakeup(VALUE self) {
struct IO_Event_Selector_KQueue *selector = NULL;
TypedData_Get_Struct(self, struct IO_Event_Selector_KQueue, &IO_Event_Selector_KQueue_Type, selector);
if (selector->blocked) {
#ifdef IO_EVENT_SELECTOR_KQUEUE_USE_INTERRUPT
IO_Event_Interrupt_signal(&selector->interrupt);
#else
struct kevent trigger = {0};
trigger.filter = EVFILT_USER;
trigger.flags = EV_ADD | EV_CLEAR;
int result = kevent(selector->descriptor, &trigger, 1, NULL, 0, NULL);
if (result == -1) {
rb_sys_fail("IO_Event_Selector_KQueue_wakeup:kevent");
}
// FreeBSD apparently only works if the NOTE_TRIGGER is done as a separate call.
trigger.flags = 0;
trigger.fflags = NOTE_TRIGGER;
result = kevent(selector->descriptor, &trigger, 1, NULL, 0, NULL);
if (result == -1) {
rb_sys_fail("IO_Event_Selector_KQueue_wakeup:kevent");
}
#endif
return Qtrue;
}
return Qfalse;
}
|
#yield ⇒ Object
399 400 401 402 403 404 405 |
# File 'ext/io/event/selector/kqueue.c', line 399
VALUE IO_Event_Selector_KQueue_yield(VALUE self)
{
struct IO_Event_Selector_KQueue *selector = NULL;
TypedData_Get_Struct(self, struct IO_Event_Selector_KQueue, &IO_Event_Selector_KQueue_Type, selector);
return IO_Event_Selector_yield(&selector->backend);
}
|