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 size_t mmap_size = (*count)*stride;
479 void * base = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, FIBER_STACK_FLAGS, -1, 0);
481 if (base == MAP_FAILED) {
483 *count = (*count) >> 1;
486 ruby_annotate_mmap(base, mmap_size,
"Ruby:fiber_pool_allocate_memory");
487 #if defined(MADV_FREE_REUSE)
491 while (madvise(base, mmap_size, MADV_FREE_REUSE) == -1 &&
errno == EAGAIN);
508 STACK_GROW_DIR_DETECTION;
511 size_t stride = size + RB_PAGE_SIZE;
514 void * base = fiber_pool_allocate_memory(&count, stride);
517 rb_raise(rb_eFiberError,
"can't alloc machine stack to fiber (%"PRIuSIZE
" x %"PRIuSIZE
" bytes): %s", count, size, ERRNOMSG);
524 allocation->base = base;
525 allocation->size = size;
526 allocation->stride = stride;
527 allocation->count = count;
528 #ifdef FIBER_POOL_ALLOCATION_FREE
529 allocation->used = 0;
534 fprintf(stderr,
"fiber_pool_expand(%"PRIuSIZE
"): %p, %"PRIuSIZE
"/%"PRIuSIZE
" x [%"PRIuSIZE
":%"PRIuSIZE
"]\n",
539 for (
size_t i = 0; i < count; i += 1) {
540 void * base = (
char*)allocation->base + (stride * i);
541 void * page = (
char*)base + STACK_DIR_UPPER(size, 0);
546 if (!VirtualProtect(page, RB_PAGE_SIZE, PAGE_READWRITE | PAGE_GUARD, &old_protect)) {
547 VirtualFree(allocation->base, 0, MEM_RELEASE);
548 rb_raise(rb_eFiberError,
"can't set a guard page: %s", ERRNOMSG);
551 if (mprotect(page, RB_PAGE_SIZE, PROT_NONE) < 0) {
552 munmap(allocation->base, count*stride);
553 rb_raise(rb_eFiberError,
"can't set a guard page: %s", ERRNOMSG);
557 vacancies = fiber_pool_vacancy_initialize(
559 (
char*)base + STACK_DIR_UPPER(0, RB_PAGE_SIZE),
563 #ifdef FIBER_POOL_ALLOCATION_FREE
564 vacancies->stack.allocation = allocation;
571 #ifdef FIBER_POOL_ALLOCATION_FREE
572 if (allocation->next) {
573 allocation->next->previous = allocation;
576 allocation->previous = NULL;
589 fiber_pool_initialize(
struct fiber_pool *
fiber_pool,
size_t size,
size_t count,
size_t vm_stack_size)
591 VM_ASSERT(vm_stack_size < size);
595 fiber_pool->size = ((size / RB_PAGE_SIZE) + 1) * RB_PAGE_SIZE;
606 #ifdef FIBER_POOL_ALLOCATION_FREE
611 STACK_GROW_DIR_DETECTION;
613 VM_ASSERT(allocation->used == 0);
615 if (DEBUG) fprintf(stderr,
"fiber_pool_allocation_free: %p base=%p count=%"PRIuSIZE
"\n", (
void*)allocation, allocation->base, allocation->count);
618 for (i = 0; i < allocation->count; i += 1) {
619 void * base = (
char*)allocation->base + (allocation->stride * i) + STACK_DIR_UPPER(0, RB_PAGE_SIZE);
621 struct fiber_pool_vacancy * vacancy = fiber_pool_vacancy_pointer(base, allocation->size);
624 fiber_pool_vacancy_remove(vacancy);
628 VirtualFree(allocation->base, 0, MEM_RELEASE);
630 munmap(allocation->base, allocation->stride * allocation->count);
633 if (allocation->previous) {
634 allocation->previous->next = allocation->next;
638 allocation->pool->allocations = allocation->next;
641 if (allocation->next) {
642 allocation->next->previous = allocation->previous;
645 allocation->pool->count -= allocation->count;
657 if (DEBUG) fprintf(stderr,
"fiber_pool_stack_acquire: %p used=%"PRIuSIZE
"\n", (
void*)
fiber_pool->vacancies,
fiber_pool->used);
660 const size_t maximum = FIBER_POOL_ALLOCATION_MAXIMUM_SIZE;
661 const size_t minimum =
fiber_pool->initial_count;
664 if (count > maximum) count = maximum;
665 if (count < minimum) count = minimum;
676 VM_ASSERT(vacancy->stack.base);
678 #if defined(COROUTINE_SANITIZE_ADDRESS)
679 __asan_unpoison_memory_region(fiber_pool_stack_poison_base(&vacancy->stack), fiber_pool_stack_poison_size(&vacancy->stack));
685 #ifdef FIBER_POOL_ALLOCATION_FREE
686 vacancy->stack.allocation->used += 1;
689 fiber_pool_stack_reset(&vacancy->stack);
691 return vacancy->stack;
699 void * base = fiber_pool_stack_base(stack);
700 size_t size = stack->available;
703 VM_ASSERT(size <= (stack->size - RB_PAGE_SIZE));
705 int advice = stack->pool->free_stacks >> 1;
707 if (DEBUG) fprintf(stderr,
"fiber_pool_stack_free: %p+%"PRIuSIZE
" [base=%p, size=%"PRIuSIZE
"] advice=%d\n", base, size, stack->base, stack->size, advice);
720 #elif VM_CHECK_MODE > 0 && defined(MADV_DONTNEED)
721 if (!advice) advice = MADV_DONTNEED;
723 madvise(base, size, advice);
724 #elif defined(MADV_FREE_REUSABLE)
725 if (!advice) advice = MADV_FREE_REUSABLE;
731 while (madvise(base, size, advice) == -1 &&
errno == EAGAIN);
732 #elif defined(MADV_FREE)
733 if (!advice) advice = MADV_FREE;
735 madvise(base, size, advice);
736 #elif defined(MADV_DONTNEED)
737 if (!advice) advice = MADV_DONTNEED;
739 madvise(base, size, advice);
740 #elif defined(POSIX_MADV_DONTNEED)
741 if (!advice) advice = POSIX_MADV_DONTNEED;
743 posix_madvise(base, size, advice);
744 #elif defined(_WIN32)
745 VirtualAlloc(base, size, MEM_RESET, PAGE_READWRITE);
750 #if defined(COROUTINE_SANITIZE_ADDRESS)
751 __asan_poison_memory_region(fiber_pool_stack_poison_base(stack), fiber_pool_stack_poison_size(stack));
760 struct fiber_pool_vacancy * vacancy = fiber_pool_vacancy_pointer(stack->base, stack->size);
762 if (DEBUG) fprintf(stderr,
"fiber_pool_stack_release: %p used=%"PRIuSIZE
"\n", stack->base, stack->pool->used);
765 vacancy->stack = *stack;
769 fiber_pool_vacancy_reset(vacancy);
772 pool->vacancies = fiber_pool_vacancy_push(vacancy, pool->vacancies);
775 #ifdef FIBER_POOL_ALLOCATION_FREE
778 allocation->used -= 1;
781 if (allocation->used == 0) {
782 fiber_pool_allocation_free(allocation);
784 else if (stack->pool->free_stacks) {
785 fiber_pool_stack_free(&vacancy->stack);
790 if (stack->pool->free_stacks) {
791 fiber_pool_stack_free(&vacancy->stack);
800 #ifdef RUBY_ASAN_ENABLED
801 ec->machine.asan_fake_stack_handle = asan_get_thread_fake_stack_handle();
803 rb_ractor_set_current_ec(th->ractor, th->ec = ec);
810 if (th->vm->ractor.main_thread == th &&
811 rb_signal_buff_size() > 0) {
812 RUBY_VM_SET_TRAP_INTERRUPT(ec);
815 VM_ASSERT(ec->fiber_ptr->cont.self == 0 || ec->vm_stack != NULL);
821 ec_switch(th, fiber);
822 VM_ASSERT(th->ec->fiber_ptr == fiber);
830 #if defined(COROUTINE_SANITIZE_ADDRESS)
840 __sanitizer_finish_switch_fiber(to->fake_stack, (
const void**)&from->stack_base, &from->stack_size);
843 rb_thread_t *thread = fiber->cont.saved_ec.thread_ptr;
845 #ifdef COROUTINE_PTHREAD_CONTEXT
846 ruby_thread_set_native(thread);
849 fiber_restore_thread(thread, fiber);
851 rb_fiber_start(fiber);
853 #ifndef COROUTINE_PTHREAD_CONTEXT
854 VM_UNREACHABLE(fiber_entry);
860 fiber_initialize_coroutine(
rb_fiber_t *fiber,
size_t * vm_stack_size)
864 void * vm_stack = NULL;
868 fiber->stack = fiber_pool_stack_acquire(
fiber_pool);
869 vm_stack = fiber_pool_stack_alloca(&fiber->stack,
fiber_pool->vm_stack_size);
872 coroutine_initialize(&fiber->context, fiber_entry, fiber_pool_stack_base(&fiber->stack), fiber->stack.available);
875 sec->machine.stack_start = fiber->stack.current;
876 sec->machine.stack_maxsize = fiber->stack.available;
878 fiber->context.argument = (
void*)fiber;
890 if (DEBUG) fprintf(stderr,
"fiber_stack_release: %p, stack.base=%p\n", (
void*)fiber, fiber->stack.base);
893 if (fiber->stack.base) {
894 fiber_pool_stack_release(&fiber->stack);
895 fiber->stack.base = NULL;
899 rb_ec_clear_vm_stack(ec);
903 fiber_status_name(
enum fiber_status s)
906 case FIBER_CREATED:
return "created";
907 case FIBER_RESUMED:
return "resumed";
908 case FIBER_SUSPENDED:
return "suspended";
909 case FIBER_TERMINATED:
return "terminated";
911 VM_UNREACHABLE(fiber_status_name);
918 #if VM_CHECK_MODE > 0
919 VM_ASSERT(fiber->cont.saved_ec.fiber_ptr == fiber);
921 switch (fiber->status) {
923 VM_ASSERT(fiber->cont.saved_ec.vm_stack != NULL);
925 case FIBER_SUSPENDED:
926 VM_ASSERT(fiber->cont.saved_ec.vm_stack != NULL);
929 case FIBER_TERMINATED:
933 VM_UNREACHABLE(fiber_verify);
939 fiber_status_set(
rb_fiber_t *fiber,
enum fiber_status s)
942 VM_ASSERT(!FIBER_TERMINATED_P(fiber));
943 VM_ASSERT(fiber->status != s);
964 if (!fiber)
rb_raise(rb_eFiberError,
"uninitialized fiber");
969 NOINLINE(
static VALUE cont_capture(
volatile int *
volatile stat));
971 #define THREAD_MUST_BE_RUNNING(th) do { \
972 if (!(th)->ec->tag) rb_raise(rb_eThreadError, "not running thread"); \
978 return fiber->cont.saved_ec.thread_ptr;
984 return cont->saved_ec.thread_ptr->self;
988 cont_compact(
void *
ptr)
996 rb_execution_context_update(&cont->saved_ec);
1000 cont_mark(
void *
ptr)
1004 RUBY_MARK_ENTER(
"cont");
1010 rb_execution_context_mark(&cont->saved_ec);
1013 if (cont->saved_vm_stack.ptr) {
1014 #ifdef CAPTURE_JUST_VALID_VM_STACK
1016 cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
1019 cont->saved_vm_stack.ptr, cont->saved_ec.stack_size);
1023 if (cont->machine.stack) {
1024 if (cont->type == CONTINUATION_CONTEXT) {
1027 cont->machine.stack + cont->machine.stack_size);
1035 RUBY_MARK_LEAVE(
"cont");
1042 return fiber == fiber->cont.saved_ec.thread_ptr->root_fiber;
1046 static void jit_cont_free(
struct rb_jit_cont *cont);
1049 cont_free(
void *
ptr)
1053 RUBY_FREE_ENTER(
"cont");
1055 if (cont->type == CONTINUATION_CONTEXT) {
1057 RUBY_FREE_UNLESS_NULL(cont->machine.stack);
1061 coroutine_destroy(&fiber->context);
1062 fiber_stack_release(fiber);
1065 RUBY_FREE_UNLESS_NULL(cont->saved_vm_stack.ptr);
1067 VM_ASSERT(cont->jit_cont != NULL);
1068 jit_cont_free(cont->jit_cont);
1071 RUBY_FREE_LEAVE(
"cont");
1075 cont_memsize(
const void *
ptr)
1080 size =
sizeof(*cont);
1081 if (cont->saved_vm_stack.ptr) {
1082 #ifdef CAPTURE_JUST_VALID_VM_STACK
1083 size_t n = (cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
1085 size_t n = cont->saved_ec.vm_stack_size;
1087 size += n *
sizeof(*cont->saved_vm_stack.ptr);
1090 if (cont->machine.stack) {
1091 size += cont->machine.stack_size *
sizeof(*cont->machine.stack);
1100 if (fiber->cont.self) {
1104 rb_execution_context_update(&fiber->cont.saved_ec);
1111 if (fiber->cont.self) {
1115 rb_execution_context_mark(&fiber->cont.saved_ec);
1120 fiber_compact(
void *
ptr)
1125 if (fiber->prev) rb_fiber_update_self(fiber->prev);
1127 cont_compact(&fiber->cont);
1128 fiber_verify(fiber);
1132 fiber_mark(
void *
ptr)
1135 RUBY_MARK_ENTER(
"cont");
1136 fiber_verify(fiber);
1138 if (fiber->prev) rb_fiber_mark_self(fiber->prev);
1139 cont_mark(&fiber->cont);
1140 RUBY_MARK_LEAVE(
"cont");
1144 fiber_free(
void *
ptr)
1147 RUBY_FREE_ENTER(
"fiber");
1149 if (DEBUG) fprintf(stderr,
"fiber_free: %p[%p]\n", (
void *)fiber, fiber->stack.base);
1151 if (fiber->cont.saved_ec.local_storage) {
1152 rb_id_table_free(fiber->cont.saved_ec.local_storage);
1155 cont_free(&fiber->cont);
1156 RUBY_FREE_LEAVE(
"fiber");
1160 fiber_memsize(
const void *
ptr)
1163 size_t size =
sizeof(*fiber);
1165 const rb_thread_t *th = rb_ec_thread_ptr(saved_ec);
1170 if (saved_ec->local_storage && fiber != th->root_fiber) {
1171 size += rb_id_table_memsize(saved_ec->local_storage);
1172 size += rb_obj_memsize_of(saved_ec->storage);
1175 size += cont_memsize(&fiber->cont);
1190 SET_MACHINE_STACK_END(&th->ec->machine.stack_end);
1192 if (th->ec->machine.stack_start > th->ec->machine.stack_end) {
1193 size = cont->machine.stack_size = th->ec->machine.stack_start - th->ec->machine.stack_end;
1194 cont->machine.stack_src = th->ec->machine.stack_end;
1197 size = cont->machine.stack_size = th->ec->machine.stack_end - th->ec->machine.stack_start;
1198 cont->machine.stack_src = th->ec->machine.stack_start;
1201 if (cont->machine.stack) {
1208 FLUSH_REGISTER_WINDOWS;
1209 asan_unpoison_memory_region(cont->machine.stack_src, size,
false);
1210 MEMCPY(cont->machine.stack, cont->machine.stack_src,
VALUE, size);
1215 {cont_mark, cont_free, cont_memsize, cont_compact},
1216 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1224 VM_ASSERT(th->status == THREAD_RUNNABLE);
1231 sec->machine.stack_end = NULL;
1234 static rb_nativethread_lock_t jit_cont_lock;
1252 if (first_jit_cont == NULL) {
1253 cont->next = cont->prev = NULL;
1257 cont->next = first_jit_cont;
1258 first_jit_cont->prev = cont;
1260 first_jit_cont = cont;
1273 if (cont == first_jit_cont) {
1274 first_jit_cont = cont->next;
1275 if (first_jit_cont != NULL)
1276 first_jit_cont->prev = NULL;
1279 cont->prev->next = cont->next;
1280 if (cont->next != NULL)
1281 cont->next->prev = cont->prev;
1290 rb_jit_cont_each_iseq(rb_iseq_callback callback,
void *data)
1293 for (cont = first_jit_cont; cont != NULL; cont = cont->next) {
1294 if (cont->ec->vm_stack == NULL)
1298 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(cont->ec, cfp)) {
1299 if (cfp->pc && cfp->iseq && imemo_type((
VALUE)cfp->iseq) == imemo_iseq) {
1300 callback(cfp->iseq, data);
1302 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1311 rb_yjit_cancel_jit_return(
void *leave_exit,
void *leave_exception)
1314 for (cont = first_jit_cont; cont != NULL; cont = cont->next) {
1315 if (cont->ec->vm_stack == NULL)
1319 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(cont->ec, cfp)) {
1320 if (cfp->jit_return && cfp->jit_return != leave_exception) {
1323 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1331 rb_jit_cont_finish(
void)
1334 for (cont = first_jit_cont; cont != NULL; cont = next) {
1344 VM_ASSERT(cont->jit_cont == NULL);
1346 cont->jit_cont = jit_cont_new(&(cont->saved_ec));
1352 return &fiber->cont.saved_ec;
1359 cont_save_thread(cont, th);
1360 cont->saved_ec.thread_ptr = th;
1361 cont->saved_ec.local_storage = NULL;
1362 cont->saved_ec.local_storage_recursive_hash =
Qnil;
1363 cont->saved_ec.local_storage_recursive_hash_for_trace =
Qnil;
1364 cont_init_jit_cont(cont);
1368 cont_new(
VALUE klass)
1371 volatile VALUE contval;
1374 THREAD_MUST_BE_RUNNING(th);
1376 cont->self = contval;
1377 cont_init(cont, th);
1384 return fiber->cont.self;
1390 return fiber->blocking;
1395 rb_jit_cont_init(
void)
1404 VALUE *p = ec->vm_stack;
1405 while (p < ec->cfp->sp) {
1406 fprintf(stderr,
"%3d ", (
int)(p - ec->vm_stack));
1407 rb_obj_info_dump(*p);
1417 while (cfp != end_of_cfp) {
1420 pc = cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded;
1422 fprintf(stderr,
"%2d pc: %d\n", i++, pc);
1423 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1429 cont_capture(
volatile int *
volatile stat)
1433 volatile VALUE contval;
1436 THREAD_MUST_BE_RUNNING(th);
1437 rb_vm_stack_to_heap(th->ec);
1438 cont = cont_new(rb_cContinuation);
1439 contval = cont->self;
1441 #ifdef CAPTURE_JUST_VALID_VM_STACK
1442 cont->saved_vm_stack.slen = ec->cfp->sp - ec->vm_stack;
1443 cont->saved_vm_stack.clen = ec->vm_stack + ec->vm_stack_size - (
VALUE*)ec->cfp;
1444 cont->saved_vm_stack.ptr =
ALLOC_N(
VALUE, cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
1445 MEMCPY(cont->saved_vm_stack.ptr,
1447 VALUE, cont->saved_vm_stack.slen);
1448 MEMCPY(cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen,
1451 cont->saved_vm_stack.clen);
1453 cont->saved_vm_stack.ptr =
ALLOC_N(
VALUE, ec->vm_stack_size);
1454 MEMCPY(cont->saved_vm_stack.ptr, ec->vm_stack,
VALUE, ec->vm_stack_size);
1457 rb_ec_set_vm_stack(&cont->saved_ec, NULL, 0);
1458 VM_ASSERT(cont->saved_ec.cfp != NULL);
1459 cont_save_machine_stack(th, cont);
1461 if (ruby_setjmp(cont->jmpbuf)) {
1464 VAR_INITIALIZED(cont);
1465 value = cont->value;
1483 if (cont->type == CONTINUATION_CONTEXT) {
1488 if (sec->fiber_ptr != NULL) {
1489 fiber = sec->fiber_ptr;
1491 else if (th->root_fiber) {
1492 fiber = th->root_fiber;
1495 if (fiber && th->ec != &fiber->cont.saved_ec) {
1496 ec_switch(th, fiber);
1499 if (th->ec->trace_arg != sec->trace_arg) {
1504 #ifdef CAPTURE_JUST_VALID_VM_STACK
1506 cont->saved_vm_stack.ptr,
1507 VALUE, cont->saved_vm_stack.slen);
1508 MEMCPY(th->ec->vm_stack + th->ec->vm_stack_size - cont->saved_vm_stack.clen,
1509 cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen,
1510 VALUE, cont->saved_vm_stack.clen);
1512 MEMCPY(th->ec->vm_stack, cont->saved_vm_stack.ptr,
VALUE, sec->vm_stack_size);
1516 th->ec->cfp = sec->cfp;
1517 th->ec->raised_flag = sec->raised_flag;
1518 th->ec->tag = sec->tag;
1519 th->ec->root_lep = sec->root_lep;
1520 th->ec->root_svar = sec->root_svar;
1521 th->ec->errinfo = sec->errinfo;
1523 VM_ASSERT(th->ec->vm_stack != NULL);
1539 if (!FIBER_TERMINATED_P(old_fiber)) {
1540 STACK_GROW_DIR_DETECTION;
1541 SET_MACHINE_STACK_END(&th->ec->machine.stack_end);
1542 if (STACK_DIR_UPPER(0, 1)) {
1543 old_fiber->cont.machine.stack_size = th->ec->machine.stack_start - th->ec->machine.stack_end;
1544 old_fiber->cont.machine.stack = th->ec->machine.stack_end;
1547 old_fiber->cont.machine.stack_size = th->ec->machine.stack_end - th->ec->machine.stack_start;
1548 old_fiber->cont.machine.stack = th->ec->machine.stack_start;
1553 old_fiber->cont.saved_ec.machine.stack_start = th->ec->machine.stack_start;
1554 old_fiber->cont.saved_ec.machine.stack_end = FIBER_TERMINATED_P(old_fiber) ? NULL : th->ec->machine.stack_end;
1559 #if defined(COROUTINE_SANITIZE_ADDRESS)
1560 __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);
1564 struct coroutine_context * from = coroutine_transfer(&old_fiber->context, &new_fiber->context);
1566 #if defined(COROUTINE_SANITIZE_ADDRESS)
1567 __sanitizer_finish_switch_fiber(old_fiber->context.fake_stack, NULL, NULL);
1575 fiber_restore_thread(th, old_fiber);
1581 NOINLINE(NORETURN(
static void cont_restore_1(
rb_context_t *)));
1586 cont_restore_thread(cont);
1589 #if defined(_M_AMD64) && !defined(__MINGW64__)
1594 _JUMP_BUFFER *bp = (
void*)&cont->jmpbuf;
1595 bp->Frame = ((_JUMP_BUFFER*)((
void*)&buf))->Frame;
1598 if (cont->machine.stack_src) {
1599 FLUSH_REGISTER_WINDOWS;
1600 MEMCPY(cont->machine.stack_src, cont->machine.stack,
1601 VALUE, cont->machine.stack_size);
1604 ruby_longjmp(cont->jmpbuf, 1);
1612 if (cont->machine.stack_src) {
1614 #define STACK_PAD_SIZE 1
1616 #define STACK_PAD_SIZE 1024
1618 VALUE space[STACK_PAD_SIZE];
1620 #if !STACK_GROW_DIRECTION
1621 if (addr_in_prev_frame > &space[0]) {
1624 #if STACK_GROW_DIRECTION <= 0
1625 volatile VALUE *
const end = cont->machine.stack_src;
1626 if (&space[0] > end) {
1635 cont_restore_0(cont, &space[0]);
1639 #if !STACK_GROW_DIRECTION
1644 #if STACK_GROW_DIRECTION >= 0
1645 volatile VALUE *
const end = cont->machine.stack_src + cont->machine.stack_size;
1646 if (&space[STACK_PAD_SIZE] < end) {
1651 cont_restore_0(cont, &space[STACK_PAD_SIZE-1]);
1655 #if !STACK_GROW_DIRECTION
1659 cont_restore_1(cont);
1746 rb_callcc(
VALUE self)
1748 volatile int called;
1749 volatile VALUE val = cont_capture(&called);
1758 #ifdef RUBY_ASAN_ENABLED
1761 MAYBE_UNUSED(
static void notusing_callcc(
void)) { rb_callcc(
Qnil); }
1762 # define rb_callcc rb_f_notimplement
1767 make_passing_arg(
int argc,
const VALUE *argv)
1783 NORETURN(
static VALUE rb_cont_call(
int argc,
VALUE *argv,
VALUE contval));
1801 rb_cont_call(
int argc,
VALUE *argv,
VALUE contval)
1806 if (cont_thread_value(cont) != th->self) {
1809 if (cont->saved_ec.fiber_ptr) {
1810 if (th->ec->fiber_ptr != cont->saved_ec.fiber_ptr) {
1816 cont->value = make_passing_arg(argc, argv);
1818 cont_restore_0(cont, &contval);
1911 {fiber_mark, fiber_free, fiber_memsize, fiber_compact,},
1912 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1916 fiber_alloc(
VALUE klass)
1922 fiber_t_alloc(
VALUE fiber_value,
unsigned int blocking)
1931 THREAD_MUST_BE_RUNNING(th);
1933 fiber->cont.self = fiber_value;
1934 fiber->cont.type = FIBER_CONTEXT;
1935 fiber->blocking = blocking;
1937 cont_init(&fiber->cont, th);
1939 fiber->cont.saved_ec.fiber_ptr = fiber;
1940 rb_ec_clear_vm_stack(&fiber->cont.saved_ec);
1946 VM_ASSERT(FIBER_CREATED_P(fiber));
1956 VALUE fiber_value = fiber_alloc(rb_cFiber);
1959 VM_ASSERT(
DATA_PTR(fiber_value) == NULL);
1960 VM_ASSERT(fiber->cont.type == FIBER_CONTEXT);
1961 VM_ASSERT(FIBER_RESUMED_P(fiber));
1963 th->root_fiber = fiber;
1965 fiber->cont.self = fiber_value;
1967 coroutine_initialize_main(&fiber->context);
1976 if (ec->fiber_ptr->cont.self == 0) {
1977 root_fiber_alloc(rb_ec_thread_ptr(ec));
1979 return ec->fiber_ptr;
1983 current_fiber_storage(
void)
1990 inherit_fiber_storage(
void)
1998 fiber->cont.saved_ec.storage = storage;
2002 fiber_storage_get(
rb_fiber_t *fiber,
int allocate)
2004 VALUE storage = fiber->cont.saved_ec.storage;
2005 if (storage ==
Qnil && allocate) {
2007 fiber_storage_set(fiber, storage);
2013 storage_access_must_be_from_same_fiber(
VALUE self)
2017 if (fiber != current) {
2029 rb_fiber_storage_get(
VALUE self)
2031 storage_access_must_be_from_same_fiber(
self);
2033 VALUE storage = fiber_storage_get(fiber_ptr(
self), FALSE);
2035 if (storage ==
Qnil) {
2052 fiber_storage_validate(
VALUE value)
2055 if (value ==
Qnil)
return;
2091 rb_fiber_storage_set(
VALUE self,
VALUE value)
2095 "Fiber#storage= is experimental and may be removed in the future!");
2098 storage_access_must_be_from_same_fiber(
self);
2099 fiber_storage_validate(value);
2101 fiber_ptr(
self)->cont.saved_ec.storage =
rb_obj_dup(value);
2116 rb_fiber_storage_aref(
VALUE class,
VALUE key)
2120 VALUE storage = fiber_storage_get(fiber_current(), FALSE);
2141 VALUE storage = fiber_storage_get(fiber_current(), value !=
Qnil);
2144 if (value ==
Qnil) {
2157 storage = inherit_fiber_storage();
2160 fiber_storage_validate(storage);
2164 rb_fiber_t *fiber = fiber_t_alloc(
self, blocking);
2166 fiber->cont.saved_ec.storage = storage;
2167 fiber->first_proc = proc;
2168 fiber->stack.base = NULL;
2180 size_t vm_stack_size = 0;
2181 VALUE *vm_stack = fiber_initialize_coroutine(fiber, &vm_stack_size);
2184 cont->saved_vm_stack.ptr = NULL;
2185 rb_ec_initialize_vm_stack(sec, vm_stack, vm_stack_size /
sizeof(
VALUE));
2188 sec->local_storage = NULL;
2189 sec->local_storage_recursive_hash =
Qnil;
2190 sec->local_storage_recursive_hash_for_trace =
Qnil;
2194 rb_fiber_pool_default(
VALUE pool)
2196 return &shared_fiber_pool;
2202 fiber->cont.saved_ec.storage = storage;
2208 rb_fiber_initialize_kw(
int argc,
VALUE* argv,
VALUE self,
int kw_splat)
2219 rb_get_kwargs(options, fiber_initialize_keywords, 0, 3, arguments);
2221 if (!UNDEF_P(arguments[0])) {
2222 blocking = arguments[0];
2225 if (!UNDEF_P(arguments[1])) {
2226 pool = arguments[1];
2229 storage = arguments[2];
2232 return fiber_initialize(
self,
rb_block_proc(), rb_fiber_pool_default(pool),
RTEST(blocking), storage);
2285 rb_fiber_initialize(
int argc,
VALUE* argv,
VALUE self)
2293 return fiber_initialize(fiber_alloc(rb_cFiber),
rb_proc_new(func, obj), rb_fiber_pool_default(
Qnil), 0, storage);
2303 rb_fiber_s_schedule_kw(
int argc,
VALUE* argv,
int kw_splat)
2306 VALUE scheduler = th->scheduler;
2309 if (scheduler !=
Qnil) {
2361 rb_fiber_s_schedule(
int argc,
VALUE *argv,
VALUE obj)
2377 rb_fiber_s_scheduler(
VALUE klass)
2391 rb_fiber_current_scheduler(
VALUE klass)
2413 rb_fiber_set_scheduler(
VALUE klass,
VALUE scheduler)
2418 NORETURN(
static void rb_fiber_terminate(
rb_fiber_t *fiber,
int need_interrupt,
VALUE err));
2423 rb_thread_t *
volatile th = fiber->cont.saved_ec.thread_ptr;
2426 enum ruby_tag_type state;
2428 VM_ASSERT(th->ec == GET_EC());
2429 VM_ASSERT(FIBER_RESUMED_P(fiber));
2431 if (fiber->blocking) {
2435 EC_PUSH_TAG(th->ec);
2436 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2439 const VALUE *argv, args = cont->value;
2440 GetProcPtr(fiber->first_proc, proc);
2443 th->ec->errinfo =
Qnil;
2444 th->ec->root_lep = rb_vm_proc_local_ep(fiber->first_proc);
2445 th->ec->root_svar =
Qfalse;
2448 cont->value = rb_vm_invoke_proc(th->ec, proc, argc, argv, cont->kw_splat, VM_BLOCK_HANDLER_NONE);
2452 int need_interrupt = TRUE;
2455 err = th->ec->errinfo;
2456 VM_ASSERT(FIBER_RESUMED_P(fiber));
2458 if (state == TAG_RAISE) {
2461 else if (state == TAG_FATAL && err == RUBY_FATAL_FIBER_KILLED) {
2462 need_interrupt = FALSE;
2465 else if (state == TAG_FATAL) {
2466 rb_threadptr_pending_interrupt_enque(th, err);
2469 err = rb_vm_make_jump_tag_but_local_jump(state, err);
2473 rb_fiber_terminate(fiber, need_interrupt, err);
2484 fiber->cont.type = FIBER_CONTEXT;
2485 fiber->cont.saved_ec.fiber_ptr = fiber;
2486 fiber->cont.saved_ec.thread_ptr = th;
2487 fiber->blocking = 1;
2489 fiber_status_set(fiber, FIBER_RESUMED);
2490 th->ec = &fiber->cont.saved_ec;
2491 cont_init_jit_cont(&fiber->cont);
2497 if (th->root_fiber) {
2503 VM_ASSERT(th->ec->fiber_ptr->cont.type == FIBER_CONTEXT);
2504 VM_ASSERT(th->ec->fiber_ptr->cont.self == 0);
2506 if (ec && th->ec == ec) {
2507 rb_ractor_set_current_ec(th->ractor, NULL);
2509 fiber_free(th->ec->fiber_ptr);
2515 rb_threadptr_root_fiber_terminate(
rb_thread_t *th)
2519 fiber->status = FIBER_TERMINATED;
2522 rb_ec_clear_vm_stack(th->ec);
2526 return_fiber(
bool terminate)
2533 prev->resuming_fiber = NULL;
2538 rb_raise(rb_eFiberError,
"attempt to yield on a not resumed fiber");
2544 VM_ASSERT(root_fiber != NULL);
2547 for (fiber = root_fiber; fiber->resuming_fiber; fiber = fiber->resuming_fiber) {
2557 return fiber_current()->cont.self;
2566 if (th->ec->fiber_ptr != NULL) {
2567 fiber = th->ec->fiber_ptr;
2571 fiber = root_fiber_alloc(th);
2574 if (FIBER_CREATED_P(next_fiber)) {
2575 fiber_prepare_stack(next_fiber);
2578 VM_ASSERT(FIBER_RESUMED_P(fiber) || FIBER_TERMINATED_P(fiber));
2579 VM_ASSERT(FIBER_RUNNABLE_P(next_fiber));
2581 if (FIBER_RESUMED_P(fiber)) fiber_status_set(fiber, FIBER_SUSPENDED);
2583 fiber_status_set(next_fiber, FIBER_RESUMED);
2584 fiber_setcontext(next_fiber, fiber);
2590 VM_ASSERT(fiber == fiber_current());
2592 if (fiber->killed) {
2593 rb_thread_t *thread = fiber->cont.saved_ec.thread_ptr;
2595 thread->ec->errinfo = RUBY_FATAL_FIBER_KILLED;
2596 EC_JUMP_TAG(thread->ec, RUBY_TAG_FATAL);
2608 if (th->root_fiber == NULL) root_fiber_alloc(th);
2610 if (th->ec->fiber_ptr == fiber) {
2614 return make_passing_arg(argc, argv);
2617 if (cont_thread_value(cont) != th->self) {
2618 rb_raise(rb_eFiberError,
"fiber called across threads");
2621 if (FIBER_TERMINATED_P(fiber)) {
2622 value =
rb_exc_new2(rb_eFiberError,
"dead fiber called");
2624 if (!FIBER_TERMINATED_P(th->ec->fiber_ptr)) {
2626 VM_UNREACHABLE(fiber_switch);
2632 VM_ASSERT(FIBER_SUSPENDED_P(th->root_fiber));
2634 cont = &th->root_fiber->cont;
2636 cont->value = value;
2638 fiber_setcontext(th->root_fiber, th->ec->fiber_ptr);
2640 VM_UNREACHABLE(fiber_switch);
2644 VM_ASSERT(FIBER_RUNNABLE_P(fiber));
2648 VM_ASSERT(!current_fiber->resuming_fiber);
2650 if (resuming_fiber) {
2651 current_fiber->resuming_fiber = resuming_fiber;
2652 fiber->prev = fiber_current();
2653 fiber->yielding = 0;
2656 VM_ASSERT(!current_fiber->yielding);
2658 current_fiber->yielding = 1;
2661 if (current_fiber->blocking) {
2666 cont->kw_splat = kw_splat;
2667 cont->value = make_passing_arg(argc, argv);
2669 fiber_store(fiber, th);
2672 #ifndef COROUTINE_PTHREAD_CONTEXT
2673 if (resuming_fiber && FIBER_TERMINATED_P(fiber)) {
2674 fiber_stack_release(fiber);
2678 if (fiber_current()->blocking) {
2682 RUBY_VM_CHECK_INTS(th->ec);
2686 current_fiber = th->ec->fiber_ptr;
2687 value = current_fiber->cont.value;
2689 fiber_check_killed(current_fiber);
2691 if (current_fiber->cont.argc == -1) {
2702 return fiber_switch(fiber_ptr(fiber_value), argc, argv,
RB_NO_KEYWORDS, NULL,
false);
2720 rb_fiber_blocking_p(
VALUE fiber)
2722 return RBOOL(fiber_ptr(fiber)->blocking);
2726 fiber_blocking_yield(
VALUE fiber_value)
2729 rb_thread_t *
volatile th = fiber->cont.saved_ec.thread_ptr;
2731 VM_ASSERT(fiber->blocking == 0);
2734 fiber->blocking = 1;
2743 fiber_blocking_ensure(
VALUE fiber_value)
2746 rb_thread_t *
volatile th = fiber->cont.saved_ec.thread_ptr;
2749 fiber->blocking = 0;
2766 rb_fiber_blocking(
VALUE class)
2772 if (fiber->blocking) {
2776 return rb_ensure(fiber_blocking_yield, fiber_value, fiber_blocking_ensure, fiber_value);
2799 rb_fiber_s_blocking_p(
VALUE klass)
2802 unsigned blocking = thread->blocking;
2813 fiber_status_set(fiber, FIBER_TERMINATED);
2817 rb_fiber_terminate(
rb_fiber_t *fiber,
int need_interrupt,
VALUE error)
2819 VALUE value = fiber->cont.value;
2821 VM_ASSERT(FIBER_RESUMED_P(fiber));
2822 rb_fiber_close(fiber);
2824 fiber->cont.machine.stack = NULL;
2825 fiber->cont.machine.stack_size = 0;
2829 if (need_interrupt) RUBY_VM_SET_INTERRUPT(&next_fiber->cont.saved_ec);
2832 fiber_switch(next_fiber, -1, &error,
RB_NO_KEYWORDS, NULL,
false);
2834 fiber_switch(next_fiber, 1, &value,
RB_NO_KEYWORDS, NULL,
false);
2839 fiber_resume_kw(
rb_fiber_t *fiber,
int argc,
const VALUE *argv,
int kw_splat)
2843 if (argc == -1 && FIBER_CREATED_P(fiber)) {
2844 rb_raise(rb_eFiberError,
"cannot raise exception on unborn fiber");
2846 else if (FIBER_TERMINATED_P(fiber)) {
2847 rb_raise(rb_eFiberError,
"attempt to resume a terminated fiber");
2849 else if (fiber == current_fiber) {
2850 rb_raise(rb_eFiberError,
"attempt to resume the current fiber");
2852 else if (fiber->prev != NULL) {
2853 rb_raise(rb_eFiberError,
"attempt to resume a resumed fiber (double resume)");
2855 else if (fiber->resuming_fiber) {
2856 rb_raise(rb_eFiberError,
"attempt to resume a resuming fiber");
2858 else if (fiber->prev == NULL &&
2859 (!fiber->yielding && fiber->status != FIBER_CREATED)) {
2860 rb_raise(rb_eFiberError,
"attempt to resume a transferring fiber");
2863 return fiber_switch(fiber, argc, argv, kw_splat, fiber,
false);
2869 return fiber_resume_kw(fiber_ptr(
self), argc, argv, kw_splat);
2875 return fiber_resume_kw(fiber_ptr(
self), argc, argv,
RB_NO_KEYWORDS);
2881 return fiber_switch(return_fiber(
false), argc, argv, kw_splat, NULL,
true);
2887 return fiber_switch(return_fiber(
false), argc, argv,
RB_NO_KEYWORDS, NULL,
true);
2891 rb_fiber_reset_root_local_storage(
rb_thread_t *th)
2893 if (th->root_fiber && th->root_fiber != th->ec->fiber_ptr) {
2894 th->ec->local_storage = th->root_fiber->cont.saved_ec.local_storage;
2909 return RBOOL(!FIBER_TERMINATED_P(fiber_ptr(fiber_value)));
2928 rb_fiber_m_resume(
int argc,
VALUE *argv,
VALUE fiber)
2980 rb_fiber_backtrace(
int argc,
VALUE *argv,
VALUE fiber)
2982 return rb_vm_backtrace(argc, argv, &fiber_ptr(fiber)->cont.saved_ec);
3005 rb_fiber_backtrace_locations(
int argc,
VALUE *argv,
VALUE fiber)
3007 return rb_vm_backtrace_locations(argc, argv, &fiber_ptr(fiber)->cont.saved_ec);
3093 rb_fiber_m_transfer(
int argc,
VALUE *argv,
VALUE self)
3099 fiber_transfer_kw(
rb_fiber_t *fiber,
int argc,
const VALUE *argv,
int kw_splat)
3101 if (fiber->resuming_fiber) {
3102 rb_raise(rb_eFiberError,
"attempt to transfer to a resuming fiber");
3105 if (fiber->yielding) {
3106 rb_raise(rb_eFiberError,
"attempt to transfer to a yielding fiber");
3109 return fiber_switch(fiber, argc, argv, kw_splat, NULL,
false);
3115 return fiber_transfer_kw(fiber_ptr(
self), argc, argv, kw_splat);
3129 rb_fiber_s_yield(
int argc,
VALUE *argv,
VALUE klass)
3137 if (fiber == fiber_current()) {
3140 else if (fiber->resuming_fiber) {
3141 return fiber_raise(fiber->resuming_fiber, exception);
3143 else if (FIBER_SUSPENDED_P(fiber) && !fiber->yielding) {
3156 return fiber_raise(fiber_ptr(fiber), exception);
3185 rb_fiber_m_raise(
int argc,
VALUE *argv,
VALUE self)
3208 rb_fiber_m_kill(
VALUE self)
3212 if (fiber->killed)
return Qfalse;
3215 if (fiber->status == FIBER_CREATED) {
3216 fiber->status = FIBER_TERMINATED;
3218 else if (fiber->status != FIBER_TERMINATED) {
3219 if (fiber_current() == fiber) {
3220 fiber_check_killed(fiber);
3223 fiber_raise(fiber_ptr(
self),
Qnil);
3238 rb_fiber_s_current(
VALUE klass)
3244 fiber_to_s(
VALUE fiber_value)
3246 const rb_fiber_t *fiber = fiber_ptr(fiber_value);
3248 char status_info[0x20];
3250 if (fiber->resuming_fiber) {
3251 snprintf(status_info, 0x20,
" (%s by resuming)", fiber_status_name(fiber->status));
3254 snprintf(status_info, 0x20,
" (%s)", fiber_status_name(fiber->status));
3259 strlcat(status_info,
">",
sizeof(status_info));
3264 GetProcPtr(fiber->first_proc, proc);
3265 return rb_block_to_s(fiber_value, &proc->block, status_info);
3268 #ifdef HAVE_WORKING_FORK
3272 if (th->root_fiber) {
3273 if (&th->root_fiber->cont.saved_ec != th->ec) {
3274 th->root_fiber = th->ec->fiber_ptr;
3276 th->root_fiber->prev = 0;
3281 #ifdef RB_EXPERIMENTAL_FIBER_POOL
3283 fiber_pool_free(
void *
ptr)
3286 RUBY_FREE_ENTER(
"fiber_pool");
3288 fiber_pool_allocation_free(
fiber_pool->allocations);
3291 RUBY_FREE_LEAVE(
"fiber_pool");
3295 fiber_pool_memsize(
const void *
ptr)
3298 size_t size =
sizeof(*fiber_pool);
3307 {NULL, fiber_pool_free, fiber_pool_memsize,},
3308 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3312 fiber_pool_alloc(
VALUE klass)
3320 rb_fiber_pool_initialize(
int argc,
VALUE* argv,
VALUE self)
3327 rb_scan_args(argc, argv,
"03", &size, &count, &vm_stack_size);
3330 size =
SIZET2NUM(th->vm->default_params.fiber_machine_stack_size);
3337 if (
NIL_P(vm_stack_size)) {
3338 vm_stack_size =
SIZET2NUM(th->vm->default_params.fiber_vm_stack_size);
3366 size_t vm_stack_size = th->vm->default_params.fiber_vm_stack_size;
3367 size_t machine_stack_size = th->vm->default_params.fiber_machine_stack_size;
3368 size_t stack_size = machine_stack_size + vm_stack_size;
3372 GetSystemInfo(&info);
3373 pagesize = info.dwPageSize;
3375 pagesize = sysconf(_SC_PAGESIZE);
3377 SET_MACHINE_STACK_END(&th->ec->machine.stack_end);
3379 fiber_pool_initialize(&shared_fiber_pool, stack_size, FIBER_POOL_INITIAL_SIZE, vm_stack_size);
3385 const char *fiber_shared_fiber_pool_free_stacks = getenv(
"RUBY_SHARED_FIBER_POOL_FREE_STACKS");
3386 if (fiber_shared_fiber_pool_free_stacks) {
3387 shared_fiber_pool.free_stacks = atoi(fiber_shared_fiber_pool_free_stacks);
3389 if (shared_fiber_pool.free_stacks < 0) {
3390 rb_warn(
"Setting RUBY_SHARED_FIBER_POOL_FREE_STACKS to a negative value is not allowed.");
3391 shared_fiber_pool.free_stacks = 0;
3394 if (shared_fiber_pool.free_stacks > 1) {
3395 rb_warn(
"Setting RUBY_SHARED_FIBER_POOL_FREE_STACKS to a value greater than 1 is operating system specific, and may cause crashes.");
3416 rb_define_method(rb_cFiber,
"backtrace_locations", rb_fiber_backtrace_locations, -1);
3429 #ifdef RB_EXPERIMENTAL_FIBER_POOL
3432 rb_define_method(rb_cFiberPool,
"initialize", rb_fiber_pool_initialize, -1);
3438 RUBY_SYMBOL_EXPORT_BEGIN
3441 ruby_Init_Continuation_body(
void)
3451 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.