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);
831 #if defined(COROUTINE_SANITIZE_ADDRESS)
841 __sanitizer_finish_switch_fiber(to->fake_stack, (
const void**)&from->stack_base, &from->stack_size);
844 rb_thread_t *thread = fiber->cont.saved_ec.thread_ptr;
846 #ifdef COROUTINE_PTHREAD_CONTEXT
847 ruby_thread_set_native(thread);
850 fiber_restore_thread(thread, fiber);
852 rb_fiber_start(fiber);
854 #ifndef COROUTINE_PTHREAD_CONTEXT
855 VM_UNREACHABLE(fiber_entry);
861 fiber_initialize_coroutine(
rb_fiber_t *fiber,
size_t * vm_stack_size)
865 void * vm_stack = NULL;
869 fiber->stack = fiber_pool_stack_acquire(
fiber_pool);
870 vm_stack = fiber_pool_stack_alloca(&fiber->stack,
fiber_pool->vm_stack_size);
873 coroutine_initialize(&fiber->context, fiber_entry, fiber_pool_stack_base(&fiber->stack), fiber->stack.available);
876 sec->machine.stack_start = fiber->stack.current;
877 sec->machine.stack_maxsize = fiber->stack.available;
879 fiber->context.argument = (
void*)fiber;
891 if (DEBUG) fprintf(stderr,
"fiber_stack_release: %p, stack.base=%p\n", (
void*)fiber, fiber->stack.base);
894 if (fiber->stack.base) {
895 fiber_pool_stack_release(&fiber->stack);
896 fiber->stack.base = NULL;
900 rb_ec_clear_vm_stack(ec);
904 fiber_status_name(
enum fiber_status s)
907 case FIBER_CREATED:
return "created";
908 case FIBER_RESUMED:
return "resumed";
909 case FIBER_SUSPENDED:
return "suspended";
910 case FIBER_TERMINATED:
return "terminated";
912 VM_UNREACHABLE(fiber_status_name);
919 #if VM_CHECK_MODE > 0
920 VM_ASSERT(fiber->cont.saved_ec.fiber_ptr == fiber);
922 switch (fiber->status) {
924 VM_ASSERT(fiber->cont.saved_ec.vm_stack != NULL);
926 case FIBER_SUSPENDED:
927 VM_ASSERT(fiber->cont.saved_ec.vm_stack != NULL);
930 case FIBER_TERMINATED:
934 VM_UNREACHABLE(fiber_verify);
940 fiber_status_set(
rb_fiber_t *fiber,
enum fiber_status s)
943 VM_ASSERT(!FIBER_TERMINATED_P(fiber));
944 VM_ASSERT(fiber->status != s);
965 if (!fiber)
rb_raise(rb_eFiberError,
"uninitialized fiber");
970 NOINLINE(
static VALUE cont_capture(
volatile int *
volatile stat));
972 #define THREAD_MUST_BE_RUNNING(th) do { \
973 if (!(th)->ec->tag) rb_raise(rb_eThreadError, "not running thread"); \
979 return fiber->cont.saved_ec.thread_ptr;
985 return cont->saved_ec.thread_ptr->self;
989 cont_compact(
void *
ptr)
997 rb_execution_context_update(&cont->saved_ec);
1001 cont_mark(
void *
ptr)
1005 RUBY_MARK_ENTER(
"cont");
1011 rb_execution_context_mark(&cont->saved_ec);
1014 if (cont->saved_vm_stack.ptr) {
1015 #ifdef CAPTURE_JUST_VALID_VM_STACK
1017 cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
1020 cont->saved_vm_stack.ptr, cont->saved_ec.stack_size);
1024 if (cont->machine.stack) {
1025 if (cont->type == CONTINUATION_CONTEXT) {
1028 cont->machine.stack + cont->machine.stack_size);
1036 RUBY_MARK_LEAVE(
"cont");
1043 return fiber == fiber->cont.saved_ec.thread_ptr->root_fiber;
1047 static void jit_cont_free(
struct rb_jit_cont *cont);
1050 cont_free(
void *
ptr)
1054 RUBY_FREE_ENTER(
"cont");
1056 if (cont->type == CONTINUATION_CONTEXT) {
1058 RUBY_FREE_UNLESS_NULL(cont->machine.stack);
1062 coroutine_destroy(&fiber->context);
1063 fiber_stack_release(fiber);
1066 RUBY_FREE_UNLESS_NULL(cont->saved_vm_stack.ptr);
1068 VM_ASSERT(cont->jit_cont != NULL);
1069 jit_cont_free(cont->jit_cont);
1072 RUBY_FREE_LEAVE(
"cont");
1076 cont_memsize(
const void *
ptr)
1081 size =
sizeof(*cont);
1082 if (cont->saved_vm_stack.ptr) {
1083 #ifdef CAPTURE_JUST_VALID_VM_STACK
1084 size_t n = (cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
1086 size_t n = cont->saved_ec.vm_stack_size;
1088 size += n *
sizeof(*cont->saved_vm_stack.ptr);
1091 if (cont->machine.stack) {
1092 size += cont->machine.stack_size *
sizeof(*cont->machine.stack);
1101 if (fiber->cont.self) {
1105 rb_execution_context_update(&fiber->cont.saved_ec);
1112 if (fiber->cont.self) {
1116 rb_execution_context_mark(&fiber->cont.saved_ec);
1121 fiber_compact(
void *
ptr)
1126 if (fiber->prev) rb_fiber_update_self(fiber->prev);
1128 cont_compact(&fiber->cont);
1129 fiber_verify(fiber);
1133 fiber_mark(
void *
ptr)
1136 RUBY_MARK_ENTER(
"cont");
1137 fiber_verify(fiber);
1139 if (fiber->prev) rb_fiber_mark_self(fiber->prev);
1140 cont_mark(&fiber->cont);
1141 RUBY_MARK_LEAVE(
"cont");
1145 fiber_free(
void *
ptr)
1148 RUBY_FREE_ENTER(
"fiber");
1150 if (DEBUG) fprintf(stderr,
"fiber_free: %p[%p]\n", (
void *)fiber, fiber->stack.base);
1152 if (fiber->cont.saved_ec.local_storage) {
1153 rb_id_table_free(fiber->cont.saved_ec.local_storage);
1156 cont_free(&fiber->cont);
1157 RUBY_FREE_LEAVE(
"fiber");
1161 fiber_memsize(
const void *
ptr)
1164 size_t size =
sizeof(*fiber);
1166 const rb_thread_t *th = rb_ec_thread_ptr(saved_ec);
1171 if (saved_ec->local_storage && fiber != th->root_fiber) {
1172 size += rb_id_table_memsize(saved_ec->local_storage);
1173 size += rb_obj_memsize_of(saved_ec->storage);
1176 size += cont_memsize(&fiber->cont);
1191 SET_MACHINE_STACK_END(&th->ec->machine.stack_end);
1193 if (th->ec->machine.stack_start > th->ec->machine.stack_end) {
1194 size = cont->machine.stack_size = th->ec->machine.stack_start - th->ec->machine.stack_end;
1195 cont->machine.stack_src = th->ec->machine.stack_end;
1198 size = cont->machine.stack_size = th->ec->machine.stack_end - th->ec->machine.stack_start;
1199 cont->machine.stack_src = th->ec->machine.stack_start;
1202 if (cont->machine.stack) {
1209 FLUSH_REGISTER_WINDOWS;
1210 asan_unpoison_memory_region(cont->machine.stack_src, size,
false);
1211 MEMCPY(cont->machine.stack, cont->machine.stack_src,
VALUE, size);
1216 {cont_mark, cont_free, cont_memsize, cont_compact},
1217 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1225 VM_ASSERT(th->status == THREAD_RUNNABLE);
1232 sec->machine.stack_end = NULL;
1235 static rb_nativethread_lock_t jit_cont_lock;
1253 if (first_jit_cont == NULL) {
1254 cont->next = cont->prev = NULL;
1258 cont->next = first_jit_cont;
1259 first_jit_cont->prev = cont;
1261 first_jit_cont = cont;
1274 if (cont == first_jit_cont) {
1275 first_jit_cont = cont->next;
1276 if (first_jit_cont != NULL)
1277 first_jit_cont->prev = NULL;
1280 cont->prev->next = cont->next;
1281 if (cont->next != NULL)
1282 cont->next->prev = cont->prev;
1291 rb_jit_cont_each_iseq(rb_iseq_callback callback,
void *data)
1294 for (cont = first_jit_cont; cont != NULL; cont = cont->next) {
1295 if (cont->ec->vm_stack == NULL)
1299 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(cont->ec, cfp)) {
1300 if (cfp->pc && cfp->iseq && imemo_type((
VALUE)cfp->iseq) == imemo_iseq) {
1301 callback(cfp->iseq, data);
1303 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1312 rb_yjit_cancel_jit_return(
void *leave_exit,
void *leave_exception)
1315 for (cont = first_jit_cont; cont != NULL; cont = cont->next) {
1316 if (cont->ec->vm_stack == NULL)
1320 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(cont->ec, cfp)) {
1321 if (cfp->jit_return && cfp->jit_return != leave_exception) {
1324 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1332 rb_jit_cont_finish(
void)
1335 for (cont = first_jit_cont; cont != NULL; cont = next) {
1345 VM_ASSERT(cont->jit_cont == NULL);
1347 cont->jit_cont = jit_cont_new(&(cont->saved_ec));
1353 return &fiber->cont.saved_ec;
1360 cont_save_thread(cont, th);
1361 cont->saved_ec.thread_ptr = th;
1362 cont->saved_ec.local_storage = NULL;
1363 cont->saved_ec.local_storage_recursive_hash =
Qnil;
1364 cont->saved_ec.local_storage_recursive_hash_for_trace =
Qnil;
1365 cont_init_jit_cont(cont);
1369 cont_new(
VALUE klass)
1372 volatile VALUE contval;
1375 THREAD_MUST_BE_RUNNING(th);
1377 cont->self = contval;
1378 cont_init(cont, th);
1385 return fiber->cont.self;
1391 return fiber->blocking;
1396 rb_jit_cont_init(
void)
1405 VALUE *p = ec->vm_stack;
1406 while (p < ec->cfp->sp) {
1407 fprintf(stderr,
"%3d ", (
int)(p - ec->vm_stack));
1408 rb_obj_info_dump(*p);
1418 while (cfp != end_of_cfp) {
1421 pc = cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded;
1423 fprintf(stderr,
"%2d pc: %d\n", i++, pc);
1424 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1430 cont_capture(
volatile int *
volatile stat)
1434 volatile VALUE contval;
1437 THREAD_MUST_BE_RUNNING(th);
1438 rb_vm_stack_to_heap(th->ec);
1439 cont = cont_new(rb_cContinuation);
1440 contval = cont->self;
1442 #ifdef CAPTURE_JUST_VALID_VM_STACK
1443 cont->saved_vm_stack.slen = ec->cfp->sp - ec->vm_stack;
1444 cont->saved_vm_stack.clen = ec->vm_stack + ec->vm_stack_size - (
VALUE*)ec->cfp;
1445 cont->saved_vm_stack.ptr =
ALLOC_N(
VALUE, cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
1446 MEMCPY(cont->saved_vm_stack.ptr,
1448 VALUE, cont->saved_vm_stack.slen);
1449 MEMCPY(cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen,
1452 cont->saved_vm_stack.clen);
1454 cont->saved_vm_stack.ptr =
ALLOC_N(
VALUE, ec->vm_stack_size);
1455 MEMCPY(cont->saved_vm_stack.ptr, ec->vm_stack,
VALUE, ec->vm_stack_size);
1458 rb_ec_set_vm_stack(&cont->saved_ec, NULL, 0);
1459 VM_ASSERT(cont->saved_ec.cfp != NULL);
1460 cont_save_machine_stack(th, cont);
1462 if (ruby_setjmp(cont->jmpbuf)) {
1465 VAR_INITIALIZED(cont);
1466 value = cont->value;
1484 if (cont->type == CONTINUATION_CONTEXT) {
1489 if (sec->fiber_ptr != NULL) {
1490 fiber = sec->fiber_ptr;
1492 else if (th->root_fiber) {
1493 fiber = th->root_fiber;
1496 if (fiber && th->ec != &fiber->cont.saved_ec) {
1497 ec_switch(th, fiber);
1500 if (th->ec->trace_arg != sec->trace_arg) {
1505 #ifdef CAPTURE_JUST_VALID_VM_STACK
1507 cont->saved_vm_stack.ptr,
1508 VALUE, cont->saved_vm_stack.slen);
1509 MEMCPY(th->ec->vm_stack + th->ec->vm_stack_size - cont->saved_vm_stack.clen,
1510 cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen,
1511 VALUE, cont->saved_vm_stack.clen);
1513 MEMCPY(th->ec->vm_stack, cont->saved_vm_stack.ptr,
VALUE, sec->vm_stack_size);
1517 th->ec->cfp = sec->cfp;
1518 th->ec->raised_flag = sec->raised_flag;
1519 th->ec->tag = sec->tag;
1520 th->ec->root_lep = sec->root_lep;
1521 th->ec->root_svar = sec->root_svar;
1522 th->ec->errinfo = sec->errinfo;
1524 VM_ASSERT(th->ec->vm_stack != NULL);
1540 if (!FIBER_TERMINATED_P(old_fiber)) {
1541 STACK_GROW_DIR_DETECTION;
1542 SET_MACHINE_STACK_END(&th->ec->machine.stack_end);
1543 if (STACK_DIR_UPPER(0, 1)) {
1544 old_fiber->cont.machine.stack_size = th->ec->machine.stack_start - th->ec->machine.stack_end;
1545 old_fiber->cont.machine.stack = th->ec->machine.stack_end;
1548 old_fiber->cont.machine.stack_size = th->ec->machine.stack_end - th->ec->machine.stack_start;
1549 old_fiber->cont.machine.stack = th->ec->machine.stack_start;
1554 old_fiber->cont.saved_ec.machine.stack_start = th->ec->machine.stack_start;
1555 old_fiber->cont.saved_ec.machine.stack_end = FIBER_TERMINATED_P(old_fiber) ? NULL : th->ec->machine.stack_end;
1560 #if defined(COROUTINE_SANITIZE_ADDRESS)
1561 __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);
1565 struct coroutine_context * from = coroutine_transfer(&old_fiber->context, &new_fiber->context);
1567 #if defined(COROUTINE_SANITIZE_ADDRESS)
1568 __sanitizer_finish_switch_fiber(old_fiber->context.fake_stack, NULL, NULL);
1576 fiber_restore_thread(th, old_fiber);
1582 NOINLINE(NORETURN(
static void cont_restore_1(
rb_context_t *)));
1587 cont_restore_thread(cont);
1590 #if defined(_M_AMD64) && !defined(__MINGW64__)
1595 _JUMP_BUFFER *bp = (
void*)&cont->jmpbuf;
1596 bp->Frame = ((_JUMP_BUFFER*)((
void*)&buf))->Frame;
1599 if (cont->machine.stack_src) {
1600 FLUSH_REGISTER_WINDOWS;
1601 MEMCPY(cont->machine.stack_src, cont->machine.stack,
1602 VALUE, cont->machine.stack_size);
1605 ruby_longjmp(cont->jmpbuf, 1);
1613 if (cont->machine.stack_src) {
1615 #define STACK_PAD_SIZE 1
1617 #define STACK_PAD_SIZE 1024
1619 VALUE space[STACK_PAD_SIZE];
1621 #if !STACK_GROW_DIRECTION
1622 if (addr_in_prev_frame > &space[0]) {
1625 #if STACK_GROW_DIRECTION <= 0
1626 volatile VALUE *
const end = cont->machine.stack_src;
1627 if (&space[0] > end) {
1636 cont_restore_0(cont, &space[0]);
1640 #if !STACK_GROW_DIRECTION
1645 #if STACK_GROW_DIRECTION >= 0
1646 volatile VALUE *
const end = cont->machine.stack_src + cont->machine.stack_size;
1647 if (&space[STACK_PAD_SIZE] < end) {
1652 cont_restore_0(cont, &space[STACK_PAD_SIZE-1]);
1656 #if !STACK_GROW_DIRECTION
1660 cont_restore_1(cont);
1747 rb_callcc(
VALUE self)
1749 volatile int called;
1750 volatile VALUE val = cont_capture(&called);
1759 #ifdef RUBY_ASAN_ENABLED
1762 MAYBE_UNUSED(
static void notusing_callcc(
void)) { rb_callcc(
Qnil); }
1763 # define rb_callcc rb_f_notimplement
1768 make_passing_arg(
int argc,
const VALUE *argv)
1784 NORETURN(
static VALUE rb_cont_call(
int argc,
VALUE *argv,
VALUE contval));
1802 rb_cont_call(
int argc,
VALUE *argv,
VALUE contval)
1807 if (cont_thread_value(cont) != th->self) {
1810 if (cont->saved_ec.fiber_ptr) {
1811 if (th->ec->fiber_ptr != cont->saved_ec.fiber_ptr) {
1817 cont->value = make_passing_arg(argc, argv);
1819 cont_restore_0(cont, &contval);
1912 {fiber_mark, fiber_free, fiber_memsize, fiber_compact,},
1913 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1917 fiber_alloc(
VALUE klass)
1923 fiber_t_alloc(
VALUE fiber_value,
unsigned int blocking)
1932 THREAD_MUST_BE_RUNNING(th);
1934 fiber->cont.self = fiber_value;
1935 fiber->cont.type = FIBER_CONTEXT;
1936 fiber->blocking = blocking;
1938 cont_init(&fiber->cont, th);
1940 fiber->cont.saved_ec.fiber_ptr = fiber;
1941 rb_ec_clear_vm_stack(&fiber->cont.saved_ec);
1947 VM_ASSERT(FIBER_CREATED_P(fiber));
1957 VALUE fiber_value = fiber_alloc(rb_cFiber);
1960 VM_ASSERT(
DATA_PTR(fiber_value) == NULL);
1961 VM_ASSERT(fiber->cont.type == FIBER_CONTEXT);
1962 VM_ASSERT(FIBER_RESUMED_P(fiber));
1964 th->root_fiber = fiber;
1966 fiber->cont.self = fiber_value;
1968 coroutine_initialize_main(&fiber->context);
1977 if (ec->fiber_ptr->cont.self == 0) {
1978 root_fiber_alloc(rb_ec_thread_ptr(ec));
1980 return ec->fiber_ptr;
1984 current_fiber_storage(
void)
1991 inherit_fiber_storage(
void)
1999 fiber->cont.saved_ec.storage = storage;
2003 fiber_storage_get(
rb_fiber_t *fiber,
int allocate)
2005 VALUE storage = fiber->cont.saved_ec.storage;
2006 if (storage ==
Qnil && allocate) {
2008 fiber_storage_set(fiber, storage);
2014 storage_access_must_be_from_same_fiber(
VALUE self)
2018 if (fiber != current) {
2030 rb_fiber_storage_get(
VALUE self)
2032 storage_access_must_be_from_same_fiber(
self);
2034 VALUE storage = fiber_storage_get(fiber_ptr(
self), FALSE);
2036 if (storage ==
Qnil) {
2053 fiber_storage_validate(
VALUE value)
2056 if (value ==
Qnil)
return;
2092 rb_fiber_storage_set(
VALUE self,
VALUE value)
2096 "Fiber#storage= is experimental and may be removed in the future!");
2099 storage_access_must_be_from_same_fiber(
self);
2100 fiber_storage_validate(value);
2102 fiber_ptr(
self)->cont.saved_ec.storage =
rb_obj_dup(value);
2117 rb_fiber_storage_aref(
VALUE class,
VALUE key)
2121 VALUE storage = fiber_storage_get(fiber_current(), FALSE);
2142 VALUE storage = fiber_storage_get(fiber_current(), value !=
Qnil);
2145 if (value ==
Qnil) {
2158 storage = inherit_fiber_storage();
2161 fiber_storage_validate(storage);
2165 rb_fiber_t *fiber = fiber_t_alloc(
self, blocking);
2167 fiber->cont.saved_ec.storage = storage;
2168 fiber->first_proc = proc;
2169 fiber->stack.base = NULL;
2181 size_t vm_stack_size = 0;
2182 VALUE *vm_stack = fiber_initialize_coroutine(fiber, &vm_stack_size);
2185 cont->saved_vm_stack.ptr = NULL;
2186 rb_ec_initialize_vm_stack(sec, vm_stack, vm_stack_size /
sizeof(
VALUE));
2189 sec->local_storage = NULL;
2190 sec->local_storage_recursive_hash =
Qnil;
2191 sec->local_storage_recursive_hash_for_trace =
Qnil;
2195 rb_fiber_pool_default(
VALUE pool)
2197 return &shared_fiber_pool;
2203 fiber->cont.saved_ec.storage = storage;
2209 rb_fiber_initialize_kw(
int argc,
VALUE* argv,
VALUE self,
int kw_splat)
2220 rb_get_kwargs(options, fiber_initialize_keywords, 0, 3, arguments);
2222 if (!UNDEF_P(arguments[0])) {
2223 blocking = arguments[0];
2226 if (!UNDEF_P(arguments[1])) {
2227 pool = arguments[1];
2230 storage = arguments[2];
2233 return fiber_initialize(
self,
rb_block_proc(), rb_fiber_pool_default(pool),
RTEST(blocking), storage);
2286 rb_fiber_initialize(
int argc,
VALUE* argv,
VALUE self)
2294 return fiber_initialize(fiber_alloc(rb_cFiber),
rb_proc_new(func, obj), rb_fiber_pool_default(
Qnil), 0, storage);
2304 rb_fiber_s_schedule_kw(
int argc,
VALUE* argv,
int kw_splat)
2307 VALUE scheduler = th->scheduler;
2310 if (scheduler !=
Qnil) {
2362 rb_fiber_s_schedule(
int argc,
VALUE *argv,
VALUE obj)
2378 rb_fiber_s_scheduler(
VALUE klass)
2392 rb_fiber_current_scheduler(
VALUE klass)
2414 rb_fiber_set_scheduler(
VALUE klass,
VALUE scheduler)
2419 NORETURN(
static void rb_fiber_terminate(
rb_fiber_t *fiber,
int need_interrupt,
VALUE err));
2424 rb_thread_t *
volatile th = fiber->cont.saved_ec.thread_ptr;
2427 enum ruby_tag_type state;
2429 VM_ASSERT(th->ec == GET_EC());
2430 VM_ASSERT(FIBER_RESUMED_P(fiber));
2432 if (fiber->blocking) {
2436 EC_PUSH_TAG(th->ec);
2437 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2440 const VALUE *argv, args = cont->value;
2441 GetProcPtr(fiber->first_proc, proc);
2444 th->ec->errinfo =
Qnil;
2445 th->ec->root_lep = rb_vm_proc_local_ep(fiber->first_proc);
2446 th->ec->root_svar =
Qfalse;
2449 cont->value = rb_vm_invoke_proc(th->ec, proc, argc, argv, cont->kw_splat, VM_BLOCK_HANDLER_NONE);
2453 int need_interrupt = TRUE;
2456 err = th->ec->errinfo;
2457 VM_ASSERT(FIBER_RESUMED_P(fiber));
2459 if (state == TAG_RAISE) {
2462 else if (state == TAG_FATAL && err == RUBY_FATAL_FIBER_KILLED) {
2463 need_interrupt = FALSE;
2466 else if (state == TAG_FATAL) {
2467 rb_threadptr_pending_interrupt_enque(th, err);
2470 err = rb_vm_make_jump_tag_but_local_jump(state, err);
2474 rb_fiber_terminate(fiber, need_interrupt, err);
2485 fiber->cont.type = FIBER_CONTEXT;
2486 fiber->cont.saved_ec.fiber_ptr = fiber;
2487 fiber->cont.saved_ec.thread_ptr = th;
2488 fiber->blocking = 1;
2490 fiber_status_set(fiber, FIBER_RESUMED);
2491 th->ec = &fiber->cont.saved_ec;
2492 cont_init_jit_cont(&fiber->cont);
2498 if (th->root_fiber) {
2504 VM_ASSERT(th->ec->fiber_ptr->cont.type == FIBER_CONTEXT);
2505 VM_ASSERT(th->ec->fiber_ptr->cont.self == 0);
2507 if (ec && th->ec == ec) {
2508 rb_ractor_set_current_ec(th->ractor, NULL);
2510 fiber_free(th->ec->fiber_ptr);
2516 rb_threadptr_root_fiber_terminate(
rb_thread_t *th)
2520 fiber->status = FIBER_TERMINATED;
2523 rb_ec_clear_vm_stack(th->ec);
2527 return_fiber(
bool terminate)
2534 prev->resuming_fiber = NULL;
2539 rb_raise(rb_eFiberError,
"attempt to yield on a not resumed fiber");
2545 VM_ASSERT(root_fiber != NULL);
2548 for (fiber = root_fiber; fiber->resuming_fiber; fiber = fiber->resuming_fiber) {
2558 return fiber_current()->cont.self;
2567 if (th->ec->fiber_ptr != NULL) {
2568 fiber = th->ec->fiber_ptr;
2572 fiber = root_fiber_alloc(th);
2575 if (FIBER_CREATED_P(next_fiber)) {
2576 fiber_prepare_stack(next_fiber);
2579 VM_ASSERT(FIBER_RESUMED_P(fiber) || FIBER_TERMINATED_P(fiber));
2580 VM_ASSERT(FIBER_RUNNABLE_P(next_fiber));
2582 if (FIBER_RESUMED_P(fiber)) fiber_status_set(fiber, FIBER_SUSPENDED);
2584 fiber_status_set(next_fiber, FIBER_RESUMED);
2585 fiber_setcontext(next_fiber, fiber);
2591 VM_ASSERT(fiber == fiber_current());
2593 if (fiber->killed) {
2594 rb_thread_t *thread = fiber->cont.saved_ec.thread_ptr;
2596 thread->ec->errinfo = RUBY_FATAL_FIBER_KILLED;
2597 EC_JUMP_TAG(thread->ec, RUBY_TAG_FATAL);
2609 if (th->root_fiber == NULL) root_fiber_alloc(th);
2611 if (th->ec->fiber_ptr == fiber) {
2615 return make_passing_arg(argc, argv);
2618 if (cont_thread_value(cont) != th->self) {
2619 rb_raise(rb_eFiberError,
"fiber called across threads");
2622 if (FIBER_TERMINATED_P(fiber)) {
2623 value =
rb_exc_new2(rb_eFiberError,
"dead fiber called");
2625 if (!FIBER_TERMINATED_P(th->ec->fiber_ptr)) {
2627 VM_UNREACHABLE(fiber_switch);
2633 VM_ASSERT(FIBER_SUSPENDED_P(th->root_fiber));
2635 cont = &th->root_fiber->cont;
2637 cont->value = value;
2639 fiber_setcontext(th->root_fiber, th->ec->fiber_ptr);
2641 VM_UNREACHABLE(fiber_switch);
2645 VM_ASSERT(FIBER_RUNNABLE_P(fiber));
2649 VM_ASSERT(!current_fiber->resuming_fiber);
2651 if (resuming_fiber) {
2652 current_fiber->resuming_fiber = resuming_fiber;
2653 fiber->prev = fiber_current();
2654 fiber->yielding = 0;
2657 VM_ASSERT(!current_fiber->yielding);
2659 current_fiber->yielding = 1;
2662 if (current_fiber->blocking) {
2667 cont->kw_splat = kw_splat;
2668 cont->value = make_passing_arg(argc, argv);
2670 fiber_store(fiber, th);
2673 #ifndef COROUTINE_PTHREAD_CONTEXT
2674 if (resuming_fiber && FIBER_TERMINATED_P(fiber)) {
2675 fiber_stack_release(fiber);
2679 if (fiber_current()->blocking) {
2683 RUBY_VM_CHECK_INTS(th->ec);
2687 current_fiber = th->ec->fiber_ptr;
2688 value = current_fiber->cont.value;
2690 fiber_check_killed(current_fiber);
2692 if (current_fiber->cont.argc == -1) {
2703 return fiber_switch(fiber_ptr(fiber_value), argc, argv,
RB_NO_KEYWORDS, NULL,
false);
2721 rb_fiber_blocking_p(
VALUE fiber)
2723 return RBOOL(fiber_ptr(fiber)->blocking);
2727 fiber_blocking_yield(
VALUE fiber_value)
2730 rb_thread_t *
volatile th = fiber->cont.saved_ec.thread_ptr;
2732 VM_ASSERT(fiber->blocking == 0);
2735 fiber->blocking = 1;
2744 fiber_blocking_ensure(
VALUE fiber_value)
2747 rb_thread_t *
volatile th = fiber->cont.saved_ec.thread_ptr;
2750 fiber->blocking = 0;
2767 rb_fiber_blocking(
VALUE class)
2773 if (fiber->blocking) {
2777 return rb_ensure(fiber_blocking_yield, fiber_value, fiber_blocking_ensure, fiber_value);
2800 rb_fiber_s_blocking_p(
VALUE klass)
2803 unsigned blocking = thread->blocking;
2814 fiber_status_set(fiber, FIBER_TERMINATED);
2818 rb_fiber_terminate(
rb_fiber_t *fiber,
int need_interrupt,
VALUE error)
2820 VALUE value = fiber->cont.value;
2822 VM_ASSERT(FIBER_RESUMED_P(fiber));
2823 rb_fiber_close(fiber);
2825 fiber->cont.machine.stack = NULL;
2826 fiber->cont.machine.stack_size = 0;
2830 if (need_interrupt) RUBY_VM_SET_INTERRUPT(&next_fiber->cont.saved_ec);
2833 fiber_switch(next_fiber, -1, &error,
RB_NO_KEYWORDS, NULL,
false);
2835 fiber_switch(next_fiber, 1, &value,
RB_NO_KEYWORDS, NULL,
false);
2840 fiber_resume_kw(
rb_fiber_t *fiber,
int argc,
const VALUE *argv,
int kw_splat)
2844 if (argc == -1 && FIBER_CREATED_P(fiber)) {
2845 rb_raise(rb_eFiberError,
"cannot raise exception on unborn fiber");
2847 else if (FIBER_TERMINATED_P(fiber)) {
2848 rb_raise(rb_eFiberError,
"attempt to resume a terminated fiber");
2850 else if (fiber == current_fiber) {
2851 rb_raise(rb_eFiberError,
"attempt to resume the current fiber");
2853 else if (fiber->prev != NULL) {
2854 rb_raise(rb_eFiberError,
"attempt to resume a resumed fiber (double resume)");
2856 else if (fiber->resuming_fiber) {
2857 rb_raise(rb_eFiberError,
"attempt to resume a resuming fiber");
2859 else if (fiber->prev == NULL &&
2860 (!fiber->yielding && fiber->status != FIBER_CREATED)) {
2861 rb_raise(rb_eFiberError,
"attempt to resume a transferring fiber");
2864 return fiber_switch(fiber, argc, argv, kw_splat, fiber,
false);
2870 return fiber_resume_kw(fiber_ptr(
self), argc, argv, kw_splat);
2876 return fiber_resume_kw(fiber_ptr(
self), argc, argv,
RB_NO_KEYWORDS);
2882 return fiber_switch(return_fiber(
false), argc, argv, kw_splat, NULL,
true);
2888 return fiber_switch(return_fiber(
false), argc, argv,
RB_NO_KEYWORDS, NULL,
true);
2892 rb_fiber_reset_root_local_storage(
rb_thread_t *th)
2894 if (th->root_fiber && th->root_fiber != th->ec->fiber_ptr) {
2895 th->ec->local_storage = th->root_fiber->cont.saved_ec.local_storage;
2910 return RBOOL(!FIBER_TERMINATED_P(fiber_ptr(fiber_value)));
2929 rb_fiber_m_resume(
int argc,
VALUE *argv,
VALUE fiber)
2981 rb_fiber_backtrace(
int argc,
VALUE *argv,
VALUE fiber)
2983 return rb_vm_backtrace(argc, argv, &fiber_ptr(fiber)->cont.saved_ec);
3006 rb_fiber_backtrace_locations(
int argc,
VALUE *argv,
VALUE fiber)
3008 return rb_vm_backtrace_locations(argc, argv, &fiber_ptr(fiber)->cont.saved_ec);
3094 rb_fiber_m_transfer(
int argc,
VALUE *argv,
VALUE self)
3100 fiber_transfer_kw(
rb_fiber_t *fiber,
int argc,
const VALUE *argv,
int kw_splat)
3102 if (fiber->resuming_fiber) {
3103 rb_raise(rb_eFiberError,
"attempt to transfer to a resuming fiber");
3106 if (fiber->yielding) {
3107 rb_raise(rb_eFiberError,
"attempt to transfer to a yielding fiber");
3110 return fiber_switch(fiber, argc, argv, kw_splat, NULL,
false);
3116 return fiber_transfer_kw(fiber_ptr(
self), argc, argv, kw_splat);
3130 rb_fiber_s_yield(
int argc,
VALUE *argv,
VALUE klass)
3138 if (fiber == fiber_current()) {
3141 else if (fiber->resuming_fiber) {
3142 return fiber_raise(fiber->resuming_fiber, exception);
3144 else if (FIBER_SUSPENDED_P(fiber) && !fiber->yielding) {
3157 return fiber_raise(fiber_ptr(fiber), exception);
3186 rb_fiber_m_raise(
int argc,
VALUE *argv,
VALUE self)
3209 rb_fiber_m_kill(
VALUE self)
3213 if (fiber->killed)
return Qfalse;
3216 if (fiber->status == FIBER_CREATED) {
3217 fiber->status = FIBER_TERMINATED;
3219 else if (fiber->status != FIBER_TERMINATED) {
3220 if (fiber_current() == fiber) {
3221 fiber_check_killed(fiber);
3224 fiber_raise(fiber_ptr(
self),
Qnil);
3239 rb_fiber_s_current(
VALUE klass)
3245 fiber_to_s(
VALUE fiber_value)
3247 const rb_fiber_t *fiber = fiber_ptr(fiber_value);
3249 char status_info[0x20];
3251 if (fiber->resuming_fiber) {
3252 snprintf(status_info, 0x20,
" (%s by resuming)", fiber_status_name(fiber->status));
3255 snprintf(status_info, 0x20,
" (%s)", fiber_status_name(fiber->status));
3260 strlcat(status_info,
">",
sizeof(status_info));
3265 GetProcPtr(fiber->first_proc, proc);
3266 return rb_block_to_s(fiber_value, &proc->block, status_info);
3269 #ifdef HAVE_WORKING_FORK
3273 if (th->root_fiber) {
3274 if (&th->root_fiber->cont.saved_ec != th->ec) {
3275 th->root_fiber = th->ec->fiber_ptr;
3277 th->root_fiber->prev = 0;
3282 #ifdef RB_EXPERIMENTAL_FIBER_POOL
3284 fiber_pool_free(
void *
ptr)
3287 RUBY_FREE_ENTER(
"fiber_pool");
3289 fiber_pool_allocation_free(
fiber_pool->allocations);
3292 RUBY_FREE_LEAVE(
"fiber_pool");
3296 fiber_pool_memsize(
const void *
ptr)
3299 size_t size =
sizeof(*fiber_pool);
3308 {NULL, fiber_pool_free, fiber_pool_memsize,},
3309 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3313 fiber_pool_alloc(
VALUE klass)
3321 rb_fiber_pool_initialize(
int argc,
VALUE* argv,
VALUE self)
3328 rb_scan_args(argc, argv,
"03", &size, &count, &vm_stack_size);
3331 size =
SIZET2NUM(th->vm->default_params.fiber_machine_stack_size);
3338 if (
NIL_P(vm_stack_size)) {
3339 vm_stack_size =
SIZET2NUM(th->vm->default_params.fiber_vm_stack_size);
3367 size_t vm_stack_size = th->vm->default_params.fiber_vm_stack_size;
3368 size_t machine_stack_size = th->vm->default_params.fiber_machine_stack_size;
3369 size_t stack_size = machine_stack_size + vm_stack_size;
3373 GetSystemInfo(&info);
3374 pagesize = info.dwPageSize;
3376 pagesize = sysconf(_SC_PAGESIZE);
3378 SET_MACHINE_STACK_END(&th->ec->machine.stack_end);
3380 fiber_pool_initialize(&shared_fiber_pool, stack_size, FIBER_POOL_INITIAL_SIZE, vm_stack_size);
3386 const char *fiber_shared_fiber_pool_free_stacks = getenv(
"RUBY_SHARED_FIBER_POOL_FREE_STACKS");
3387 if (fiber_shared_fiber_pool_free_stacks) {
3388 shared_fiber_pool.free_stacks = atoi(fiber_shared_fiber_pool_free_stacks);
3390 if (shared_fiber_pool.free_stacks < 0) {
3391 rb_warn(
"Setting RUBY_SHARED_FIBER_POOL_FREE_STACKS to a negative value is not allowed.");
3392 shared_fiber_pool.free_stacks = 0;
3395 if (shared_fiber_pool.free_stacks > 1) {
3396 rb_warn(
"Setting RUBY_SHARED_FIBER_POOL_FREE_STACKS to a value greater than 1 is operating system specific, and may cause crashes.");
3417 rb_define_method(rb_cFiber,
"backtrace_locations", rb_fiber_backtrace_locations, -1);
3430 #ifdef RB_EXPERIMENTAL_FIBER_POOL
3433 rb_define_method(rb_cFiberPool,
"initialize", rb_fiber_pool_initialize, -1);
3439 RUBY_SYMBOL_EXPORT_BEGIN
3442 ruby_Init_Continuation_body(
void)
3452 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_class, 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.