12 #include "ruby/internal/config.h"
21 #ifdef NEED_MADVICE_PROTOTYPE_USING_CADDR_T
22 #include <sys/types.h>
23 extern int madvise(caddr_t,
size_t,
int);
28 #include "eval_intern.h"
30 #include "internal/cont.h"
31 #include "internal/thread.h"
32 #include "internal/error.h"
33 #include "internal/gc.h"
34 #include "internal/proc.h"
35 #include "internal/sanitizers.h"
36 #include "internal/warnings.h"
43 #include "ractor_core.h"
45 static const int DEBUG = 0;
47 #define RB_PAGE_SIZE (pagesize)
48 #define RB_PAGE_MASK (~(RB_PAGE_SIZE - 1))
52 static VALUE rb_cContinuation;
53 static VALUE rb_cFiber;
54 static VALUE rb_eFiberError;
55 #ifdef RB_EXPERIMENTAL_FIBER_POOL
56 static VALUE rb_cFiberPool;
59 #define CAPTURE_JUST_VALID_VM_STACK 1
62 #ifdef COROUTINE_LIMITED_ADDRESS_SPACE
63 #define FIBER_POOL_ALLOCATION_FREE
64 #define FIBER_POOL_INITIAL_SIZE 8
65 #define FIBER_POOL_ALLOCATION_MAXIMUM_SIZE 32
67 #define FIBER_POOL_INITIAL_SIZE 32
68 #define FIBER_POOL_ALLOCATION_MAXIMUM_SIZE 1024
70 #ifdef RB_EXPERIMENTAL_FIBER_POOL
71 #define FIBER_POOL_ALLOCATION_FREE
75 CONTINUATION_CONTEXT = 0,
81 #ifdef CAPTURE_JUST_VALID_VM_STACK
118 #ifdef FIBER_POOL_ALLOCATION_FREE
161 #ifdef FIBER_POOL_ALLOCATION_FREE
169 #ifdef FIBER_POOL_ALLOCATION_FREE
190 size_t initial_count;
201 size_t vm_stack_size;
214 enum context_type type;
252 #define FIBER_CREATED_P(fiber) ((fiber)->status == FIBER_CREATED)
253 #define FIBER_RESUMED_P(fiber) ((fiber)->status == FIBER_RESUMED)
254 #define FIBER_SUSPENDED_P(fiber) ((fiber)->status == FIBER_SUSPENDED)
255 #define FIBER_TERMINATED_P(fiber) ((fiber)->status == FIBER_TERMINATED)
256 #define FIBER_RUNNABLE_P(fiber) (FIBER_CREATED_P(fiber) || FIBER_SUSPENDED_P(fiber))
264 BITFIELD(
enum fiber_status, status, 2);
266 unsigned int yielding : 1;
267 unsigned int blocking : 1;
269 unsigned int killed : 1;
275 static struct fiber_pool shared_fiber_pool = {NULL, NULL, 0, 0, 0, 0};
278 rb_free_shared_fiber_pool(
void)
280 xfree(shared_fiber_pool.allocations);
283 static ID fiber_initialize_keywords[3] = {0};
290 #if defined(MAP_STACK) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
291 #define FIBER_STACK_FLAGS (MAP_PRIVATE | MAP_ANON | MAP_STACK)
293 #define FIBER_STACK_FLAGS (MAP_PRIVATE | MAP_ANON)
296 #define ERRNOMSG strerror(errno)
300 fiber_pool_vacancy_pointer(
void * base,
size_t size)
302 STACK_GROW_DIR_DETECTION;
305 (
char*)base + STACK_DIR_UPPER(0, size - RB_PAGE_SIZE)
309 #if defined(COROUTINE_SANITIZE_ADDRESS)
314 STACK_GROW_DIR_DETECTION;
316 return (
char*)stack->base + STACK_DIR_UPPER(RB_PAGE_SIZE, 0);
323 return stack->size - RB_PAGE_SIZE;
331 STACK_GROW_DIR_DETECTION;
333 stack->current = (
char*)stack->base + STACK_DIR_UPPER(0, stack->size);
334 stack->available = stack->size;
341 STACK_GROW_DIR_DETECTION;
343 VM_ASSERT(stack->current);
345 return STACK_DIR_UPPER(stack->current, (
char*)stack->current - stack->available);
353 STACK_GROW_DIR_DETECTION;
355 if (DEBUG) fprintf(stderr,
"fiber_pool_stack_alloca(%p): %"PRIuSIZE
"/%"PRIuSIZE
"\n", (
void*)stack, offset, stack->available);
356 VM_ASSERT(stack->available >= offset);
359 void * pointer = STACK_DIR_UPPER(stack->current, (
char*)stack->current - offset);
362 stack->current = STACK_DIR_UPPER((
char*)stack->current + offset, (
char*)stack->current - offset);
363 stack->available -= offset;
372 fiber_pool_stack_reset(&vacancy->stack);
375 fiber_pool_stack_alloca(&vacancy->stack, RB_PAGE_SIZE);
381 vacancy->next = head;
383 #ifdef FIBER_POOL_ALLOCATION_FREE
385 head->previous = vacancy;
386 vacancy->previous = NULL;
393 #ifdef FIBER_POOL_ALLOCATION_FREE
398 vacancy->next->previous = vacancy->previous;
401 if (vacancy->previous) {
402 vacancy->previous->next = vacancy->next;
406 vacancy->stack.pool->vacancies = vacancy->next;
411 fiber_pool_vacancy_pop(
struct fiber_pool * pool)
416 fiber_pool_vacancy_remove(vacancy);
423 fiber_pool_vacancy_pop(
struct fiber_pool * pool)
428 pool->vacancies = vacancy->next;
443 vacancy->stack.base = base;
444 vacancy->stack.size = size;
446 fiber_pool_vacancy_reset(vacancy);
450 return fiber_pool_vacancy_push(vacancy, vacancies);
458 fiber_pool_allocate_memory(
size_t * count,
size_t stride)
468 void * base = VirtualAlloc(0, (*count)*stride, MEM_COMMIT, PAGE_READWRITE);
471 *count = (*count) >> 1;
478 void * base = mmap(NULL, (*count)*stride, PROT_READ | PROT_WRITE, FIBER_STACK_FLAGS, -1, 0);
480 if (base == MAP_FAILED) {
482 *count = (*count) >> 1;
485 #if defined(MADV_FREE_REUSE)
489 while (madvise(base, (*count)*stride, MADV_FREE_REUSE) == -1 &&
errno == EAGAIN);
506 STACK_GROW_DIR_DETECTION;
509 size_t stride = size + RB_PAGE_SIZE;
512 void * base = fiber_pool_allocate_memory(&count, stride);
515 rb_raise(rb_eFiberError,
"can't alloc machine stack to fiber (%"PRIuSIZE
" x %"PRIuSIZE
" bytes): %s", count, size, ERRNOMSG);
522 allocation->base = base;
523 allocation->size = size;
524 allocation->stride = stride;
525 allocation->count = count;
526 #ifdef FIBER_POOL_ALLOCATION_FREE
527 allocation->used = 0;
532 fprintf(stderr,
"fiber_pool_expand(%"PRIuSIZE
"): %p, %"PRIuSIZE
"/%"PRIuSIZE
" x [%"PRIuSIZE
":%"PRIuSIZE
"]\n",
537 for (
size_t i = 0; i < count; i += 1) {
538 void * base = (
char*)allocation->base + (stride * i);
539 void * page = (
char*)base + STACK_DIR_UPPER(size, 0);
544 if (!VirtualProtect(page, RB_PAGE_SIZE, PAGE_READWRITE | PAGE_GUARD, &old_protect)) {
545 VirtualFree(allocation->base, 0, MEM_RELEASE);
546 rb_raise(rb_eFiberError,
"can't set a guard page: %s", ERRNOMSG);
549 if (mprotect(page, RB_PAGE_SIZE, PROT_NONE) < 0) {
550 munmap(allocation->base, count*stride);
551 rb_raise(rb_eFiberError,
"can't set a guard page: %s", ERRNOMSG);
555 vacancies = fiber_pool_vacancy_initialize(
557 (
char*)base + STACK_DIR_UPPER(0, RB_PAGE_SIZE),
561 #ifdef FIBER_POOL_ALLOCATION_FREE
562 vacancies->stack.allocation = allocation;
569 #ifdef FIBER_POOL_ALLOCATION_FREE
570 if (allocation->next) {
571 allocation->next->previous = allocation;
574 allocation->previous = NULL;
587 fiber_pool_initialize(
struct fiber_pool *
fiber_pool,
size_t size,
size_t count,
size_t vm_stack_size)
589 VM_ASSERT(vm_stack_size < size);
593 fiber_pool->size = ((size / RB_PAGE_SIZE) + 1) * RB_PAGE_SIZE;
604 #ifdef FIBER_POOL_ALLOCATION_FREE
609 STACK_GROW_DIR_DETECTION;
611 VM_ASSERT(allocation->used == 0);
613 if (DEBUG) fprintf(stderr,
"fiber_pool_allocation_free: %p base=%p count=%"PRIuSIZE
"\n", (
void*)allocation, allocation->base, allocation->count);
616 for (i = 0; i < allocation->count; i += 1) {
617 void * base = (
char*)allocation->base + (allocation->stride * i) + STACK_DIR_UPPER(0, RB_PAGE_SIZE);
619 struct fiber_pool_vacancy * vacancy = fiber_pool_vacancy_pointer(base, allocation->size);
622 fiber_pool_vacancy_remove(vacancy);
626 VirtualFree(allocation->base, 0, MEM_RELEASE);
628 munmap(allocation->base, allocation->stride * allocation->count);
631 if (allocation->previous) {
632 allocation->previous->next = allocation->next;
636 allocation->pool->allocations = allocation->next;
639 if (allocation->next) {
640 allocation->next->previous = allocation->previous;
643 allocation->pool->count -= allocation->count;
655 if (DEBUG) fprintf(stderr,
"fiber_pool_stack_acquire: %p used=%"PRIuSIZE
"\n", (
void*)
fiber_pool->vacancies,
fiber_pool->used);
658 const size_t maximum = FIBER_POOL_ALLOCATION_MAXIMUM_SIZE;
659 const size_t minimum =
fiber_pool->initial_count;
662 if (count > maximum) count = maximum;
663 if (count < minimum) count = minimum;
674 VM_ASSERT(vacancy->stack.base);
676 #if defined(COROUTINE_SANITIZE_ADDRESS)
677 __asan_unpoison_memory_region(fiber_pool_stack_poison_base(&vacancy->stack), fiber_pool_stack_poison_size(&vacancy->stack));
683 #ifdef FIBER_POOL_ALLOCATION_FREE
684 vacancy->stack.allocation->used += 1;
687 fiber_pool_stack_reset(&vacancy->stack);
689 return vacancy->stack;
697 void * base = fiber_pool_stack_base(stack);
698 size_t size = stack->available;
701 VM_ASSERT(size <= (stack->size - RB_PAGE_SIZE));
703 int advice = stack->pool->free_stacks >> 1;
705 if (DEBUG) fprintf(stderr,
"fiber_pool_stack_free: %p+%"PRIuSIZE
" [base=%p, size=%"PRIuSIZE
"] advice=%d\n", base, size, stack->base, stack->size, advice);
718 #elif VM_CHECK_MODE > 0 && defined(MADV_DONTNEED)
719 if (!advice) advice = MADV_DONTNEED;
721 madvise(base, size, advice);
722 #elif defined(MADV_FREE_REUSABLE)
723 if (!advice) advice = MADV_FREE_REUSABLE;
729 while (madvise(base, size, advice) == -1 &&
errno == EAGAIN);
730 #elif defined(MADV_FREE)
731 if (!advice) advice = MADV_FREE;
733 madvise(base, size, advice);
734 #elif defined(MADV_DONTNEED)
735 if (!advice) advice = MADV_DONTNEED;
737 madvise(base, size, advice);
738 #elif defined(POSIX_MADV_DONTNEED)
739 if (!advice) advice = POSIX_MADV_DONTNEED;
741 posix_madvise(base, size, advice);
742 #elif defined(_WIN32)
743 VirtualAlloc(base, size, MEM_RESET, PAGE_READWRITE);
748 #if defined(COROUTINE_SANITIZE_ADDRESS)
749 __asan_poison_memory_region(fiber_pool_stack_poison_base(stack), fiber_pool_stack_poison_size(stack));
758 struct fiber_pool_vacancy * vacancy = fiber_pool_vacancy_pointer(stack->base, stack->size);
760 if (DEBUG) fprintf(stderr,
"fiber_pool_stack_release: %p used=%"PRIuSIZE
"\n", stack->base, stack->pool->used);
763 vacancy->stack = *stack;
767 fiber_pool_vacancy_reset(vacancy);
770 pool->vacancies = fiber_pool_vacancy_push(vacancy, pool->vacancies);
773 #ifdef FIBER_POOL_ALLOCATION_FREE
776 allocation->used -= 1;
779 if (allocation->used == 0) {
780 fiber_pool_allocation_free(allocation);
782 else if (stack->pool->free_stacks) {
783 fiber_pool_stack_free(&vacancy->stack);
788 if (stack->pool->free_stacks) {
789 fiber_pool_stack_free(&vacancy->stack);
798 #ifdef RUBY_ASAN_ENABLED
799 ec->machine.asan_fake_stack_handle = asan_get_thread_fake_stack_handle();
801 rb_ractor_set_current_ec(th->ractor, th->ec = ec);
808 if (th->vm->ractor.main_thread == th &&
809 rb_signal_buff_size() > 0) {
810 RUBY_VM_SET_TRAP_INTERRUPT(ec);
813 VM_ASSERT(ec->fiber_ptr->cont.self == 0 || ec->vm_stack != NULL);
819 ec_switch(th, fiber);
820 VM_ASSERT(th->ec->fiber_ptr == fiber);
828 #if defined(COROUTINE_SANITIZE_ADDRESS)
838 __sanitizer_finish_switch_fiber(to->fake_stack, (
const void**)&from->stack_base, &from->stack_size);
841 rb_thread_t *thread = fiber->cont.saved_ec.thread_ptr;
843 #ifdef COROUTINE_PTHREAD_CONTEXT
844 ruby_thread_set_native(thread);
847 fiber_restore_thread(thread, fiber);
849 rb_fiber_start(fiber);
851 #ifndef COROUTINE_PTHREAD_CONTEXT
852 VM_UNREACHABLE(fiber_entry);
858 fiber_initialize_coroutine(
rb_fiber_t *fiber,
size_t * vm_stack_size)
862 void * vm_stack = NULL;
866 fiber->stack = fiber_pool_stack_acquire(
fiber_pool);
867 vm_stack = fiber_pool_stack_alloca(&fiber->stack,
fiber_pool->vm_stack_size);
870 coroutine_initialize(&fiber->context, fiber_entry, fiber_pool_stack_base(&fiber->stack), fiber->stack.available);
873 sec->machine.stack_start = fiber->stack.current;
874 sec->machine.stack_maxsize = fiber->stack.available;
876 fiber->context.argument = (
void*)fiber;
888 if (DEBUG) fprintf(stderr,
"fiber_stack_release: %p, stack.base=%p\n", (
void*)fiber, fiber->stack.base);
891 if (fiber->stack.base) {
892 fiber_pool_stack_release(&fiber->stack);
893 fiber->stack.base = NULL;
897 rb_ec_clear_vm_stack(ec);
901 fiber_status_name(
enum fiber_status s)
904 case FIBER_CREATED:
return "created";
905 case FIBER_RESUMED:
return "resumed";
906 case FIBER_SUSPENDED:
return "suspended";
907 case FIBER_TERMINATED:
return "terminated";
909 VM_UNREACHABLE(fiber_status_name);
916 #if VM_CHECK_MODE > 0
917 VM_ASSERT(fiber->cont.saved_ec.fiber_ptr == fiber);
919 switch (fiber->status) {
921 VM_ASSERT(fiber->cont.saved_ec.vm_stack != NULL);
923 case FIBER_SUSPENDED:
924 VM_ASSERT(fiber->cont.saved_ec.vm_stack != NULL);
927 case FIBER_TERMINATED:
931 VM_UNREACHABLE(fiber_verify);
937 fiber_status_set(
rb_fiber_t *fiber,
enum fiber_status s)
940 VM_ASSERT(!FIBER_TERMINATED_P(fiber));
941 VM_ASSERT(fiber->status != s);
962 if (!fiber)
rb_raise(rb_eFiberError,
"uninitialized fiber");
967 NOINLINE(
static VALUE cont_capture(
volatile int *
volatile stat));
969 #define THREAD_MUST_BE_RUNNING(th) do { \
970 if (!(th)->ec->tag) rb_raise(rb_eThreadError, "not running thread"); \
976 return fiber->cont.saved_ec.thread_ptr;
982 return cont->saved_ec.thread_ptr->self;
986 cont_compact(
void *
ptr)
994 rb_execution_context_update(&cont->saved_ec);
1002 RUBY_MARK_ENTER(
"cont");
1008 rb_execution_context_mark(&cont->saved_ec);
1011 if (cont->saved_vm_stack.ptr) {
1012 #ifdef CAPTURE_JUST_VALID_VM_STACK
1014 cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
1017 cont->saved_vm_stack.ptr, cont->saved_ec.stack_size);
1021 if (cont->machine.stack) {
1022 if (cont->type == CONTINUATION_CONTEXT) {
1025 cont->machine.stack + cont->machine.stack_size);
1033 RUBY_MARK_LEAVE(
"cont");
1040 return fiber == fiber->cont.saved_ec.thread_ptr->root_fiber;
1044 static void jit_cont_free(
struct rb_jit_cont *cont);
1047 cont_free(
void *
ptr)
1051 RUBY_FREE_ENTER(
"cont");
1053 if (cont->type == CONTINUATION_CONTEXT) {
1055 RUBY_FREE_UNLESS_NULL(cont->machine.stack);
1059 coroutine_destroy(&fiber->context);
1060 fiber_stack_release(fiber);
1063 RUBY_FREE_UNLESS_NULL(cont->saved_vm_stack.ptr);
1065 VM_ASSERT(cont->jit_cont != NULL);
1066 jit_cont_free(cont->jit_cont);
1069 RUBY_FREE_LEAVE(
"cont");
1073 cont_memsize(
const void *
ptr)
1078 size =
sizeof(*cont);
1079 if (cont->saved_vm_stack.ptr) {
1080 #ifdef CAPTURE_JUST_VALID_VM_STACK
1081 size_t n = (cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
1083 size_t n = cont->saved_ec.vm_stack_size;
1085 size += n *
sizeof(*cont->saved_vm_stack.ptr);
1088 if (cont->machine.stack) {
1089 size += cont->machine.stack_size *
sizeof(*cont->machine.stack);
1098 if (fiber->cont.self) {
1102 rb_execution_context_update(&fiber->cont.saved_ec);
1109 if (fiber->cont.self) {
1113 rb_execution_context_mark(&fiber->cont.saved_ec);
1118 fiber_compact(
void *
ptr)
1123 if (fiber->prev) rb_fiber_update_self(fiber->prev);
1125 cont_compact(&fiber->cont);
1126 fiber_verify(fiber);
1130 fiber_mark(
void *
ptr)
1133 RUBY_MARK_ENTER(
"cont");
1134 fiber_verify(fiber);
1136 if (fiber->prev) rb_fiber_mark_self(fiber->prev);
1137 cont_mark(&fiber->cont);
1138 RUBY_MARK_LEAVE(
"cont");
1142 fiber_free(
void *
ptr)
1145 RUBY_FREE_ENTER(
"fiber");
1147 if (DEBUG) fprintf(stderr,
"fiber_free: %p[%p]\n", (
void *)fiber, fiber->stack.base);
1149 if (fiber->cont.saved_ec.local_storage) {
1150 rb_id_table_free(fiber->cont.saved_ec.local_storage);
1153 cont_free(&fiber->cont);
1154 RUBY_FREE_LEAVE(
"fiber");
1158 fiber_memsize(
const void *
ptr)
1161 size_t size =
sizeof(*fiber);
1163 const rb_thread_t *th = rb_ec_thread_ptr(saved_ec);
1168 if (saved_ec->local_storage && fiber != th->root_fiber) {
1169 size += rb_id_table_memsize(saved_ec->local_storage);
1170 size += rb_obj_memsize_of(saved_ec->storage);
1173 size += cont_memsize(&fiber->cont);
1188 SET_MACHINE_STACK_END(&th->ec->machine.stack_end);
1190 if (th->ec->machine.stack_start > th->ec->machine.stack_end) {
1191 size = cont->machine.stack_size = th->ec->machine.stack_start - th->ec->machine.stack_end;
1192 cont->machine.stack_src = th->ec->machine.stack_end;
1195 size = cont->machine.stack_size = th->ec->machine.stack_end - th->ec->machine.stack_start;
1196 cont->machine.stack_src = th->ec->machine.stack_start;
1199 if (cont->machine.stack) {
1206 FLUSH_REGISTER_WINDOWS;
1207 asan_unpoison_memory_region(cont->machine.stack_src, size,
false);
1208 MEMCPY(cont->machine.stack, cont->machine.stack_src,
VALUE, size);
1213 {cont_mark, cont_free, cont_memsize, cont_compact},
1214 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1222 VM_ASSERT(th->status == THREAD_RUNNABLE);
1229 sec->machine.stack_end = NULL;
1232 static rb_nativethread_lock_t jit_cont_lock;
1250 if (first_jit_cont == NULL) {
1251 cont->next = cont->prev = NULL;
1255 cont->next = first_jit_cont;
1256 first_jit_cont->prev = cont;
1258 first_jit_cont = cont;
1271 if (cont == first_jit_cont) {
1272 first_jit_cont = cont->next;
1273 if (first_jit_cont != NULL)
1274 first_jit_cont->prev = NULL;
1277 cont->prev->next = cont->next;
1278 if (cont->next != NULL)
1279 cont->next->prev = cont->prev;
1288 rb_jit_cont_each_iseq(rb_iseq_callback callback,
void *data)
1291 for (cont = first_jit_cont; cont != NULL; cont = cont->next) {
1292 if (cont->ec->vm_stack == NULL)
1296 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(cont->ec, cfp)) {
1297 if (cfp->pc && cfp->iseq && imemo_type((
VALUE)cfp->iseq) == imemo_iseq) {
1298 callback(cfp->iseq, data);
1300 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1309 rb_yjit_cancel_jit_return(
void *leave_exit,
void *leave_exception)
1312 for (cont = first_jit_cont; cont != NULL; cont = cont->next) {
1313 if (cont->ec->vm_stack == NULL)
1317 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(cont->ec, cfp)) {
1318 if (cfp->jit_return && cfp->jit_return != leave_exception) {
1321 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1329 rb_jit_cont_finish(
void)
1332 for (cont = first_jit_cont; cont != NULL; cont = next) {
1342 VM_ASSERT(cont->jit_cont == NULL);
1344 cont->jit_cont = jit_cont_new(&(cont->saved_ec));
1350 return &fiber->cont.saved_ec;
1357 cont_save_thread(cont, th);
1358 cont->saved_ec.thread_ptr = th;
1359 cont->saved_ec.local_storage = NULL;
1360 cont->saved_ec.local_storage_recursive_hash =
Qnil;
1361 cont->saved_ec.local_storage_recursive_hash_for_trace =
Qnil;
1362 cont_init_jit_cont(cont);
1366 cont_new(
VALUE klass)
1369 volatile VALUE contval;
1372 THREAD_MUST_BE_RUNNING(th);
1374 cont->self = contval;
1375 cont_init(cont, th);
1382 return fiber->cont.self;
1388 return fiber->blocking;
1393 rb_jit_cont_init(
void)
1402 VALUE *p = ec->vm_stack;
1403 while (p < ec->cfp->sp) {
1404 fprintf(stderr,
"%3d ", (
int)(p - ec->vm_stack));
1405 rb_obj_info_dump(*p);
1415 while (cfp != end_of_cfp) {
1418 pc = cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded;
1420 fprintf(stderr,
"%2d pc: %d\n", i++, pc);
1421 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1427 cont_capture(
volatile int *
volatile stat)
1431 volatile VALUE contval;
1434 THREAD_MUST_BE_RUNNING(th);
1435 rb_vm_stack_to_heap(th->ec);
1436 cont = cont_new(rb_cContinuation);
1437 contval = cont->self;
1439 #ifdef CAPTURE_JUST_VALID_VM_STACK
1440 cont->saved_vm_stack.slen = ec->cfp->sp - ec->vm_stack;
1441 cont->saved_vm_stack.clen = ec->vm_stack + ec->vm_stack_size - (
VALUE*)ec->cfp;
1442 cont->saved_vm_stack.ptr =
ALLOC_N(
VALUE, cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
1443 MEMCPY(cont->saved_vm_stack.ptr,
1445 VALUE, cont->saved_vm_stack.slen);
1446 MEMCPY(cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen,
1449 cont->saved_vm_stack.clen);
1451 cont->saved_vm_stack.ptr =
ALLOC_N(
VALUE, ec->vm_stack_size);
1452 MEMCPY(cont->saved_vm_stack.ptr, ec->vm_stack,
VALUE, ec->vm_stack_size);
1455 rb_ec_set_vm_stack(&cont->saved_ec, NULL, 0);
1456 VM_ASSERT(cont->saved_ec.cfp != NULL);
1457 cont_save_machine_stack(th, cont);
1459 if (ruby_setjmp(cont->jmpbuf)) {
1462 VAR_INITIALIZED(cont);
1463 value = cont->value;
1481 if (cont->type == CONTINUATION_CONTEXT) {
1486 if (sec->fiber_ptr != NULL) {
1487 fiber = sec->fiber_ptr;
1489 else if (th->root_fiber) {
1490 fiber = th->root_fiber;
1493 if (fiber && th->ec != &fiber->cont.saved_ec) {
1494 ec_switch(th, fiber);
1497 if (th->ec->trace_arg != sec->trace_arg) {
1502 #ifdef CAPTURE_JUST_VALID_VM_STACK
1504 cont->saved_vm_stack.ptr,
1505 VALUE, cont->saved_vm_stack.slen);
1506 MEMCPY(th->ec->vm_stack + th->ec->vm_stack_size - cont->saved_vm_stack.clen,
1507 cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen,
1508 VALUE, cont->saved_vm_stack.clen);
1510 MEMCPY(th->ec->vm_stack, cont->saved_vm_stack.ptr,
VALUE, sec->vm_stack_size);
1514 th->ec->cfp = sec->cfp;
1515 th->ec->raised_flag = sec->raised_flag;
1516 th->ec->tag = sec->tag;
1517 th->ec->root_lep = sec->root_lep;
1518 th->ec->root_svar = sec->root_svar;
1519 th->ec->errinfo = sec->errinfo;
1521 VM_ASSERT(th->ec->vm_stack != NULL);
1537 if (!FIBER_TERMINATED_P(old_fiber)) {
1538 STACK_GROW_DIR_DETECTION;
1539 SET_MACHINE_STACK_END(&th->ec->machine.stack_end);
1540 if (STACK_DIR_UPPER(0, 1)) {
1541 old_fiber->cont.machine.stack_size = th->ec->machine.stack_start - th->ec->machine.stack_end;
1542 old_fiber->cont.machine.stack = th->ec->machine.stack_end;
1545 old_fiber->cont.machine.stack_size = th->ec->machine.stack_end - th->ec->machine.stack_start;
1546 old_fiber->cont.machine.stack = th->ec->machine.stack_start;
1551 old_fiber->cont.saved_ec.machine.stack_start = th->ec->machine.stack_start;
1552 old_fiber->cont.saved_ec.machine.stack_end = FIBER_TERMINATED_P(old_fiber) ? NULL : th->ec->machine.stack_end;
1557 #if defined(COROUTINE_SANITIZE_ADDRESS)
1558 __sanitizer_start_switch_fiber(FIBER_TERMINATED_P(old_fiber) ? NULL : &old_fiber->context.fake_stack, new_fiber->context.stack_base, new_fiber->context.stack_size);
1562 struct coroutine_context * from = coroutine_transfer(&old_fiber->context, &new_fiber->context);
1564 #if defined(COROUTINE_SANITIZE_ADDRESS)
1565 __sanitizer_finish_switch_fiber(old_fiber->context.fake_stack, NULL, NULL);
1573 fiber_restore_thread(th, old_fiber);
1579 NOINLINE(NORETURN(
static void cont_restore_1(
rb_context_t *)));
1584 cont_restore_thread(cont);
1587 #if defined(_M_AMD64) && !defined(__MINGW64__)
1592 _JUMP_BUFFER *bp = (
void*)&cont->jmpbuf;
1593 bp->Frame = ((_JUMP_BUFFER*)((
void*)&buf))->Frame;
1596 if (cont->machine.stack_src) {
1597 FLUSH_REGISTER_WINDOWS;
1598 MEMCPY(cont->machine.stack_src, cont->machine.stack,
1599 VALUE, cont->machine.stack_size);
1602 ruby_longjmp(cont->jmpbuf, 1);
1610 if (cont->machine.stack_src) {
1612 #define STACK_PAD_SIZE 1
1614 #define STACK_PAD_SIZE 1024
1616 VALUE space[STACK_PAD_SIZE];
1618 #if !STACK_GROW_DIRECTION
1619 if (addr_in_prev_frame > &space[0]) {
1622 #if STACK_GROW_DIRECTION <= 0
1623 volatile VALUE *
const end = cont->machine.stack_src;
1624 if (&space[0] > end) {
1633 cont_restore_0(cont, &space[0]);
1637 #if !STACK_GROW_DIRECTION
1642 #if STACK_GROW_DIRECTION >= 0
1643 volatile VALUE *
const end = cont->machine.stack_src + cont->machine.stack_size;
1644 if (&space[STACK_PAD_SIZE] < end) {
1649 cont_restore_0(cont, &space[STACK_PAD_SIZE-1]);
1653 #if !STACK_GROW_DIRECTION
1657 cont_restore_1(cont);
1744 rb_callcc(
VALUE self)
1746 volatile int called;
1747 volatile VALUE val = cont_capture(&called);
1756 #ifdef RUBY_ASAN_ENABLED
1759 MAYBE_UNUSED(
static void notusing_callcc(
void)) { rb_callcc(
Qnil); }
1760 # define rb_callcc rb_f_notimplement
1765 make_passing_arg(
int argc,
const VALUE *argv)
1781 NORETURN(
static VALUE rb_cont_call(
int argc,
VALUE *argv,
VALUE contval));
1799 rb_cont_call(
int argc,
VALUE *argv,
VALUE contval)
1804 if (cont_thread_value(cont) != th->self) {
1807 if (cont->saved_ec.fiber_ptr) {
1808 if (th->ec->fiber_ptr != cont->saved_ec.fiber_ptr) {
1814 cont->value = make_passing_arg(argc, argv);
1816 cont_restore_0(cont, &contval);
1909 {fiber_mark, fiber_free, fiber_memsize, fiber_compact,},
1910 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1914 fiber_alloc(
VALUE klass)
1920 fiber_t_alloc(
VALUE fiber_value,
unsigned int blocking)
1929 THREAD_MUST_BE_RUNNING(th);
1931 fiber->cont.self = fiber_value;
1932 fiber->cont.type = FIBER_CONTEXT;
1933 fiber->blocking = blocking;
1935 cont_init(&fiber->cont, th);
1937 fiber->cont.saved_ec.fiber_ptr = fiber;
1938 rb_ec_clear_vm_stack(&fiber->cont.saved_ec);
1944 VM_ASSERT(FIBER_CREATED_P(fiber));
1954 VALUE fiber_value = fiber_alloc(rb_cFiber);
1957 VM_ASSERT(
DATA_PTR(fiber_value) == NULL);
1958 VM_ASSERT(fiber->cont.type == FIBER_CONTEXT);
1959 VM_ASSERT(FIBER_RESUMED_P(fiber));
1961 th->root_fiber = fiber;
1963 fiber->cont.self = fiber_value;
1965 coroutine_initialize_main(&fiber->context);
1974 if (ec->fiber_ptr->cont.self == 0) {
1975 root_fiber_alloc(rb_ec_thread_ptr(ec));
1977 return ec->fiber_ptr;
1981 current_fiber_storage(
void)
1988 inherit_fiber_storage(
void)
1996 fiber->cont.saved_ec.storage = storage;
2000 fiber_storage_get(
rb_fiber_t *fiber,
int allocate)
2002 VALUE storage = fiber->cont.saved_ec.storage;
2003 if (storage ==
Qnil && allocate) {
2005 fiber_storage_set(fiber, storage);
2011 storage_access_must_be_from_same_fiber(
VALUE self)
2015 if (fiber != current) {
2027 rb_fiber_storage_get(
VALUE self)
2029 storage_access_must_be_from_same_fiber(
self);
2031 VALUE storage = fiber_storage_get(fiber_ptr(
self), FALSE);
2033 if (storage ==
Qnil) {
2050 fiber_storage_validate(
VALUE value)
2053 if (value ==
Qnil)
return;
2089 rb_fiber_storage_set(
VALUE self,
VALUE value)
2093 "Fiber#storage= is experimental and may be removed in the future!");
2096 storage_access_must_be_from_same_fiber(
self);
2097 fiber_storage_validate(value);
2099 fiber_ptr(
self)->cont.saved_ec.storage =
rb_obj_dup(value);
2114 rb_fiber_storage_aref(
VALUE class,
VALUE key)
2118 VALUE storage = fiber_storage_get(fiber_current(), FALSE);
2139 VALUE storage = fiber_storage_get(fiber_current(), value !=
Qnil);
2142 if (value ==
Qnil) {
2155 storage = inherit_fiber_storage();
2158 fiber_storage_validate(storage);
2162 rb_fiber_t *fiber = fiber_t_alloc(
self, blocking);
2164 fiber->cont.saved_ec.storage = storage;
2165 fiber->first_proc = proc;
2166 fiber->stack.base = NULL;
2178 size_t vm_stack_size = 0;
2179 VALUE *vm_stack = fiber_initialize_coroutine(fiber, &vm_stack_size);
2182 cont->saved_vm_stack.ptr = NULL;
2183 rb_ec_initialize_vm_stack(sec, vm_stack, vm_stack_size /
sizeof(
VALUE));
2186 sec->local_storage = NULL;
2187 sec->local_storage_recursive_hash =
Qnil;
2188 sec->local_storage_recursive_hash_for_trace =
Qnil;
2192 rb_fiber_pool_default(
VALUE pool)
2194 return &shared_fiber_pool;
2200 fiber->cont.saved_ec.storage = storage;
2206 rb_fiber_initialize_kw(
int argc,
VALUE* argv,
VALUE self,
int kw_splat)
2217 rb_get_kwargs(options, fiber_initialize_keywords, 0, 3, arguments);
2219 if (!UNDEF_P(arguments[0])) {
2220 blocking = arguments[0];
2223 if (!UNDEF_P(arguments[1])) {
2224 pool = arguments[1];
2227 storage = arguments[2];
2230 return fiber_initialize(
self,
rb_block_proc(), rb_fiber_pool_default(pool),
RTEST(blocking), storage);
2283 rb_fiber_initialize(
int argc,
VALUE* argv,
VALUE self)
2291 return fiber_initialize(fiber_alloc(rb_cFiber),
rb_proc_new(func, obj), rb_fiber_pool_default(
Qnil), 0, storage);
2301 rb_fiber_s_schedule_kw(
int argc,
VALUE* argv,
int kw_splat)
2304 VALUE scheduler = th->scheduler;
2307 if (scheduler !=
Qnil) {
2359 rb_fiber_s_schedule(
int argc,
VALUE *argv,
VALUE obj)
2375 rb_fiber_s_scheduler(
VALUE klass)
2389 rb_fiber_current_scheduler(
VALUE klass)
2411 rb_fiber_set_scheduler(
VALUE klass,
VALUE scheduler)
2416 NORETURN(
static void rb_fiber_terminate(
rb_fiber_t *fiber,
int need_interrupt,
VALUE err));
2421 rb_thread_t *
volatile th = fiber->cont.saved_ec.thread_ptr;
2424 enum ruby_tag_type state;
2426 VM_ASSERT(th->ec == GET_EC());
2427 VM_ASSERT(FIBER_RESUMED_P(fiber));
2429 if (fiber->blocking) {
2433 EC_PUSH_TAG(th->ec);
2434 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2437 const VALUE *argv, args = cont->value;
2438 GetProcPtr(fiber->first_proc, proc);
2441 th->ec->errinfo =
Qnil;
2442 th->ec->root_lep = rb_vm_proc_local_ep(fiber->first_proc);
2443 th->ec->root_svar =
Qfalse;
2446 cont->value = rb_vm_invoke_proc(th->ec, proc, argc, argv, cont->kw_splat, VM_BLOCK_HANDLER_NONE);
2450 int need_interrupt = TRUE;
2453 err = th->ec->errinfo;
2454 VM_ASSERT(FIBER_RESUMED_P(fiber));
2456 if (state == TAG_RAISE) {
2459 else if (state == TAG_FATAL && err == RUBY_FATAL_FIBER_KILLED) {
2460 need_interrupt = FALSE;
2463 else if (state == TAG_FATAL) {
2464 rb_threadptr_pending_interrupt_enque(th, err);
2467 err = rb_vm_make_jump_tag_but_local_jump(state, err);
2471 rb_fiber_terminate(fiber, need_interrupt, err);
2482 fiber->cont.type = FIBER_CONTEXT;
2483 fiber->cont.saved_ec.fiber_ptr = fiber;
2484 fiber->cont.saved_ec.thread_ptr = th;
2485 fiber->blocking = 1;
2487 fiber_status_set(fiber, FIBER_RESUMED);
2488 th->ec = &fiber->cont.saved_ec;
2489 cont_init_jit_cont(&fiber->cont);
2495 if (th->root_fiber) {
2501 VM_ASSERT(th->ec->fiber_ptr->cont.type == FIBER_CONTEXT);
2502 VM_ASSERT(th->ec->fiber_ptr->cont.self == 0);
2504 if (ec && th->ec == ec) {
2505 rb_ractor_set_current_ec(th->ractor, NULL);
2507 fiber_free(th->ec->fiber_ptr);
2513 rb_threadptr_root_fiber_terminate(
rb_thread_t *th)
2517 fiber->status = FIBER_TERMINATED;
2520 rb_ec_clear_vm_stack(th->ec);
2524 return_fiber(
bool terminate)
2531 prev->resuming_fiber = NULL;
2536 rb_raise(rb_eFiberError,
"attempt to yield on a not resumed fiber");
2542 VM_ASSERT(root_fiber != NULL);
2545 for (fiber = root_fiber; fiber->resuming_fiber; fiber = fiber->resuming_fiber) {
2555 return fiber_current()->cont.self;
2564 if (th->ec->fiber_ptr != NULL) {
2565 fiber = th->ec->fiber_ptr;
2569 fiber = root_fiber_alloc(th);
2572 if (FIBER_CREATED_P(next_fiber)) {
2573 fiber_prepare_stack(next_fiber);
2576 VM_ASSERT(FIBER_RESUMED_P(fiber) || FIBER_TERMINATED_P(fiber));
2577 VM_ASSERT(FIBER_RUNNABLE_P(next_fiber));
2579 if (FIBER_RESUMED_P(fiber)) fiber_status_set(fiber, FIBER_SUSPENDED);
2581 fiber_status_set(next_fiber, FIBER_RESUMED);
2582 fiber_setcontext(next_fiber, fiber);
2588 VM_ASSERT(fiber == fiber_current());
2590 if (fiber->killed) {
2591 rb_thread_t *thread = fiber->cont.saved_ec.thread_ptr;
2593 thread->ec->errinfo = RUBY_FATAL_FIBER_KILLED;
2594 EC_JUMP_TAG(thread->ec, RUBY_TAG_FATAL);
2606 if (th->root_fiber == NULL) root_fiber_alloc(th);
2608 if (th->ec->fiber_ptr == fiber) {
2612 return make_passing_arg(argc, argv);
2615 if (cont_thread_value(cont) != th->self) {
2616 rb_raise(rb_eFiberError,
"fiber called across threads");
2619 if (FIBER_TERMINATED_P(fiber)) {
2620 value =
rb_exc_new2(rb_eFiberError,
"dead fiber called");
2622 if (!FIBER_TERMINATED_P(th->ec->fiber_ptr)) {
2624 VM_UNREACHABLE(fiber_switch);
2630 VM_ASSERT(FIBER_SUSPENDED_P(th->root_fiber));
2632 cont = &th->root_fiber->cont;
2634 cont->value = value;
2636 fiber_setcontext(th->root_fiber, th->ec->fiber_ptr);
2638 VM_UNREACHABLE(fiber_switch);
2642 VM_ASSERT(FIBER_RUNNABLE_P(fiber));
2646 VM_ASSERT(!current_fiber->resuming_fiber);
2648 if (resuming_fiber) {
2649 current_fiber->resuming_fiber = resuming_fiber;
2650 fiber->prev = fiber_current();
2651 fiber->yielding = 0;
2654 VM_ASSERT(!current_fiber->yielding);
2656 current_fiber->yielding = 1;
2659 if (current_fiber->blocking) {
2664 cont->kw_splat = kw_splat;
2665 cont->value = make_passing_arg(argc, argv);
2667 fiber_store(fiber, th);
2670 #ifndef COROUTINE_PTHREAD_CONTEXT
2671 if (resuming_fiber && FIBER_TERMINATED_P(fiber)) {
2672 fiber_stack_release(fiber);
2676 if (fiber_current()->blocking) {
2680 RUBY_VM_CHECK_INTS(th->ec);
2684 current_fiber = th->ec->fiber_ptr;
2685 value = current_fiber->cont.value;
2687 fiber_check_killed(current_fiber);
2689 if (current_fiber->cont.argc == -1) {
2700 return fiber_switch(fiber_ptr(fiber_value), argc, argv,
RB_NO_KEYWORDS, NULL,
false);
2718 rb_fiber_blocking_p(
VALUE fiber)
2720 return RBOOL(fiber_ptr(fiber)->blocking);
2724 fiber_blocking_yield(
VALUE fiber_value)
2727 rb_thread_t *
volatile th = fiber->cont.saved_ec.thread_ptr;
2729 VM_ASSERT(fiber->blocking == 0);
2732 fiber->blocking = 1;
2741 fiber_blocking_ensure(
VALUE fiber_value)
2744 rb_thread_t *
volatile th = fiber->cont.saved_ec.thread_ptr;
2747 fiber->blocking = 0;
2764 rb_fiber_blocking(
VALUE class)
2770 if (fiber->blocking) {
2774 return rb_ensure(fiber_blocking_yield, fiber_value, fiber_blocking_ensure, fiber_value);
2797 rb_fiber_s_blocking_p(
VALUE klass)
2800 unsigned blocking = thread->blocking;
2811 fiber_status_set(fiber, FIBER_TERMINATED);
2815 rb_fiber_terminate(
rb_fiber_t *fiber,
int need_interrupt,
VALUE error)
2817 VALUE value = fiber->cont.value;
2819 VM_ASSERT(FIBER_RESUMED_P(fiber));
2820 rb_fiber_close(fiber);
2822 fiber->cont.machine.stack = NULL;
2823 fiber->cont.machine.stack_size = 0;
2827 if (need_interrupt) RUBY_VM_SET_INTERRUPT(&next_fiber->cont.saved_ec);
2830 fiber_switch(next_fiber, -1, &error,
RB_NO_KEYWORDS, NULL,
false);
2832 fiber_switch(next_fiber, 1, &value,
RB_NO_KEYWORDS, NULL,
false);
2837 fiber_resume_kw(
rb_fiber_t *fiber,
int argc,
const VALUE *argv,
int kw_splat)
2841 if (argc == -1 && FIBER_CREATED_P(fiber)) {
2842 rb_raise(rb_eFiberError,
"cannot raise exception on unborn fiber");
2844 else if (FIBER_TERMINATED_P(fiber)) {
2845 rb_raise(rb_eFiberError,
"attempt to resume a terminated fiber");
2847 else if (fiber == current_fiber) {
2848 rb_raise(rb_eFiberError,
"attempt to resume the current fiber");
2850 else if (fiber->prev != NULL) {
2851 rb_raise(rb_eFiberError,
"attempt to resume a resumed fiber (double resume)");
2853 else if (fiber->resuming_fiber) {
2854 rb_raise(rb_eFiberError,
"attempt to resume a resuming fiber");
2856 else if (fiber->prev == NULL &&
2857 (!fiber->yielding && fiber->status != FIBER_CREATED)) {
2858 rb_raise(rb_eFiberError,
"attempt to resume a transferring fiber");
2861 return fiber_switch(fiber, argc, argv, kw_splat, fiber,
false);
2867 return fiber_resume_kw(fiber_ptr(
self), argc, argv, kw_splat);
2873 return fiber_resume_kw(fiber_ptr(
self), argc, argv,
RB_NO_KEYWORDS);
2879 return fiber_switch(return_fiber(
false), argc, argv, kw_splat, NULL,
true);
2885 return fiber_switch(return_fiber(
false), argc, argv,
RB_NO_KEYWORDS, NULL,
true);
2889 rb_fiber_reset_root_local_storage(
rb_thread_t *th)
2891 if (th->root_fiber && th->root_fiber != th->ec->fiber_ptr) {
2892 th->ec->local_storage = th->root_fiber->cont.saved_ec.local_storage;
2907 return RBOOL(!FIBER_TERMINATED_P(fiber_ptr(fiber_value)));
2926 rb_fiber_m_resume(
int argc,
VALUE *argv,
VALUE fiber)
2978 rb_fiber_backtrace(
int argc,
VALUE *argv,
VALUE fiber)
2980 return rb_vm_backtrace(argc, argv, &fiber_ptr(fiber)->cont.saved_ec);
3003 rb_fiber_backtrace_locations(
int argc,
VALUE *argv,
VALUE fiber)
3005 return rb_vm_backtrace_locations(argc, argv, &fiber_ptr(fiber)->cont.saved_ec);
3091 rb_fiber_m_transfer(
int argc,
VALUE *argv,
VALUE self)
3097 fiber_transfer_kw(
rb_fiber_t *fiber,
int argc,
const VALUE *argv,
int kw_splat)
3099 if (fiber->resuming_fiber) {
3100 rb_raise(rb_eFiberError,
"attempt to transfer to a resuming fiber");
3103 if (fiber->yielding) {
3104 rb_raise(rb_eFiberError,
"attempt to transfer to a yielding fiber");
3107 return fiber_switch(fiber, argc, argv, kw_splat, NULL,
false);
3113 return fiber_transfer_kw(fiber_ptr(
self), argc, argv, kw_splat);
3127 rb_fiber_s_yield(
int argc,
VALUE *argv,
VALUE klass)
3135 if (fiber == fiber_current()) {
3138 else if (fiber->resuming_fiber) {
3139 return fiber_raise(fiber->resuming_fiber, exception);
3141 else if (FIBER_SUSPENDED_P(fiber) && !fiber->yielding) {
3154 return fiber_raise(fiber_ptr(fiber), exception);
3183 rb_fiber_m_raise(
int argc,
VALUE *argv,
VALUE self)
3206 rb_fiber_m_kill(
VALUE self)
3210 if (fiber->killed)
return Qfalse;
3213 if (fiber->status == FIBER_CREATED) {
3214 fiber->status = FIBER_TERMINATED;
3216 else if (fiber->status != FIBER_TERMINATED) {
3217 if (fiber_current() == fiber) {
3218 fiber_check_killed(fiber);
3221 fiber_raise(fiber_ptr(
self),
Qnil);
3236 rb_fiber_s_current(
VALUE klass)
3242 fiber_to_s(
VALUE fiber_value)
3244 const rb_fiber_t *fiber = fiber_ptr(fiber_value);
3246 char status_info[0x20];
3248 if (fiber->resuming_fiber) {
3249 snprintf(status_info, 0x20,
" (%s by resuming)", fiber_status_name(fiber->status));
3252 snprintf(status_info, 0x20,
" (%s)", fiber_status_name(fiber->status));
3257 strlcat(status_info,
">",
sizeof(status_info));
3262 GetProcPtr(fiber->first_proc, proc);
3263 return rb_block_to_s(fiber_value, &proc->block, status_info);
3266 #ifdef HAVE_WORKING_FORK
3270 if (th->root_fiber) {
3271 if (&th->root_fiber->cont.saved_ec != th->ec) {
3272 th->root_fiber = th->ec->fiber_ptr;
3274 th->root_fiber->prev = 0;
3279 #ifdef RB_EXPERIMENTAL_FIBER_POOL
3281 fiber_pool_free(
void *
ptr)
3284 RUBY_FREE_ENTER(
"fiber_pool");
3286 fiber_pool_allocation_free(
fiber_pool->allocations);
3289 RUBY_FREE_LEAVE(
"fiber_pool");
3293 fiber_pool_memsize(
const void *
ptr)
3296 size_t size =
sizeof(*fiber_pool);
3305 {NULL, fiber_pool_free, fiber_pool_memsize,},
3306 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3310 fiber_pool_alloc(
VALUE klass)
3318 rb_fiber_pool_initialize(
int argc,
VALUE* argv,
VALUE self)
3325 rb_scan_args(argc, argv,
"03", &size, &count, &vm_stack_size);
3328 size =
SIZET2NUM(th->vm->default_params.fiber_machine_stack_size);
3335 if (
NIL_P(vm_stack_size)) {
3336 vm_stack_size =
SIZET2NUM(th->vm->default_params.fiber_vm_stack_size);
3364 size_t vm_stack_size = th->vm->default_params.fiber_vm_stack_size;
3365 size_t machine_stack_size = th->vm->default_params.fiber_machine_stack_size;
3366 size_t stack_size = machine_stack_size + vm_stack_size;
3370 GetSystemInfo(&info);
3371 pagesize = info.dwPageSize;
3373 pagesize = sysconf(_SC_PAGESIZE);
3375 SET_MACHINE_STACK_END(&th->ec->machine.stack_end);
3377 fiber_pool_initialize(&shared_fiber_pool, stack_size, FIBER_POOL_INITIAL_SIZE, vm_stack_size);
3383 const char *fiber_shared_fiber_pool_free_stacks = getenv(
"RUBY_SHARED_FIBER_POOL_FREE_STACKS");
3384 if (fiber_shared_fiber_pool_free_stacks) {
3385 shared_fiber_pool.free_stacks = atoi(fiber_shared_fiber_pool_free_stacks);
3387 if (shared_fiber_pool.free_stacks < 0) {
3388 rb_warn(
"Setting RUBY_SHARED_FIBER_POOL_FREE_STACKS to a negative value is not allowed.");
3389 shared_fiber_pool.free_stacks = 0;
3392 if (shared_fiber_pool.free_stacks > 1) {
3393 rb_warn(
"Setting RUBY_SHARED_FIBER_POOL_FREE_STACKS to a value greater than 1 is operating system specific, and may cause crashes.");
3414 rb_define_method(rb_cFiber,
"backtrace_locations", rb_fiber_backtrace_locations, -1);
3427 #ifdef RB_EXPERIMENTAL_FIBER_POOL
3430 rb_define_method(rb_cFiberPool,
"initialize", rb_fiber_pool_initialize, -1);
3436 RUBY_SYMBOL_EXPORT_BEGIN
3439 ruby_Init_Continuation_body(
void)
3449 RUBY_SYMBOL_EXPORT_END
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define RUBY_EVENT_FIBER_SWITCH
Encountered a Fiber#yield.
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
int rb_scan_args_kw(int kw_flag, int argc, const VALUE *argv, const char *fmt,...)
Identical to rb_scan_args(), except it also accepts kw_splat.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
int rb_keyword_given_p(void)
Determines if the current method is given a keyword argument.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
#define REALLOC_N
Old name of RB_REALLOC_N.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define ZALLOC
Old name of RB_ZALLOC.
#define CLASS_OF
Old name of rb_class_of.
#define rb_ary_new4
Old name of rb_ary_new_from_values.
#define SIZET2NUM
Old name of RB_SIZE2NUM.
#define rb_exc_new2
Old name of rb_exc_new_cstr.
#define T_HASH
Old name of RUBY_T_HASH.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define NUM2SIZET
Old name of RB_NUM2SIZE.
void ruby_stop(int ex)
Calls ruby_cleanup() and exits the process.
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
void rb_syserr_fail(int e, const char *mesg)
Raises appropriate exception that represents a C errno.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
VALUE rb_eStandardError
StandardError exception.
VALUE rb_eFrozenError
FrozenError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eArgError
ArgumentError exception.
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
@ RB_WARN_CATEGORY_EXPERIMENTAL
Warning is for experimental features.
VALUE rb_any_to_s(VALUE obj)
Generates a textual representation of the given object.
VALUE rb_obj_dup(VALUE obj)
Duplicates the given object.
void rb_gc_mark(VALUE obj)
Marks an object.
void rb_memerror(void)
Triggers out-of-memory error.
void rb_gc_mark_movable(VALUE obj)
Maybe this is the only function provided for C extensions to control the pinning of objects,...
VALUE rb_gc_location(VALUE obj)
Finds a new "location" of an object.
void rb_gc_mark_locations(const VALUE *start, const VALUE *end)
Marks objects between the two pointers.
VALUE rb_fiber_transfer_kw(VALUE fiber, int argc, const VALUE *argv, int kw_splat)
Identical to rb_fiber_transfer(), except you can specify how to handle the last element of the given ...
VALUE rb_fiber_new_storage(rb_block_call_func_t func, VALUE callback_obj, VALUE storage)
Creates a Fiber instance from a C-backended block with the specified storage.
VALUE rb_fiber_raise(VALUE fiber, int argc, const VALUE *argv)
Identical to rb_fiber_resume() but instead of resuming normal execution of the passed fiber,...
VALUE rb_fiber_current(void)
Queries the fiber which is calling this function.
VALUE rb_fiber_yield_kw(int argc, const VALUE *argv, int kw_splat)
Identical to rb_fiber_yield(), except you can specify how to handle the last element of the given arr...
VALUE rb_fiber_transfer(VALUE fiber, int argc, const VALUE *argv)
Transfers control to another fiber, resuming it from where it last stopped or starting it if it was n...
VALUE rb_fiber_resume_kw(VALUE fiber, int argc, const VALUE *argv, int kw_splat)
Identical to rb_fiber_resume(), except you can specify how to handle the last element of the given ar...
VALUE rb_fiber_alive_p(VALUE fiber)
Queries the liveness of the passed fiber.
VALUE rb_fiber_new(rb_block_call_func_t func, VALUE callback_obj)
Creates a Fiber instance from a C-backended block.
VALUE rb_obj_is_fiber(VALUE obj)
Queries if an object is a fiber.
VALUE rb_fiber_yield(int argc, const VALUE *argv)
Yields the control back to the point where the current fiber was resumed.
VALUE rb_fiber_resume(VALUE fiber, int argc, const VALUE *argv)
Resumes the execution of the passed fiber, either from the point at which the last rb_fiber_yield() w...
VALUE rb_make_exception(int argc, const VALUE *argv)
Constructs an exception object from the list of arguments, in a manner similar to Ruby's raise.
void rb_hash_foreach(VALUE hash, int(*func)(VALUE key, VALUE val, VALUE arg), VALUE arg)
Iterates over a hash.
VALUE rb_hash_delete(VALUE hash, VALUE key)
Deletes the passed key from the passed hash table, if any.
VALUE rb_hash_aref(VALUE hash, VALUE key)
Queries the given key in the given hash table.
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Inserts or replaces ("upsert"s) the objects into the given hash table.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
VALUE rb_proc_new(rb_block_call_func_t func, VALUE callback_arg)
This is an rb_iterate() + rb_block_proc() combo.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
VALUE rb_str_cat_cstr(VALUE dst, const char *src)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
VALUE rb_yield(VALUE val)
Yields the block.
rb_block_call_func * rb_block_call_func_t
Shorthand type that represents an iterator-written-in-C function pointer.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define ALLOCA_N(type, n)
#define RB_ALLOC(type)
Shorthand of RB_ALLOC_N with n=1.
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define DATA_PTR(obj)
Convenient getter macro.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
#define errno
Ractor-aware version of errno.
#define RB_NO_KEYWORDS
Do not pass keywords.
VALUE rb_fiber_scheduler_current(void)
Identical to rb_fiber_scheduler_get(), except it also returns RUBY_Qnil in case of a blocking fiber.
VALUE rb_fiber_scheduler_set(VALUE scheduler)
Destructively assigns the passed scheduler to that of the current thread that is calling this functio...
VALUE rb_fiber_scheduler_get(void)
Queries the current scheduler of the current thread that is calling this function.
VALUE rb_fiber_scheduler_fiber(VALUE scheduler, int argc, VALUE *argv, int kw_splat)
Create and schedule a non-blocking fiber.
#define RTEST
This is an old name of RB_TEST.
This is the struct that holds necessary info for a struct.
void rb_native_mutex_lock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_lock.
void rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_initialize.
void rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_unlock.
void rb_native_mutex_destroy(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_destroy.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
void ruby_xfree(void *ptr)
Deallocates a storage instance.