Ruby 3.4.1p0 (2024-12-25 revision 48d4efcb85000e1ebae42004e963b5d0cedddcf2)
cont.c
1/**********************************************************************
2
3 cont.c -
4
5 $Author$
6 created at: Thu May 23 09:03:43 2007
7
8 Copyright (C) 2007 Koichi Sasada
9
10**********************************************************************/
11
12#include "ruby/internal/config.h"
13
14#ifndef _WIN32
15#include <unistd.h>
16#include <sys/mman.h>
17#endif
18
19// On Solaris, madvise() is NOT declared for SUS (XPG4v2) or later,
20// but MADV_* macros are defined when __EXTENSIONS__ is defined.
21#ifdef NEED_MADVICE_PROTOTYPE_USING_CADDR_T
22#include <sys/types.h>
23extern int madvise(caddr_t, size_t, int);
24#endif
25
26#include COROUTINE_H
27
28#include "eval_intern.h"
29#include "internal.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"
38#include "rjit.h"
39#include "yjit.h"
40#include "vm_core.h"
41#include "vm_sync.h"
42#include "id_table.h"
43#include "ractor_core.h"
44
45static const int DEBUG = 0;
46
47#define RB_PAGE_SIZE (pagesize)
48#define RB_PAGE_MASK (~(RB_PAGE_SIZE - 1))
49static long pagesize;
50
51static const rb_data_type_t cont_data_type, fiber_data_type;
52static VALUE rb_cContinuation;
53static VALUE rb_cFiber;
54static VALUE rb_eFiberError;
55#ifdef RB_EXPERIMENTAL_FIBER_POOL
56static VALUE rb_cFiberPool;
57#endif
58
59#define CAPTURE_JUST_VALID_VM_STACK 1
60
61// Defined in `coroutine/$arch/Context.h`:
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
66#else
67#define FIBER_POOL_INITIAL_SIZE 32
68#define FIBER_POOL_ALLOCATION_MAXIMUM_SIZE 1024
69#endif
70#ifdef RB_EXPERIMENTAL_FIBER_POOL
71#define FIBER_POOL_ALLOCATION_FREE
72#endif
73
74enum context_type {
75 CONTINUATION_CONTEXT = 0,
76 FIBER_CONTEXT = 1
77};
78
80 VALUE *ptr;
81#ifdef CAPTURE_JUST_VALID_VM_STACK
82 size_t slen; /* length of stack (head of ec->vm_stack) */
83 size_t clen; /* length of control frames (tail of ec->vm_stack) */
84#endif
85};
86
87struct fiber_pool;
88
89// Represents a single stack.
91 // A pointer to the memory allocation (lowest address) for the stack.
92 void * base;
93
94 // The current stack pointer, taking into account the direction of the stack.
95 void * current;
96
97 // The size of the stack excluding any guard pages.
98 size_t size;
99
100 // The available stack capacity w.r.t. the current stack offset.
101 size_t available;
102
103 // The pool this stack should be allocated from.
104 struct fiber_pool * pool;
105
106 // If the stack is allocated, the allocation it came from.
107 struct fiber_pool_allocation * allocation;
108};
109
110// A linked list of vacant (unused) stacks.
111// This structure is stored in the first page of a stack if it is not in use.
112// @sa fiber_pool_vacancy_pointer
114 // Details about the vacant stack:
115 struct fiber_pool_stack stack;
116
117 // The vacancy linked list.
118#ifdef FIBER_POOL_ALLOCATION_FREE
119 struct fiber_pool_vacancy * previous;
120#endif
121 struct fiber_pool_vacancy * next;
122};
123
124// Manages singly linked list of mapped regions of memory which contains 1 more more stack:
125//
126// base = +-------------------------------+-----------------------+ +
127// |VM Stack |VM Stack | | |
128// | | | | |
129// | | | | |
130// +-------------------------------+ | |
131// |Machine Stack |Machine Stack | | |
132// | | | | |
133// | | | | |
134// | | | . . . . | | size
135// | | | | |
136// | | | | |
137// | | | | |
138// | | | | |
139// | | | | |
140// +-------------------------------+ | |
141// |Guard Page |Guard Page | | |
142// +-------------------------------+-----------------------+ v
143//
144// +------------------------------------------------------->
145//
146// count
147//
149 // A pointer to the memory mapped region.
150 void * base;
151
152 // The size of the individual stacks.
153 size_t size;
154
155 // The stride of individual stacks (including any guard pages or other accounting details).
156 size_t stride;
157
158 // The number of stacks that were allocated.
159 size_t count;
160
161#ifdef FIBER_POOL_ALLOCATION_FREE
162 // The number of stacks used in this allocation.
163 size_t used;
164#endif
165
166 struct fiber_pool * pool;
167
168 // The allocation linked list.
169#ifdef FIBER_POOL_ALLOCATION_FREE
170 struct fiber_pool_allocation * previous;
171#endif
172 struct fiber_pool_allocation * next;
173};
174
175// A fiber pool manages vacant stacks to reduce the overhead of creating fibers.
177 // A singly-linked list of allocations which contain 1 or more stacks each.
178 struct fiber_pool_allocation * allocations;
179
180 // Free list that provides O(1) stack "allocation".
181 struct fiber_pool_vacancy * vacancies;
182
183 // The size of the stack allocations (excluding any guard page).
184 size_t size;
185
186 // The total number of stacks that have been allocated in this pool.
187 size_t count;
188
189 // The initial number of stacks to allocate.
190 size_t initial_count;
191
192 // Whether to madvise(free) the stack or not.
193 // If this value is set to 1, the stack will be madvise(free)ed
194 // (or equivalent), where possible, when it is returned to the pool.
195 int free_stacks;
196
197 // The number of stacks that have been used in this pool.
198 size_t used;
199
200 // The amount to allocate for the vm_stack.
201 size_t vm_stack_size;
202};
203
204// Continuation contexts used by JITs
206 rb_execution_context_t *ec; // continuation ec
207 struct rb_jit_cont *prev, *next; // used to form lists
208};
209
210// Doubly linked list for enumerating all on-stack ISEQs.
211static struct rb_jit_cont *first_jit_cont;
212
213typedef struct rb_context_struct {
214 enum context_type type;
215 int argc;
216 int kw_splat;
217 VALUE self;
218 VALUE value;
219
220 struct cont_saved_vm_stack saved_vm_stack;
221
222 struct {
223 VALUE *stack;
224 VALUE *stack_src;
225 size_t stack_size;
226 } machine;
227 rb_execution_context_t saved_ec;
228 rb_jmpbuf_t jmpbuf;
229 struct rb_jit_cont *jit_cont; // Continuation contexts for JITs
230} rb_context_t;
231
232/*
233 * Fiber status:
234 * [Fiber.new] ------> FIBER_CREATED ----> [Fiber#kill] --> |
235 * | [Fiber#resume] |
236 * v |
237 * +--> FIBER_RESUMED ----> [return] ------> |
238 * [Fiber#resume] | | [Fiber.yield/transfer] |
239 * [Fiber#transfer] | v |
240 * +--- FIBER_SUSPENDED --> [Fiber#kill] --> |
241 * |
242 * |
243 * FIBER_TERMINATED <-------------------+
244 */
245enum fiber_status {
246 FIBER_CREATED,
247 FIBER_RESUMED,
248 FIBER_SUSPENDED,
249 FIBER_TERMINATED
250};
251
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))
257
259 rb_context_t cont;
260 VALUE first_proc;
261 struct rb_fiber_struct *prev;
262 struct rb_fiber_struct *resuming_fiber;
263
264 BITFIELD(enum fiber_status, status, 2);
265 /* Whether the fiber is allowed to implicitly yield. */
266 unsigned int yielding : 1;
267 unsigned int blocking : 1;
268
269 unsigned int killed : 1;
270
271 struct coroutine_context context;
272 struct fiber_pool_stack stack;
273};
274
275static struct fiber_pool shared_fiber_pool = {NULL, NULL, 0, 0, 0, 0};
276
277void
278rb_free_shared_fiber_pool(void)
279{
280 struct fiber_pool_allocation *allocations = shared_fiber_pool.allocations;
281 while (allocations) {
282 struct fiber_pool_allocation *next = allocations->next;
283 xfree(allocations);
284 allocations = next;
285 }
286}
287
288static ID fiber_initialize_keywords[3] = {0};
289
290/*
291 * FreeBSD require a first (i.e. addr) argument of mmap(2) is not NULL
292 * if MAP_STACK is passed.
293 * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=158755
294 */
295#if defined(MAP_STACK) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
296#define FIBER_STACK_FLAGS (MAP_PRIVATE | MAP_ANON | MAP_STACK)
297#else
298#define FIBER_STACK_FLAGS (MAP_PRIVATE | MAP_ANON)
299#endif
300
301#define ERRNOMSG strerror(errno)
302
303// Locates the stack vacancy details for the given stack.
304inline static struct fiber_pool_vacancy *
305fiber_pool_vacancy_pointer(void * base, size_t size)
306{
307 STACK_GROW_DIR_DETECTION;
308
309 return (struct fiber_pool_vacancy *)(
310 (char*)base + STACK_DIR_UPPER(0, size - RB_PAGE_SIZE)
311 );
312}
313
314#if defined(COROUTINE_SANITIZE_ADDRESS)
315// Compute the base pointer for a vacant stack, for the area which can be poisoned.
316inline static void *
317fiber_pool_stack_poison_base(struct fiber_pool_stack * stack)
318{
319 STACK_GROW_DIR_DETECTION;
320
321 return (char*)stack->base + STACK_DIR_UPPER(RB_PAGE_SIZE, 0);
322}
323
324// Compute the size of the vacant stack, for the area that can be poisoned.
325inline static size_t
326fiber_pool_stack_poison_size(struct fiber_pool_stack * stack)
327{
328 return stack->size - RB_PAGE_SIZE;
329}
330#endif
331
332// Reset the current stack pointer and available size of the given stack.
333inline static void
334fiber_pool_stack_reset(struct fiber_pool_stack * stack)
335{
336 STACK_GROW_DIR_DETECTION;
337
338 stack->current = (char*)stack->base + STACK_DIR_UPPER(0, stack->size);
339 stack->available = stack->size;
340}
341
342// A pointer to the base of the current unused portion of the stack.
343inline static void *
344fiber_pool_stack_base(struct fiber_pool_stack * stack)
345{
346 STACK_GROW_DIR_DETECTION;
347
348 VM_ASSERT(stack->current);
349
350 return STACK_DIR_UPPER(stack->current, (char*)stack->current - stack->available);
351}
352
353// Allocate some memory from the stack. Used to allocate vm_stack inline with machine stack.
354// @sa fiber_initialize_coroutine
355inline static void *
356fiber_pool_stack_alloca(struct fiber_pool_stack * stack, size_t offset)
357{
358 STACK_GROW_DIR_DETECTION;
359
360 if (DEBUG) fprintf(stderr, "fiber_pool_stack_alloca(%p): %"PRIuSIZE"/%"PRIuSIZE"\n", (void*)stack, offset, stack->available);
361 VM_ASSERT(stack->available >= offset);
362
363 // The pointer to the memory being allocated:
364 void * pointer = STACK_DIR_UPPER(stack->current, (char*)stack->current - offset);
365
366 // Move the stack pointer:
367 stack->current = STACK_DIR_UPPER((char*)stack->current + offset, (char*)stack->current - offset);
368 stack->available -= offset;
369
370 return pointer;
371}
372
373// Reset the current stack pointer and available size of the given stack.
374inline static void
375fiber_pool_vacancy_reset(struct fiber_pool_vacancy * vacancy)
376{
377 fiber_pool_stack_reset(&vacancy->stack);
378
379 // Consume one page of the stack because it's used for the vacancy list:
380 fiber_pool_stack_alloca(&vacancy->stack, RB_PAGE_SIZE);
381}
382
383inline static struct fiber_pool_vacancy *
384fiber_pool_vacancy_push(struct fiber_pool_vacancy * vacancy, struct fiber_pool_vacancy * head)
385{
386 vacancy->next = head;
387
388#ifdef FIBER_POOL_ALLOCATION_FREE
389 if (head) {
390 head->previous = vacancy;
391 vacancy->previous = NULL;
392 }
393#endif
394
395 return vacancy;
396}
397
398#ifdef FIBER_POOL_ALLOCATION_FREE
399static void
400fiber_pool_vacancy_remove(struct fiber_pool_vacancy * vacancy)
401{
402 if (vacancy->next) {
403 vacancy->next->previous = vacancy->previous;
404 }
405
406 if (vacancy->previous) {
407 vacancy->previous->next = vacancy->next;
408 }
409 else {
410 // It's the head of the list:
411 vacancy->stack.pool->vacancies = vacancy->next;
412 }
413}
414
415inline static struct fiber_pool_vacancy *
416fiber_pool_vacancy_pop(struct fiber_pool * pool)
417{
418 struct fiber_pool_vacancy * vacancy = pool->vacancies;
419
420 if (vacancy) {
421 fiber_pool_vacancy_remove(vacancy);
422 }
423
424 return vacancy;
425}
426#else
427inline static struct fiber_pool_vacancy *
428fiber_pool_vacancy_pop(struct fiber_pool * pool)
429{
430 struct fiber_pool_vacancy * vacancy = pool->vacancies;
431
432 if (vacancy) {
433 pool->vacancies = vacancy->next;
434 }
435
436 return vacancy;
437}
438#endif
439
440// Initialize the vacant stack. The [base, size] allocation should not include the guard page.
441// @param base The pointer to the lowest address of the allocated memory.
442// @param size The size of the allocated memory.
443inline static struct fiber_pool_vacancy *
444fiber_pool_vacancy_initialize(struct fiber_pool * fiber_pool, struct fiber_pool_vacancy * vacancies, void * base, size_t size)
445{
446 struct fiber_pool_vacancy * vacancy = fiber_pool_vacancy_pointer(base, size);
447
448 vacancy->stack.base = base;
449 vacancy->stack.size = size;
450
451 fiber_pool_vacancy_reset(vacancy);
452
453 vacancy->stack.pool = fiber_pool;
454
455 return fiber_pool_vacancy_push(vacancy, vacancies);
456}
457
458// Allocate a maximum of count stacks, size given by stride.
459// @param count the number of stacks to allocate / were allocated.
460// @param stride the size of the individual stacks.
461// @return [void *] the allocated memory or NULL if allocation failed.
462inline static void *
463fiber_pool_allocate_memory(size_t * count, size_t stride)
464{
465 // We use a divide-by-2 strategy to try and allocate memory. We are trying
466 // to allocate `count` stacks. In normal situation, this won't fail. But
467 // if we ran out of address space, or we are allocating more memory than
468 // the system would allow (e.g. overcommit * physical memory + swap), we
469 // divide count by two and try again. This condition should only be
470 // encountered in edge cases, but we handle it here gracefully.
471 while (*count > 1) {
472#if defined(_WIN32)
473 void * base = VirtualAlloc(0, (*count)*stride, MEM_COMMIT, PAGE_READWRITE);
474
475 if (!base) {
476 *count = (*count) >> 1;
477 }
478 else {
479 return base;
480 }
481#else
482 errno = 0;
483 size_t mmap_size = (*count)*stride;
484 void * base = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, FIBER_STACK_FLAGS, -1, 0);
485
486 if (base == MAP_FAILED) {
487 // If the allocation fails, count = count / 2, and try again.
488 *count = (*count) >> 1;
489 }
490 else {
491 ruby_annotate_mmap(base, mmap_size, "Ruby:fiber_pool_allocate_memory");
492#if defined(MADV_FREE_REUSE)
493 // On Mac MADV_FREE_REUSE is necessary for the task_info api
494 // to keep the accounting accurate as possible when a page is marked as reusable
495 // it can possibly not occurring at first call thus re-iterating if necessary.
496 while (madvise(base, mmap_size, MADV_FREE_REUSE) == -1 && errno == EAGAIN);
497#endif
498 return base;
499 }
500#endif
501 }
502
503 return NULL;
504}
505
506// Given an existing fiber pool, expand it by the specified number of stacks.
507// @param count the maximum number of stacks to allocate.
508// @return the allocated fiber pool.
509// @sa fiber_pool_allocation_free
510static struct fiber_pool_allocation *
511fiber_pool_expand(struct fiber_pool * fiber_pool, size_t count)
512{
513 STACK_GROW_DIR_DETECTION;
514
515 size_t size = fiber_pool->size;
516 size_t stride = size + RB_PAGE_SIZE;
517
518 // Allocate the memory required for the stacks:
519 void * base = fiber_pool_allocate_memory(&count, stride);
520
521 if (base == NULL) {
522 rb_raise(rb_eFiberError, "can't alloc machine stack to fiber (%"PRIuSIZE" x %"PRIuSIZE" bytes): %s", count, size, ERRNOMSG);
523 }
524
525 struct fiber_pool_vacancy * vacancies = fiber_pool->vacancies;
526 struct fiber_pool_allocation * allocation = RB_ALLOC(struct fiber_pool_allocation);
527
528 // Initialize fiber pool allocation:
529 allocation->base = base;
530 allocation->size = size;
531 allocation->stride = stride;
532 allocation->count = count;
533#ifdef FIBER_POOL_ALLOCATION_FREE
534 allocation->used = 0;
535#endif
536 allocation->pool = fiber_pool;
537
538 if (DEBUG) {
539 fprintf(stderr, "fiber_pool_expand(%"PRIuSIZE"): %p, %"PRIuSIZE"/%"PRIuSIZE" x [%"PRIuSIZE":%"PRIuSIZE"]\n",
540 count, (void*)fiber_pool, fiber_pool->used, fiber_pool->count, size, fiber_pool->vm_stack_size);
541 }
542
543 // Iterate over all stacks, initializing the vacancy list:
544 for (size_t i = 0; i < count; i += 1) {
545 void * base = (char*)allocation->base + (stride * i);
546 void * page = (char*)base + STACK_DIR_UPPER(size, 0);
547
548#if defined(_WIN32)
549 DWORD old_protect;
550
551 if (!VirtualProtect(page, RB_PAGE_SIZE, PAGE_READWRITE | PAGE_GUARD, &old_protect)) {
552 VirtualFree(allocation->base, 0, MEM_RELEASE);
553 rb_raise(rb_eFiberError, "can't set a guard page: %s", ERRNOMSG);
554 }
555#else
556 if (mprotect(page, RB_PAGE_SIZE, PROT_NONE) < 0) {
557 munmap(allocation->base, count*stride);
558 rb_raise(rb_eFiberError, "can't set a guard page: %s", ERRNOMSG);
559 }
560#endif
561
562 vacancies = fiber_pool_vacancy_initialize(
563 fiber_pool, vacancies,
564 (char*)base + STACK_DIR_UPPER(0, RB_PAGE_SIZE),
565 size
566 );
567
568#ifdef FIBER_POOL_ALLOCATION_FREE
569 vacancies->stack.allocation = allocation;
570#endif
571 }
572
573 // Insert the allocation into the head of the pool:
574 allocation->next = fiber_pool->allocations;
575
576#ifdef FIBER_POOL_ALLOCATION_FREE
577 if (allocation->next) {
578 allocation->next->previous = allocation;
579 }
580
581 allocation->previous = NULL;
582#endif
583
584 fiber_pool->allocations = allocation;
585 fiber_pool->vacancies = vacancies;
586 fiber_pool->count += count;
587
588 return allocation;
589}
590
591// Initialize the specified fiber pool with the given number of stacks.
592// @param vm_stack_size The size of the vm stack to allocate.
593static void
594fiber_pool_initialize(struct fiber_pool * fiber_pool, size_t size, size_t count, size_t vm_stack_size)
595{
596 VM_ASSERT(vm_stack_size < size);
597
598 fiber_pool->allocations = NULL;
599 fiber_pool->vacancies = NULL;
600 fiber_pool->size = ((size / RB_PAGE_SIZE) + 1) * RB_PAGE_SIZE;
601 fiber_pool->count = 0;
602 fiber_pool->initial_count = count;
603 fiber_pool->free_stacks = 1;
604 fiber_pool->used = 0;
605
606 fiber_pool->vm_stack_size = vm_stack_size;
607
608 fiber_pool_expand(fiber_pool, count);
609}
610
611#ifdef FIBER_POOL_ALLOCATION_FREE
612// Free the list of fiber pool allocations.
613static void
614fiber_pool_allocation_free(struct fiber_pool_allocation * allocation)
615{
616 STACK_GROW_DIR_DETECTION;
617
618 VM_ASSERT(allocation->used == 0);
619
620 if (DEBUG) fprintf(stderr, "fiber_pool_allocation_free: %p base=%p count=%"PRIuSIZE"\n", (void*)allocation, allocation->base, allocation->count);
621
622 size_t i;
623 for (i = 0; i < allocation->count; i += 1) {
624 void * base = (char*)allocation->base + (allocation->stride * i) + STACK_DIR_UPPER(0, RB_PAGE_SIZE);
625
626 struct fiber_pool_vacancy * vacancy = fiber_pool_vacancy_pointer(base, allocation->size);
627
628 // Pop the vacant stack off the free list:
629 fiber_pool_vacancy_remove(vacancy);
630 }
631
632#ifdef _WIN32
633 VirtualFree(allocation->base, 0, MEM_RELEASE);
634#else
635 munmap(allocation->base, allocation->stride * allocation->count);
636#endif
637
638 if (allocation->previous) {
639 allocation->previous->next = allocation->next;
640 }
641 else {
642 // We are the head of the list, so update the pool:
643 allocation->pool->allocations = allocation->next;
644 }
645
646 if (allocation->next) {
647 allocation->next->previous = allocation->previous;
648 }
649
650 allocation->pool->count -= allocation->count;
651
652 ruby_xfree(allocation);
653}
654#endif
655
656// Acquire a stack from the given fiber pool. If none are available, allocate more.
657static struct fiber_pool_stack
658fiber_pool_stack_acquire(struct fiber_pool * fiber_pool)
659{
660 struct fiber_pool_vacancy * vacancy = fiber_pool_vacancy_pop(fiber_pool);
661
662 if (DEBUG) fprintf(stderr, "fiber_pool_stack_acquire: %p used=%"PRIuSIZE"\n", (void*)fiber_pool->vacancies, fiber_pool->used);
663
664 if (!vacancy) {
665 const size_t maximum = FIBER_POOL_ALLOCATION_MAXIMUM_SIZE;
666 const size_t minimum = fiber_pool->initial_count;
667
668 size_t count = fiber_pool->count;
669 if (count > maximum) count = maximum;
670 if (count < minimum) count = minimum;
671
672 fiber_pool_expand(fiber_pool, count);
673
674 // The free list should now contain some stacks:
675 VM_ASSERT(fiber_pool->vacancies);
676
677 vacancy = fiber_pool_vacancy_pop(fiber_pool);
678 }
679
680 VM_ASSERT(vacancy);
681 VM_ASSERT(vacancy->stack.base);
682
683#if defined(COROUTINE_SANITIZE_ADDRESS)
684 __asan_unpoison_memory_region(fiber_pool_stack_poison_base(&vacancy->stack), fiber_pool_stack_poison_size(&vacancy->stack));
685#endif
686
687 // Take the top item from the free list:
688 fiber_pool->used += 1;
689
690#ifdef FIBER_POOL_ALLOCATION_FREE
691 vacancy->stack.allocation->used += 1;
692#endif
693
694 fiber_pool_stack_reset(&vacancy->stack);
695
696 return vacancy->stack;
697}
698
699// We advise the operating system that the stack memory pages are no longer being used.
700// This introduce some performance overhead but allows system to relaim memory when there is pressure.
701static inline void
702fiber_pool_stack_free(struct fiber_pool_stack * stack)
703{
704 void * base = fiber_pool_stack_base(stack);
705 size_t size = stack->available;
706
707 // If this is not true, the vacancy information will almost certainly be destroyed:
708 VM_ASSERT(size <= (stack->size - RB_PAGE_SIZE));
709
710 int advice = stack->pool->free_stacks >> 1;
711
712 if (DEBUG) fprintf(stderr, "fiber_pool_stack_free: %p+%"PRIuSIZE" [base=%p, size=%"PRIuSIZE"] advice=%d\n", base, size, stack->base, stack->size, advice);
713
714 // The pages being used by the stack can be returned back to the system.
715 // That doesn't change the page mapping, but it does allow the system to
716 // reclaim the physical memory.
717 // Since we no longer care about the data itself, we don't need to page
718 // out to disk, since that is costly. Not all systems support that, so
719 // we try our best to select the most efficient implementation.
720 // In addition, it's actually slightly desirable to not do anything here,
721 // but that results in higher memory usage.
722
723#ifdef __wasi__
724 // WebAssembly doesn't support madvise, so we just don't do anything.
725#elif VM_CHECK_MODE > 0 && defined(MADV_DONTNEED)
726 if (!advice) advice = MADV_DONTNEED;
727 // This immediately discards the pages and the memory is reset to zero.
728 madvise(base, size, advice);
729#elif defined(MADV_FREE_REUSABLE)
730 if (!advice) advice = MADV_FREE_REUSABLE;
731 // Darwin / macOS / iOS.
732 // Acknowledge the kernel down to the task info api we make this
733 // page reusable for future use.
734 // As for MADV_FREE_REUSABLE below we ensure in the rare occasions the task was not
735 // completed at the time of the call to re-iterate.
736 while (madvise(base, size, advice) == -1 && errno == EAGAIN);
737#elif defined(MADV_FREE)
738 if (!advice) advice = MADV_FREE;
739 // Recent Linux.
740 madvise(base, size, advice);
741#elif defined(MADV_DONTNEED)
742 if (!advice) advice = MADV_DONTNEED;
743 // Old Linux.
744 madvise(base, size, advice);
745#elif defined(POSIX_MADV_DONTNEED)
746 if (!advice) advice = POSIX_MADV_DONTNEED;
747 // Solaris?
748 posix_madvise(base, size, advice);
749#elif defined(_WIN32)
750 VirtualAlloc(base, size, MEM_RESET, PAGE_READWRITE);
751 // Not available in all versions of Windows.
752 //DiscardVirtualMemory(base, size);
753#endif
754
755#if defined(COROUTINE_SANITIZE_ADDRESS)
756 __asan_poison_memory_region(fiber_pool_stack_poison_base(stack), fiber_pool_stack_poison_size(stack));
757#endif
758}
759
760// Release and return a stack to the vacancy list.
761static void
762fiber_pool_stack_release(struct fiber_pool_stack * stack)
763{
764 struct fiber_pool * pool = stack->pool;
765 struct fiber_pool_vacancy * vacancy = fiber_pool_vacancy_pointer(stack->base, stack->size);
766
767 if (DEBUG) fprintf(stderr, "fiber_pool_stack_release: %p used=%"PRIuSIZE"\n", stack->base, stack->pool->used);
768
769 // Copy the stack details into the vacancy area:
770 vacancy->stack = *stack;
771 // After this point, be careful about updating/using state in stack, since it's copied to the vacancy area.
772
773 // Reset the stack pointers and reserve space for the vacancy data:
774 fiber_pool_vacancy_reset(vacancy);
775
776 // Push the vacancy into the vancancies list:
777 pool->vacancies = fiber_pool_vacancy_push(vacancy, pool->vacancies);
778 pool->used -= 1;
779
780#ifdef FIBER_POOL_ALLOCATION_FREE
781 struct fiber_pool_allocation * allocation = stack->allocation;
782
783 allocation->used -= 1;
784
785 // Release address space and/or dirty memory:
786 if (allocation->used == 0) {
787 fiber_pool_allocation_free(allocation);
788 }
789 else if (stack->pool->free_stacks) {
790 fiber_pool_stack_free(&vacancy->stack);
791 }
792#else
793 // This is entirely optional, but clears the dirty flag from the stack
794 // memory, so it won't get swapped to disk when there is memory pressure:
795 if (stack->pool->free_stacks) {
796 fiber_pool_stack_free(&vacancy->stack);
797 }
798#endif
799}
800
801static inline void
802ec_switch(rb_thread_t *th, rb_fiber_t *fiber)
803{
804 rb_execution_context_t *ec = &fiber->cont.saved_ec;
805#ifdef RUBY_ASAN_ENABLED
806 ec->machine.asan_fake_stack_handle = asan_get_thread_fake_stack_handle();
807#endif
808 rb_ractor_set_current_ec(th->ractor, th->ec = ec);
809 // ruby_current_execution_context_ptr = th->ec = ec;
810
811 /*
812 * timer-thread may set trap interrupt on previous th->ec at any time;
813 * ensure we do not delay (or lose) the trap interrupt handling.
814 */
815 if (th->vm->ractor.main_thread == th &&
816 rb_signal_buff_size() > 0) {
817 RUBY_VM_SET_TRAP_INTERRUPT(ec);
818 }
819
820 VM_ASSERT(ec->fiber_ptr->cont.self == 0 || ec->vm_stack != NULL);
821}
822
823static inline void
824fiber_restore_thread(rb_thread_t *th, rb_fiber_t *fiber)
825{
826 ec_switch(th, fiber);
827 VM_ASSERT(th->ec->fiber_ptr == fiber);
828}
829
830#ifndef COROUTINE_DECL
831# define COROUTINE_DECL COROUTINE
832#endif
833NORETURN(static COROUTINE_DECL fiber_entry(struct coroutine_context * from, struct coroutine_context * to));
834static COROUTINE
835fiber_entry(struct coroutine_context * from, struct coroutine_context * to)
836{
837 rb_fiber_t *fiber = to->argument;
838
839#if defined(COROUTINE_SANITIZE_ADDRESS)
840 // Address sanitizer will copy the previous stack base and stack size into
841 // the "from" fiber. `coroutine_initialize_main` doesn't generally know the
842 // stack bounds (base + size). Therefore, the main fiber `stack_base` and
843 // `stack_size` will be NULL/0. It's specifically important in that case to
844 // get the (base+size) of the previous fiber and save it, so that later when
845 // we return to the main coroutine, we don't supply (NULL, 0) to
846 // __sanitizer_start_switch_fiber which royally messes up the internal state
847 // of ASAN and causes (sometimes) the following message:
848 // "WARNING: ASan is ignoring requested __asan_handle_no_return"
849 __sanitizer_finish_switch_fiber(to->fake_stack, (const void**)&from->stack_base, &from->stack_size);
850#endif
851
852 rb_thread_t *thread = fiber->cont.saved_ec.thread_ptr;
853
854#ifdef COROUTINE_PTHREAD_CONTEXT
855 ruby_thread_set_native(thread);
856#endif
857
858 fiber_restore_thread(thread, fiber);
859
860 rb_fiber_start(fiber);
861
862#ifndef COROUTINE_PTHREAD_CONTEXT
863 VM_UNREACHABLE(fiber_entry);
864#endif
865}
866
867// Initialize a fiber's coroutine's machine stack and vm stack.
868static VALUE *
869fiber_initialize_coroutine(rb_fiber_t *fiber, size_t * vm_stack_size)
870{
871 struct fiber_pool * fiber_pool = fiber->stack.pool;
872 rb_execution_context_t *sec = &fiber->cont.saved_ec;
873 void * vm_stack = NULL;
874
875 VM_ASSERT(fiber_pool != NULL);
876
877 fiber->stack = fiber_pool_stack_acquire(fiber_pool);
878 vm_stack = fiber_pool_stack_alloca(&fiber->stack, fiber_pool->vm_stack_size);
879 *vm_stack_size = fiber_pool->vm_stack_size;
880
881 coroutine_initialize(&fiber->context, fiber_entry, fiber_pool_stack_base(&fiber->stack), fiber->stack.available);
882
883 // The stack for this execution context is the one we allocated:
884 sec->machine.stack_start = fiber->stack.current;
885 sec->machine.stack_maxsize = fiber->stack.available;
886
887 fiber->context.argument = (void*)fiber;
888
889 return vm_stack;
890}
891
892// Release the stack from the fiber, it's execution context, and return it to
893// the fiber pool.
894static void
895fiber_stack_release(rb_fiber_t * fiber)
896{
897 rb_execution_context_t *ec = &fiber->cont.saved_ec;
898
899 if (DEBUG) fprintf(stderr, "fiber_stack_release: %p, stack.base=%p\n", (void*)fiber, fiber->stack.base);
900
901 // Return the stack back to the fiber pool if it wasn't already:
902 if (fiber->stack.base) {
903 fiber_pool_stack_release(&fiber->stack);
904 fiber->stack.base = NULL;
905 }
906
907 // The stack is no longer associated with this execution context:
908 rb_ec_clear_vm_stack(ec);
909}
910
911static const char *
912fiber_status_name(enum fiber_status s)
913{
914 switch (s) {
915 case FIBER_CREATED: return "created";
916 case FIBER_RESUMED: return "resumed";
917 case FIBER_SUSPENDED: return "suspended";
918 case FIBER_TERMINATED: return "terminated";
919 }
920 VM_UNREACHABLE(fiber_status_name);
921 return NULL;
922}
923
924static void
925fiber_verify(const rb_fiber_t *fiber)
926{
927#if VM_CHECK_MODE > 0
928 VM_ASSERT(fiber->cont.saved_ec.fiber_ptr == fiber);
929
930 switch (fiber->status) {
931 case FIBER_RESUMED:
932 VM_ASSERT(fiber->cont.saved_ec.vm_stack != NULL);
933 break;
934 case FIBER_SUSPENDED:
935 VM_ASSERT(fiber->cont.saved_ec.vm_stack != NULL);
936 break;
937 case FIBER_CREATED:
938 case FIBER_TERMINATED:
939 /* TODO */
940 break;
941 default:
942 VM_UNREACHABLE(fiber_verify);
943 }
944#endif
945}
946
947inline static void
948fiber_status_set(rb_fiber_t *fiber, enum fiber_status s)
949{
950 // if (DEBUG) fprintf(stderr, "fiber: %p, status: %s -> %s\n", (void *)fiber, fiber_status_name(fiber->status), fiber_status_name(s));
951 VM_ASSERT(!FIBER_TERMINATED_P(fiber));
952 VM_ASSERT(fiber->status != s);
953 fiber_verify(fiber);
954 fiber->status = s;
955}
956
957static rb_context_t *
958cont_ptr(VALUE obj)
959{
960 rb_context_t *cont;
961
962 TypedData_Get_Struct(obj, rb_context_t, &cont_data_type, cont);
963
964 return cont;
965}
966
967static rb_fiber_t *
968fiber_ptr(VALUE obj)
969{
970 rb_fiber_t *fiber;
971
972 TypedData_Get_Struct(obj, rb_fiber_t, &fiber_data_type, fiber);
973 if (!fiber) rb_raise(rb_eFiberError, "uninitialized fiber");
974
975 return fiber;
976}
977
978NOINLINE(static VALUE cont_capture(volatile int *volatile stat));
979
980#define THREAD_MUST_BE_RUNNING(th) do { \
981 if (!(th)->ec->tag) rb_raise(rb_eThreadError, "not running thread"); \
982 } while (0)
983
984rb_thread_t*
985rb_fiber_threadptr(const rb_fiber_t *fiber)
986{
987 return fiber->cont.saved_ec.thread_ptr;
988}
989
990static VALUE
991cont_thread_value(const rb_context_t *cont)
992{
993 return cont->saved_ec.thread_ptr->self;
994}
995
996static void
997cont_compact(void *ptr)
998{
999 rb_context_t *cont = ptr;
1000
1001 if (cont->self) {
1002 cont->self = rb_gc_location(cont->self);
1003 }
1004 cont->value = rb_gc_location(cont->value);
1005 rb_execution_context_update(&cont->saved_ec);
1006}
1007
1008static void
1009cont_mark(void *ptr)
1010{
1011 rb_context_t *cont = ptr;
1012
1013 RUBY_MARK_ENTER("cont");
1014 if (cont->self) {
1015 rb_gc_mark_movable(cont->self);
1016 }
1017 rb_gc_mark_movable(cont->value);
1018
1019 rb_execution_context_mark(&cont->saved_ec);
1020 rb_gc_mark(cont_thread_value(cont));
1021
1022 if (cont->saved_vm_stack.ptr) {
1023#ifdef CAPTURE_JUST_VALID_VM_STACK
1024 rb_gc_mark_locations(cont->saved_vm_stack.ptr,
1025 cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
1026#else
1027 rb_gc_mark_locations(cont->saved_vm_stack.ptr,
1028 cont->saved_vm_stack.ptr, cont->saved_ec.stack_size);
1029#endif
1030 }
1031
1032 if (cont->machine.stack) {
1033 if (cont->type == CONTINUATION_CONTEXT) {
1034 /* cont */
1035 rb_gc_mark_locations(cont->machine.stack,
1036 cont->machine.stack + cont->machine.stack_size);
1037 }
1038 else {
1039 /* fiber machine context is marked as part of rb_execution_context_mark, no need to
1040 * do anything here. */
1041 }
1042 }
1043
1044 RUBY_MARK_LEAVE("cont");
1045}
1046
1047#if 0
1048static int
1049fiber_is_root_p(const rb_fiber_t *fiber)
1050{
1051 return fiber == fiber->cont.saved_ec.thread_ptr->root_fiber;
1052}
1053#endif
1054
1055static void jit_cont_free(struct rb_jit_cont *cont);
1056
1057static void
1058cont_free(void *ptr)
1059{
1060 rb_context_t *cont = ptr;
1061
1062 RUBY_FREE_ENTER("cont");
1063
1064 if (cont->type == CONTINUATION_CONTEXT) {
1065 ruby_xfree(cont->saved_ec.vm_stack);
1066 RUBY_FREE_UNLESS_NULL(cont->machine.stack);
1067 }
1068 else {
1069 rb_fiber_t *fiber = (rb_fiber_t*)cont;
1070 coroutine_destroy(&fiber->context);
1071 fiber_stack_release(fiber);
1072 }
1073
1074 RUBY_FREE_UNLESS_NULL(cont->saved_vm_stack.ptr);
1075
1076 VM_ASSERT(cont->jit_cont != NULL);
1077 jit_cont_free(cont->jit_cont);
1078 /* free rb_cont_t or rb_fiber_t */
1079 ruby_xfree(ptr);
1080 RUBY_FREE_LEAVE("cont");
1081}
1082
1083static size_t
1084cont_memsize(const void *ptr)
1085{
1086 const rb_context_t *cont = ptr;
1087 size_t size = 0;
1088
1089 size = sizeof(*cont);
1090 if (cont->saved_vm_stack.ptr) {
1091#ifdef CAPTURE_JUST_VALID_VM_STACK
1092 size_t n = (cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
1093#else
1094 size_t n = cont->saved_ec.vm_stack_size;
1095#endif
1096 size += n * sizeof(*cont->saved_vm_stack.ptr);
1097 }
1098
1099 if (cont->machine.stack) {
1100 size += cont->machine.stack_size * sizeof(*cont->machine.stack);
1101 }
1102
1103 return size;
1104}
1105
1106void
1107rb_fiber_update_self(rb_fiber_t *fiber)
1108{
1109 if (fiber->cont.self) {
1110 fiber->cont.self = rb_gc_location(fiber->cont.self);
1111 }
1112 else {
1113 rb_execution_context_update(&fiber->cont.saved_ec);
1114 }
1115}
1116
1117void
1118rb_fiber_mark_self(const rb_fiber_t *fiber)
1119{
1120 if (fiber->cont.self) {
1121 rb_gc_mark_movable(fiber->cont.self);
1122 }
1123 else {
1124 rb_execution_context_mark(&fiber->cont.saved_ec);
1125 }
1126}
1127
1128static void
1129fiber_compact(void *ptr)
1130{
1131 rb_fiber_t *fiber = ptr;
1132 fiber->first_proc = rb_gc_location(fiber->first_proc);
1133
1134 if (fiber->prev) rb_fiber_update_self(fiber->prev);
1135
1136 cont_compact(&fiber->cont);
1137 fiber_verify(fiber);
1138}
1139
1140static void
1141fiber_mark(void *ptr)
1142{
1143 rb_fiber_t *fiber = ptr;
1144 RUBY_MARK_ENTER("cont");
1145 fiber_verify(fiber);
1146 rb_gc_mark_movable(fiber->first_proc);
1147 if (fiber->prev) rb_fiber_mark_self(fiber->prev);
1148 cont_mark(&fiber->cont);
1149 RUBY_MARK_LEAVE("cont");
1150}
1151
1152static void
1153fiber_free(void *ptr)
1154{
1155 rb_fiber_t *fiber = ptr;
1156 RUBY_FREE_ENTER("fiber");
1157
1158 if (DEBUG) fprintf(stderr, "fiber_free: %p[%p]\n", (void *)fiber, fiber->stack.base);
1159
1160 if (fiber->cont.saved_ec.local_storage) {
1161 rb_id_table_free(fiber->cont.saved_ec.local_storage);
1162 }
1163
1164 cont_free(&fiber->cont);
1165 RUBY_FREE_LEAVE("fiber");
1166}
1167
1168static size_t
1169fiber_memsize(const void *ptr)
1170{
1171 const rb_fiber_t *fiber = ptr;
1172 size_t size = sizeof(*fiber);
1173 const rb_execution_context_t *saved_ec = &fiber->cont.saved_ec;
1174 const rb_thread_t *th = rb_ec_thread_ptr(saved_ec);
1175
1176 /*
1177 * vm.c::thread_memsize already counts th->ec->local_storage
1178 */
1179 if (saved_ec->local_storage && fiber != th->root_fiber) {
1180 size += rb_id_table_memsize(saved_ec->local_storage);
1181 size += rb_obj_memsize_of(saved_ec->storage);
1182 }
1183
1184 size += cont_memsize(&fiber->cont);
1185 return size;
1186}
1187
1188VALUE
1189rb_obj_is_fiber(VALUE obj)
1190{
1191 return RBOOL(rb_typeddata_is_kind_of(obj, &fiber_data_type));
1192}
1193
1194static void
1195cont_save_machine_stack(rb_thread_t *th, rb_context_t *cont)
1196{
1197 size_t size;
1198
1199 SET_MACHINE_STACK_END(&th->ec->machine.stack_end);
1200
1201 if (th->ec->machine.stack_start > th->ec->machine.stack_end) {
1202 size = cont->machine.stack_size = th->ec->machine.stack_start - th->ec->machine.stack_end;
1203 cont->machine.stack_src = th->ec->machine.stack_end;
1204 }
1205 else {
1206 size = cont->machine.stack_size = th->ec->machine.stack_end - th->ec->machine.stack_start;
1207 cont->machine.stack_src = th->ec->machine.stack_start;
1208 }
1209
1210 if (cont->machine.stack) {
1211 REALLOC_N(cont->machine.stack, VALUE, size);
1212 }
1213 else {
1214 cont->machine.stack = ALLOC_N(VALUE, size);
1215 }
1216
1217 FLUSH_REGISTER_WINDOWS;
1218 asan_unpoison_memory_region(cont->machine.stack_src, size, false);
1219 MEMCPY(cont->machine.stack, cont->machine.stack_src, VALUE, size);
1220}
1221
1222static const rb_data_type_t cont_data_type = {
1223 "continuation",
1224 {cont_mark, cont_free, cont_memsize, cont_compact},
1225 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1226};
1227
1228static inline void
1229cont_save_thread(rb_context_t *cont, rb_thread_t *th)
1230{
1231 rb_execution_context_t *sec = &cont->saved_ec;
1232
1233 VM_ASSERT(th->status == THREAD_RUNNABLE);
1234
1235 /* save thread context */
1236 *sec = *th->ec;
1237
1238 /* saved_ec->machine.stack_end should be NULL */
1239 /* because it may happen GC afterward */
1240 sec->machine.stack_end = NULL;
1241}
1242
1243static rb_nativethread_lock_t jit_cont_lock;
1244
1245// Register a new continuation with execution context `ec`. Return JIT info about
1246// the continuation.
1247static struct rb_jit_cont *
1248jit_cont_new(rb_execution_context_t *ec)
1249{
1250 struct rb_jit_cont *cont;
1251
1252 // We need to use calloc instead of something like ZALLOC to avoid triggering GC here.
1253 // When this function is called from rb_thread_alloc through rb_threadptr_root_fiber_setup,
1254 // the thread is still being prepared and marking it causes SEGV.
1255 cont = calloc(1, sizeof(struct rb_jit_cont));
1256 if (cont == NULL)
1257 rb_memerror();
1258 cont->ec = ec;
1259
1260 rb_native_mutex_lock(&jit_cont_lock);
1261 if (first_jit_cont == NULL) {
1262 cont->next = cont->prev = NULL;
1263 }
1264 else {
1265 cont->prev = NULL;
1266 cont->next = first_jit_cont;
1267 first_jit_cont->prev = cont;
1268 }
1269 first_jit_cont = cont;
1270 rb_native_mutex_unlock(&jit_cont_lock);
1271
1272 return cont;
1273}
1274
1275// Unregister continuation `cont`.
1276static void
1277jit_cont_free(struct rb_jit_cont *cont)
1278{
1279 if (!cont) return;
1280
1281 rb_native_mutex_lock(&jit_cont_lock);
1282 if (cont == first_jit_cont) {
1283 first_jit_cont = cont->next;
1284 if (first_jit_cont != NULL)
1285 first_jit_cont->prev = NULL;
1286 }
1287 else {
1288 cont->prev->next = cont->next;
1289 if (cont->next != NULL)
1290 cont->next->prev = cont->prev;
1291 }
1292 rb_native_mutex_unlock(&jit_cont_lock);
1293
1294 free(cont);
1295}
1296
1297// Call a given callback against all on-stack ISEQs.
1298void
1299rb_jit_cont_each_iseq(rb_iseq_callback callback, void *data)
1300{
1301 struct rb_jit_cont *cont;
1302 for (cont = first_jit_cont; cont != NULL; cont = cont->next) {
1303 if (cont->ec->vm_stack == NULL)
1304 continue;
1305
1306 const rb_control_frame_t *cfp = cont->ec->cfp;
1307 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(cont->ec, cfp)) {
1308 if (cfp->pc && cfp->iseq && imemo_type((VALUE)cfp->iseq) == imemo_iseq) {
1309 callback(cfp->iseq, data);
1310 }
1311 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1312 }
1313 }
1314}
1315
1316#if USE_YJIT
1317// Update the jit_return of all CFPs to leave_exit unless it's leave_exception or not set.
1318// This prevents jit_exec_exception from jumping to the caller after invalidation.
1319void
1320rb_yjit_cancel_jit_return(void *leave_exit, void *leave_exception)
1321{
1322 struct rb_jit_cont *cont;
1323 for (cont = first_jit_cont; cont != NULL; cont = cont->next) {
1324 if (cont->ec->vm_stack == NULL)
1325 continue;
1326
1327 const rb_control_frame_t *cfp = cont->ec->cfp;
1328 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(cont->ec, cfp)) {
1329 if (cfp->jit_return && cfp->jit_return != leave_exception) {
1330 ((rb_control_frame_t *)cfp)->jit_return = leave_exit;
1331 }
1332 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1333 }
1334 }
1335}
1336#endif
1337
1338// Finish working with jit_cont.
1339void
1340rb_jit_cont_finish(void)
1341{
1342 struct rb_jit_cont *cont, *next;
1343 for (cont = first_jit_cont; cont != NULL; cont = next) {
1344 next = cont->next;
1345 free(cont); // Don't use xfree because it's allocated by calloc.
1346 }
1347 rb_native_mutex_destroy(&jit_cont_lock);
1348}
1349
1350static void
1351cont_init_jit_cont(rb_context_t *cont)
1352{
1353 VM_ASSERT(cont->jit_cont == NULL);
1354 // We always allocate this since YJIT may be enabled later
1355 cont->jit_cont = jit_cont_new(&(cont->saved_ec));
1356}
1357
1359rb_fiberptr_get_ec(struct rb_fiber_struct *fiber)
1360{
1361 return &fiber->cont.saved_ec;
1362}
1363
1364static void
1365cont_init(rb_context_t *cont, rb_thread_t *th)
1366{
1367 /* save thread context */
1368 cont_save_thread(cont, th);
1369 cont->saved_ec.thread_ptr = th;
1370 cont->saved_ec.local_storage = NULL;
1371 cont->saved_ec.local_storage_recursive_hash = Qnil;
1372 cont->saved_ec.local_storage_recursive_hash_for_trace = Qnil;
1373 cont_init_jit_cont(cont);
1374}
1375
1376static rb_context_t *
1377cont_new(VALUE klass)
1378{
1379 rb_context_t *cont;
1380 volatile VALUE contval;
1381 rb_thread_t *th = GET_THREAD();
1382
1383 THREAD_MUST_BE_RUNNING(th);
1384 contval = TypedData_Make_Struct(klass, rb_context_t, &cont_data_type, cont);
1385 cont->self = contval;
1386 cont_init(cont, th);
1387 return cont;
1388}
1389
1390VALUE
1391rb_fiberptr_self(struct rb_fiber_struct *fiber)
1392{
1393 return fiber->cont.self;
1394}
1395
1396unsigned int
1397rb_fiberptr_blocking(struct rb_fiber_struct *fiber)
1398{
1399 return fiber->blocking;
1400}
1401
1402// Initialize the jit_cont_lock
1403void
1404rb_jit_cont_init(void)
1405{
1406 rb_native_mutex_initialize(&jit_cont_lock);
1407}
1408
1409#if 0
1410void
1411show_vm_stack(const rb_execution_context_t *ec)
1412{
1413 VALUE *p = ec->vm_stack;
1414 while (p < ec->cfp->sp) {
1415 fprintf(stderr, "%3d ", (int)(p - ec->vm_stack));
1416 rb_obj_info_dump(*p);
1417 p++;
1418 }
1419}
1420
1421void
1422show_vm_pcs(const rb_control_frame_t *cfp,
1423 const rb_control_frame_t *end_of_cfp)
1424{
1425 int i=0;
1426 while (cfp != end_of_cfp) {
1427 int pc = 0;
1428 if (cfp->iseq) {
1429 pc = cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded;
1430 }
1431 fprintf(stderr, "%2d pc: %d\n", i++, pc);
1432 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1433 }
1434}
1435#endif
1436
1437static VALUE
1438cont_capture(volatile int *volatile stat)
1439{
1440 rb_context_t *volatile cont;
1441 rb_thread_t *th = GET_THREAD();
1442 volatile VALUE contval;
1443 const rb_execution_context_t *ec = th->ec;
1444
1445 THREAD_MUST_BE_RUNNING(th);
1446 rb_vm_stack_to_heap(th->ec);
1447 cont = cont_new(rb_cContinuation);
1448 contval = cont->self;
1449
1450#ifdef CAPTURE_JUST_VALID_VM_STACK
1451 cont->saved_vm_stack.slen = ec->cfp->sp - ec->vm_stack;
1452 cont->saved_vm_stack.clen = ec->vm_stack + ec->vm_stack_size - (VALUE*)ec->cfp;
1453 cont->saved_vm_stack.ptr = ALLOC_N(VALUE, cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
1454 MEMCPY(cont->saved_vm_stack.ptr,
1455 ec->vm_stack,
1456 VALUE, cont->saved_vm_stack.slen);
1457 MEMCPY(cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen,
1458 (VALUE*)ec->cfp,
1459 VALUE,
1460 cont->saved_vm_stack.clen);
1461#else
1462 cont->saved_vm_stack.ptr = ALLOC_N(VALUE, ec->vm_stack_size);
1463 MEMCPY(cont->saved_vm_stack.ptr, ec->vm_stack, VALUE, ec->vm_stack_size);
1464#endif
1465 // At this point, `cfp` is valid but `vm_stack` should be cleared:
1466 rb_ec_set_vm_stack(&cont->saved_ec, NULL, 0);
1467 VM_ASSERT(cont->saved_ec.cfp != NULL);
1468 cont_save_machine_stack(th, cont);
1469
1470 if (ruby_setjmp(cont->jmpbuf)) {
1471 VALUE value;
1472
1473 VAR_INITIALIZED(cont);
1474 value = cont->value;
1475 if (cont->argc == -1) rb_exc_raise(value);
1476 cont->value = Qnil;
1477 *stat = 1;
1478 return value;
1479 }
1480 else {
1481 *stat = 0;
1482 return contval;
1483 }
1484}
1485
1486static inline void
1487cont_restore_thread(rb_context_t *cont)
1488{
1489 rb_thread_t *th = GET_THREAD();
1490
1491 /* restore thread context */
1492 if (cont->type == CONTINUATION_CONTEXT) {
1493 /* continuation */
1494 rb_execution_context_t *sec = &cont->saved_ec;
1495 rb_fiber_t *fiber = NULL;
1496
1497 if (sec->fiber_ptr != NULL) {
1498 fiber = sec->fiber_ptr;
1499 }
1500 else if (th->root_fiber) {
1501 fiber = th->root_fiber;
1502 }
1503
1504 if (fiber && th->ec != &fiber->cont.saved_ec) {
1505 ec_switch(th, fiber);
1506 }
1507
1508 if (th->ec->trace_arg != sec->trace_arg) {
1509 rb_raise(rb_eRuntimeError, "can't call across trace_func");
1510 }
1511
1512 /* copy vm stack */
1513#ifdef CAPTURE_JUST_VALID_VM_STACK
1514 MEMCPY(th->ec->vm_stack,
1515 cont->saved_vm_stack.ptr,
1516 VALUE, cont->saved_vm_stack.slen);
1517 MEMCPY(th->ec->vm_stack + th->ec->vm_stack_size - cont->saved_vm_stack.clen,
1518 cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen,
1519 VALUE, cont->saved_vm_stack.clen);
1520#else
1521 MEMCPY(th->ec->vm_stack, cont->saved_vm_stack.ptr, VALUE, sec->vm_stack_size);
1522#endif
1523 /* other members of ec */
1524
1525 th->ec->cfp = sec->cfp;
1526 th->ec->raised_flag = sec->raised_flag;
1527 th->ec->tag = sec->tag;
1528 th->ec->root_lep = sec->root_lep;
1529 th->ec->root_svar = sec->root_svar;
1530 th->ec->errinfo = sec->errinfo;
1531
1532 VM_ASSERT(th->ec->vm_stack != NULL);
1533 }
1534 else {
1535 /* fiber */
1536 fiber_restore_thread(th, (rb_fiber_t*)cont);
1537 }
1538}
1539
1540NOINLINE(static void fiber_setcontext(rb_fiber_t *new_fiber, rb_fiber_t *old_fiber));
1541
1542static void
1543fiber_setcontext(rb_fiber_t *new_fiber, rb_fiber_t *old_fiber)
1544{
1545 rb_thread_t *th = GET_THREAD();
1546
1547 /* save old_fiber's machine stack - to ensure efficient garbage collection */
1548 if (!FIBER_TERMINATED_P(old_fiber)) {
1549 STACK_GROW_DIR_DETECTION;
1550 SET_MACHINE_STACK_END(&th->ec->machine.stack_end);
1551 if (STACK_DIR_UPPER(0, 1)) {
1552 old_fiber->cont.machine.stack_size = th->ec->machine.stack_start - th->ec->machine.stack_end;
1553 old_fiber->cont.machine.stack = th->ec->machine.stack_end;
1554 }
1555 else {
1556 old_fiber->cont.machine.stack_size = th->ec->machine.stack_end - th->ec->machine.stack_start;
1557 old_fiber->cont.machine.stack = th->ec->machine.stack_start;
1558 }
1559 }
1560
1561 /* these values are used in rb_gc_mark_machine_context to mark the fiber's stack. */
1562 old_fiber->cont.saved_ec.machine.stack_start = th->ec->machine.stack_start;
1563 old_fiber->cont.saved_ec.machine.stack_end = FIBER_TERMINATED_P(old_fiber) ? NULL : th->ec->machine.stack_end;
1564
1565
1566 // if (DEBUG) fprintf(stderr, "fiber_setcontext: %p[%p] -> %p[%p]\n", (void*)old_fiber, old_fiber->stack.base, (void*)new_fiber, new_fiber->stack.base);
1567
1568#if defined(COROUTINE_SANITIZE_ADDRESS)
1569 __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);
1570#endif
1571
1572 /* swap machine context */
1573 struct coroutine_context * from = coroutine_transfer(&old_fiber->context, &new_fiber->context);
1574
1575#if defined(COROUTINE_SANITIZE_ADDRESS)
1576 __sanitizer_finish_switch_fiber(old_fiber->context.fake_stack, NULL, NULL);
1577#endif
1578
1579 if (from == NULL) {
1580 rb_syserr_fail(errno, "coroutine_transfer");
1581 }
1582
1583 /* restore thread context */
1584 fiber_restore_thread(th, old_fiber);
1585
1586 // It's possible to get here, and new_fiber is already freed.
1587 // if (DEBUG) fprintf(stderr, "fiber_setcontext: %p[%p] <- %p[%p]\n", (void*)old_fiber, old_fiber->stack.base, (void*)new_fiber, new_fiber->stack.base);
1588}
1589
1590NOINLINE(NORETURN(static void cont_restore_1(rb_context_t *)));
1591
1592static void
1593cont_restore_1(rb_context_t *cont)
1594{
1595 cont_restore_thread(cont);
1596
1597 /* restore machine stack */
1598#if defined(_M_AMD64) && !defined(__MINGW64__)
1599 {
1600 /* workaround for x64 SEH */
1601 jmp_buf buf;
1602 setjmp(buf);
1603 _JUMP_BUFFER *bp = (void*)&cont->jmpbuf;
1604 bp->Frame = ((_JUMP_BUFFER*)((void*)&buf))->Frame;
1605 }
1606#endif
1607 if (cont->machine.stack_src) {
1608 FLUSH_REGISTER_WINDOWS;
1609 MEMCPY(cont->machine.stack_src, cont->machine.stack,
1610 VALUE, cont->machine.stack_size);
1611 }
1612
1613 ruby_longjmp(cont->jmpbuf, 1);
1614}
1615
1616NORETURN(NOINLINE(static void cont_restore_0(rb_context_t *, VALUE *)));
1617
1618static void
1619cont_restore_0(rb_context_t *cont, VALUE *addr_in_prev_frame)
1620{
1621 if (cont->machine.stack_src) {
1622#ifdef HAVE_ALLOCA
1623#define STACK_PAD_SIZE 1
1624#else
1625#define STACK_PAD_SIZE 1024
1626#endif
1627 VALUE space[STACK_PAD_SIZE];
1628
1629#if !STACK_GROW_DIRECTION
1630 if (addr_in_prev_frame > &space[0]) {
1631 /* Stack grows downward */
1632#endif
1633#if STACK_GROW_DIRECTION <= 0
1634 volatile VALUE *const end = cont->machine.stack_src;
1635 if (&space[0] > end) {
1636# ifdef HAVE_ALLOCA
1637 volatile VALUE *sp = ALLOCA_N(VALUE, &space[0] - end);
1638 // We need to make sure that the stack pointer is moved,
1639 // but some compilers may remove the allocation by optimization.
1640 // We hope that the following read/write will prevent such an optimization.
1641 *sp = Qfalse;
1642 space[0] = *sp;
1643# else
1644 cont_restore_0(cont, &space[0]);
1645# endif
1646 }
1647#endif
1648#if !STACK_GROW_DIRECTION
1649 }
1650 else {
1651 /* Stack grows upward */
1652#endif
1653#if STACK_GROW_DIRECTION >= 0
1654 volatile VALUE *const end = cont->machine.stack_src + cont->machine.stack_size;
1655 if (&space[STACK_PAD_SIZE] < end) {
1656# ifdef HAVE_ALLOCA
1657 volatile VALUE *sp = ALLOCA_N(VALUE, end - &space[STACK_PAD_SIZE]);
1658 space[0] = *sp;
1659# else
1660 cont_restore_0(cont, &space[STACK_PAD_SIZE-1]);
1661# endif
1662 }
1663#endif
1664#if !STACK_GROW_DIRECTION
1665 }
1666#endif
1667 }
1668 cont_restore_1(cont);
1669}
1670
1671/*
1672 * Document-class: Continuation
1673 *
1674 * Continuation objects are generated by Kernel#callcc,
1675 * after having +require+d <i>continuation</i>. They hold
1676 * a return address and execution context, allowing a nonlocal return
1677 * to the end of the #callcc block from anywhere within a
1678 * program. Continuations are somewhat analogous to a structured
1679 * version of C's <code>setjmp/longjmp</code> (although they contain
1680 * more state, so you might consider them closer to threads).
1681 *
1682 * For instance:
1683 *
1684 * require "continuation"
1685 * arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ]
1686 * callcc{|cc| $cc = cc}
1687 * puts(message = arr.shift)
1688 * $cc.call unless message =~ /Max/
1689 *
1690 * <em>produces:</em>
1691 *
1692 * Freddie
1693 * Herbie
1694 * Ron
1695 * Max
1696 *
1697 * Also you can call callcc in other methods:
1698 *
1699 * require "continuation"
1700 *
1701 * def g
1702 * arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ]
1703 * cc = callcc { |cc| cc }
1704 * puts arr.shift
1705 * return cc, arr.size
1706 * end
1707 *
1708 * def f
1709 * c, size = g
1710 * c.call(c) if size > 1
1711 * end
1712 *
1713 * f
1714 *
1715 * This (somewhat contrived) example allows the inner loop to abandon
1716 * processing early:
1717 *
1718 * require "continuation"
1719 * callcc {|cont|
1720 * for i in 0..4
1721 * print "#{i}: "
1722 * for j in i*5...(i+1)*5
1723 * cont.call() if j == 17
1724 * printf "%3d", j
1725 * end
1726 * end
1727 * }
1728 * puts
1729 *
1730 * <em>produces:</em>
1731 *
1732 * 0: 0 1 2 3 4
1733 * 1: 5 6 7 8 9
1734 * 2: 10 11 12 13 14
1735 * 3: 15 16
1736 */
1737
1738/*
1739 * call-seq:
1740 * callcc {|cont| block } -> obj
1741 *
1742 * Generates a Continuation object, which it passes to
1743 * the associated block. You need to <code>require
1744 * 'continuation'</code> before using this method. Performing a
1745 * <em>cont</em><code>.call</code> will cause the #callcc
1746 * to return (as will falling through the end of the block). The
1747 * value returned by the #callcc is the value of the
1748 * block, or the value passed to <em>cont</em><code>.call</code>. See
1749 * class Continuation for more details. Also see
1750 * Kernel#throw for an alternative mechanism for
1751 * unwinding a call stack.
1752 */
1753
1754static VALUE
1755rb_callcc(VALUE self)
1756{
1757 volatile int called;
1758 volatile VALUE val = cont_capture(&called);
1759
1760 if (called) {
1761 return val;
1762 }
1763 else {
1764 return rb_yield(val);
1765 }
1766}
1767#ifdef RUBY_ASAN_ENABLED
1768/* callcc can't possibly work with ASAN; see bug #20273. Also this function
1769 * definition below avoids a "defined and not used" warning. */
1770MAYBE_UNUSED(static void notusing_callcc(void)) { rb_callcc(Qnil); }
1771# define rb_callcc rb_f_notimplement
1772#endif
1773
1774
1775static VALUE
1776make_passing_arg(int argc, const VALUE *argv)
1777{
1778 switch (argc) {
1779 case -1:
1780 return argv[0];
1781 case 0:
1782 return Qnil;
1783 case 1:
1784 return argv[0];
1785 default:
1786 return rb_ary_new4(argc, argv);
1787 }
1788}
1789
1790typedef VALUE e_proc(VALUE);
1791
1792NORETURN(static VALUE rb_cont_call(int argc, VALUE *argv, VALUE contval));
1793
1794/*
1795 * call-seq:
1796 * cont.call(args, ...)
1797 * cont[args, ...]
1798 *
1799 * Invokes the continuation. The program continues from the end of
1800 * the #callcc block. If no arguments are given, the original #callcc
1801 * returns +nil+. If one argument is given, #callcc returns
1802 * it. Otherwise, an array containing <i>args</i> is returned.
1803 *
1804 * callcc {|cont| cont.call } #=> nil
1805 * callcc {|cont| cont.call 1 } #=> 1
1806 * callcc {|cont| cont.call 1, 2, 3 } #=> [1, 2, 3]
1807 */
1808
1809static VALUE
1810rb_cont_call(int argc, VALUE *argv, VALUE contval)
1811{
1812 rb_context_t *cont = cont_ptr(contval);
1813 rb_thread_t *th = GET_THREAD();
1814
1815 if (cont_thread_value(cont) != th->self) {
1816 rb_raise(rb_eRuntimeError, "continuation called across threads");
1817 }
1818 if (cont->saved_ec.fiber_ptr) {
1819 if (th->ec->fiber_ptr != cont->saved_ec.fiber_ptr) {
1820 rb_raise(rb_eRuntimeError, "continuation called across fiber");
1821 }
1822 }
1823
1824 cont->argc = argc;
1825 cont->value = make_passing_arg(argc, argv);
1826
1827 cont_restore_0(cont, &contval);
1829}
1830
1831/*********/
1832/* fiber */
1833/*********/
1834
1835/*
1836 * Document-class: Fiber
1837 *
1838 * Fibers are primitives for implementing light weight cooperative
1839 * concurrency in Ruby. Basically they are a means of creating code blocks
1840 * that can be paused and resumed, much like threads. The main difference
1841 * is that they are never preempted and that the scheduling must be done by
1842 * the programmer and not the VM.
1843 *
1844 * As opposed to other stackless light weight concurrency models, each fiber
1845 * comes with a stack. This enables the fiber to be paused from deeply
1846 * nested function calls within the fiber block. See the ruby(1)
1847 * manpage to configure the size of the fiber stack(s).
1848 *
1849 * When a fiber is created it will not run automatically. Rather it must
1850 * be explicitly asked to run using the Fiber#resume method.
1851 * The code running inside the fiber can give up control by calling
1852 * Fiber.yield in which case it yields control back to caller (the
1853 * caller of the Fiber#resume).
1854 *
1855 * Upon yielding or termination the Fiber returns the value of the last
1856 * executed expression
1857 *
1858 * For instance:
1859 *
1860 * fiber = Fiber.new do
1861 * Fiber.yield 1
1862 * 2
1863 * end
1864 *
1865 * puts fiber.resume
1866 * puts fiber.resume
1867 * puts fiber.resume
1868 *
1869 * <em>produces</em>
1870 *
1871 * 1
1872 * 2
1873 * FiberError: dead fiber called
1874 *
1875 * The Fiber#resume method accepts an arbitrary number of parameters,
1876 * if it is the first call to #resume then they will be passed as
1877 * block arguments. Otherwise they will be the return value of the
1878 * call to Fiber.yield
1879 *
1880 * Example:
1881 *
1882 * fiber = Fiber.new do |first|
1883 * second = Fiber.yield first + 2
1884 * end
1885 *
1886 * puts fiber.resume 10
1887 * puts fiber.resume 1_000_000
1888 * puts fiber.resume "The fiber will be dead before I can cause trouble"
1889 *
1890 * <em>produces</em>
1891 *
1892 * 12
1893 * 1000000
1894 * FiberError: dead fiber called
1895 *
1896 * == Non-blocking Fibers
1897 *
1898 * The concept of <em>non-blocking fiber</em> was introduced in Ruby 3.0.
1899 * A non-blocking fiber, when reaching a operation that would normally block
1900 * the fiber (like <code>sleep</code>, or wait for another process or I/O)
1901 * will yield control to other fibers and allow the <em>scheduler</em> to
1902 * handle blocking and waking up (resuming) this fiber when it can proceed.
1903 *
1904 * For a Fiber to behave as non-blocking, it need to be created in Fiber.new with
1905 * <tt>blocking: false</tt> (which is the default), and Fiber.scheduler
1906 * should be set with Fiber.set_scheduler. If Fiber.scheduler is not set in
1907 * the current thread, blocking and non-blocking fibers' behavior is identical.
1908 *
1909 * Ruby doesn't provide a scheduler class: it is expected to be implemented by
1910 * the user and correspond to Fiber::Scheduler.
1911 *
1912 * There is also Fiber.schedule method, which is expected to immediately perform
1913 * the given block in a non-blocking manner. Its actual implementation is up to
1914 * the scheduler.
1915 *
1916 */
1917
1918static const rb_data_type_t fiber_data_type = {
1919 "fiber",
1920 {fiber_mark, fiber_free, fiber_memsize, fiber_compact,},
1921 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1922};
1923
1924static VALUE
1925fiber_alloc(VALUE klass)
1926{
1927 return TypedData_Wrap_Struct(klass, &fiber_data_type, 0);
1928}
1929
1930static rb_fiber_t*
1931fiber_t_alloc(VALUE fiber_value, unsigned int blocking)
1932{
1933 rb_fiber_t *fiber;
1934 rb_thread_t *th = GET_THREAD();
1935
1936 if (DATA_PTR(fiber_value) != 0) {
1937 rb_raise(rb_eRuntimeError, "cannot initialize twice");
1938 }
1939
1940 THREAD_MUST_BE_RUNNING(th);
1941 fiber = ZALLOC(rb_fiber_t);
1942 fiber->cont.self = fiber_value;
1943 fiber->cont.type = FIBER_CONTEXT;
1944 fiber->blocking = blocking;
1945 fiber->killed = 0;
1946 cont_init(&fiber->cont, th);
1947
1948 fiber->cont.saved_ec.fiber_ptr = fiber;
1949 rb_ec_clear_vm_stack(&fiber->cont.saved_ec);
1950
1951 fiber->prev = NULL;
1952
1953 /* fiber->status == 0 == CREATED
1954 * So that we don't need to set status: fiber_status_set(fiber, FIBER_CREATED); */
1955 VM_ASSERT(FIBER_CREATED_P(fiber));
1956
1957 DATA_PTR(fiber_value) = fiber;
1958
1959 return fiber;
1960}
1961
1962static rb_fiber_t *
1963root_fiber_alloc(rb_thread_t *th)
1964{
1965 VALUE fiber_value = fiber_alloc(rb_cFiber);
1966 rb_fiber_t *fiber = th->ec->fiber_ptr;
1967
1968 VM_ASSERT(DATA_PTR(fiber_value) == NULL);
1969 VM_ASSERT(fiber->cont.type == FIBER_CONTEXT);
1970 VM_ASSERT(FIBER_RESUMED_P(fiber));
1971
1972 th->root_fiber = fiber;
1973 DATA_PTR(fiber_value) = fiber;
1974 fiber->cont.self = fiber_value;
1975
1976 coroutine_initialize_main(&fiber->context);
1977
1978 return fiber;
1979}
1980
1981static inline rb_fiber_t*
1982fiber_current(void)
1983{
1984 rb_execution_context_t *ec = GET_EC();
1985 if (ec->fiber_ptr->cont.self == 0) {
1986 root_fiber_alloc(rb_ec_thread_ptr(ec));
1987 }
1988 return ec->fiber_ptr;
1989}
1990
1991static inline VALUE
1992current_fiber_storage(void)
1993{
1994 rb_execution_context_t *ec = GET_EC();
1995 return ec->storage;
1996}
1997
1998static inline VALUE
1999inherit_fiber_storage(void)
2000{
2001 return rb_obj_dup(current_fiber_storage());
2002}
2003
2004static inline void
2005fiber_storage_set(struct rb_fiber_struct *fiber, VALUE storage)
2006{
2007 fiber->cont.saved_ec.storage = storage;
2008}
2009
2010static inline VALUE
2011fiber_storage_get(rb_fiber_t *fiber, int allocate)
2012{
2013 VALUE storage = fiber->cont.saved_ec.storage;
2014 if (storage == Qnil && allocate) {
2015 storage = rb_hash_new();
2016 fiber_storage_set(fiber, storage);
2017 }
2018 return storage;
2019}
2020
2021static void
2022storage_access_must_be_from_same_fiber(VALUE self)
2023{
2024 rb_fiber_t *fiber = fiber_ptr(self);
2025 rb_fiber_t *current = fiber_current();
2026 if (fiber != current) {
2027 rb_raise(rb_eArgError, "Fiber storage can only be accessed from the Fiber it belongs to");
2028 }
2029}
2030
2037static VALUE
2038rb_fiber_storage_get(VALUE self)
2039{
2040 storage_access_must_be_from_same_fiber(self);
2041
2042 VALUE storage = fiber_storage_get(fiber_ptr(self), FALSE);
2043
2044 if (storage == Qnil) {
2045 return Qnil;
2046 }
2047 else {
2048 return rb_obj_dup(storage);
2049 }
2050}
2051
2052static int
2053fiber_storage_validate_each(VALUE key, VALUE value, VALUE _argument)
2054{
2055 Check_Type(key, T_SYMBOL);
2056
2057 return ST_CONTINUE;
2058}
2059
2060static void
2061fiber_storage_validate(VALUE value)
2062{
2063 // nil is an allowed value and will be lazily initialized.
2064 if (value == Qnil) return;
2065
2066 if (!RB_TYPE_P(value, T_HASH)) {
2067 rb_raise(rb_eTypeError, "storage must be a hash");
2068 }
2069
2070 if (RB_OBJ_FROZEN(value)) {
2071 rb_raise(rb_eFrozenError, "storage must not be frozen");
2072 }
2073
2074 rb_hash_foreach(value, fiber_storage_validate_each, Qundef);
2075}
2076
2099static VALUE
2100rb_fiber_storage_set(VALUE self, VALUE value)
2101{
2102 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL)) {
2104 "Fiber#storage= is experimental and may be removed in the future!");
2105 }
2106
2107 storage_access_must_be_from_same_fiber(self);
2108 fiber_storage_validate(value);
2109
2110 fiber_ptr(self)->cont.saved_ec.storage = rb_obj_dup(value);
2111 return value;
2112}
2113
2124static VALUE
2125rb_fiber_storage_aref(VALUE class, VALUE key)
2126{
2127 key = rb_to_symbol(key);
2128
2129 VALUE storage = fiber_storage_get(fiber_current(), FALSE);
2130 if (storage == Qnil) return Qnil;
2131
2132 return rb_hash_aref(storage, key);
2133}
2134
2145static VALUE
2146rb_fiber_storage_aset(VALUE class, VALUE key, VALUE value)
2147{
2148 key = rb_to_symbol(key);
2149
2150 VALUE storage = fiber_storage_get(fiber_current(), value != Qnil);
2151 if (storage == Qnil) return Qnil;
2152
2153 if (value == Qnil) {
2154 return rb_hash_delete(storage, key);
2155 }
2156 else {
2157 return rb_hash_aset(storage, key, value);
2158 }
2159}
2160
2161static VALUE
2162fiber_initialize(VALUE self, VALUE proc, struct fiber_pool * fiber_pool, unsigned int blocking, VALUE storage)
2163{
2164 if (storage == Qundef || storage == Qtrue) {
2165 // The default, inherit storage (dup) from the current fiber:
2166 storage = inherit_fiber_storage();
2167 }
2168 else /* nil, hash, etc. */ {
2169 fiber_storage_validate(storage);
2170 storage = rb_obj_dup(storage);
2171 }
2172
2173 rb_fiber_t *fiber = fiber_t_alloc(self, blocking);
2174
2175 fiber->cont.saved_ec.storage = storage;
2176 fiber->first_proc = proc;
2177 fiber->stack.base = NULL;
2178 fiber->stack.pool = fiber_pool;
2179
2180 return self;
2181}
2182
2183static void
2184fiber_prepare_stack(rb_fiber_t *fiber)
2185{
2186 rb_context_t *cont = &fiber->cont;
2187 rb_execution_context_t *sec = &cont->saved_ec;
2188
2189 size_t vm_stack_size = 0;
2190 VALUE *vm_stack = fiber_initialize_coroutine(fiber, &vm_stack_size);
2191
2192 /* initialize cont */
2193 cont->saved_vm_stack.ptr = NULL;
2194 rb_ec_initialize_vm_stack(sec, vm_stack, vm_stack_size / sizeof(VALUE));
2195
2196 sec->tag = NULL;
2197 sec->local_storage = NULL;
2198 sec->local_storage_recursive_hash = Qnil;
2199 sec->local_storage_recursive_hash_for_trace = Qnil;
2200}
2201
2202static struct fiber_pool *
2203rb_fiber_pool_default(VALUE pool)
2204{
2205 return &shared_fiber_pool;
2206}
2207
2208VALUE rb_fiber_inherit_storage(struct rb_execution_context_struct *ec, struct rb_fiber_struct *fiber)
2209{
2210 VALUE storage = rb_obj_dup(ec->storage);
2211 fiber->cont.saved_ec.storage = storage;
2212 return storage;
2213}
2214
2215/* :nodoc: */
2216static VALUE
2217rb_fiber_initialize_kw(int argc, VALUE* argv, VALUE self, int kw_splat)
2218{
2219 VALUE pool = Qnil;
2220 VALUE blocking = Qfalse;
2221 VALUE storage = Qundef;
2222
2223 if (kw_splat != RB_NO_KEYWORDS) {
2224 VALUE options = Qnil;
2225 VALUE arguments[3] = {Qundef};
2226
2227 argc = rb_scan_args_kw(kw_splat, argc, argv, ":", &options);
2228 rb_get_kwargs(options, fiber_initialize_keywords, 0, 3, arguments);
2229
2230 if (!UNDEF_P(arguments[0])) {
2231 blocking = arguments[0];
2232 }
2233
2234 if (!UNDEF_P(arguments[1])) {
2235 pool = arguments[1];
2236 }
2237
2238 storage = arguments[2];
2239 }
2240
2241 return fiber_initialize(self, rb_block_proc(), rb_fiber_pool_default(pool), RTEST(blocking), storage);
2242}
2243
2244/*
2245 * call-seq:
2246 * Fiber.new(blocking: false, storage: true) { |*args| ... } -> fiber
2247 *
2248 * Creates new Fiber. Initially, the fiber is not running and can be resumed
2249 * with #resume. Arguments to the first #resume call will be passed to the
2250 * block:
2251 *
2252 * f = Fiber.new do |initial|
2253 * current = initial
2254 * loop do
2255 * puts "current: #{current.inspect}"
2256 * current = Fiber.yield
2257 * end
2258 * end
2259 * f.resume(100) # prints: current: 100
2260 * f.resume(1, 2, 3) # prints: current: [1, 2, 3]
2261 * f.resume # prints: current: nil
2262 * # ... and so on ...
2263 *
2264 * If <tt>blocking: false</tt> is passed to <tt>Fiber.new</tt>, _and_ current
2265 * thread has a Fiber.scheduler defined, the Fiber becomes non-blocking (see
2266 * "Non-blocking Fibers" section in class docs).
2267 *
2268 * If the <tt>storage</tt> is unspecified, the default is to inherit a copy of
2269 * the storage from the current fiber. This is the same as specifying
2270 * <tt>storage: true</tt>.
2271 *
2272 * Fiber[:x] = 1
2273 * Fiber.new do
2274 * Fiber[:x] # => 1
2275 * Fiber[:x] = 2
2276 * end.resume
2277 * Fiber[:x] # => 1
2278 *
2279 * If the given <tt>storage</tt> is <tt>nil</tt>, this function will lazy
2280 * initialize the internal storage, which starts as an empty hash.
2281 *
2282 * Fiber[:x] = "Hello World"
2283 * Fiber.new(storage: nil) do
2284 * Fiber[:x] # nil
2285 * end
2286 *
2287 * Otherwise, the given <tt>storage</tt> is used as the new fiber's storage,
2288 * and it must be an instance of Hash.
2289 *
2290 * Explicitly using <tt>storage: true</tt> is currently experimental and may
2291 * change in the future.
2292 */
2293static VALUE
2294rb_fiber_initialize(int argc, VALUE* argv, VALUE self)
2295{
2296 return rb_fiber_initialize_kw(argc, argv, self, rb_keyword_given_p());
2297}
2298
2299VALUE
2300rb_fiber_new_storage(rb_block_call_func_t func, VALUE obj, VALUE storage)
2301{
2302 return fiber_initialize(fiber_alloc(rb_cFiber), rb_proc_new(func, obj), rb_fiber_pool_default(Qnil), 0, storage);
2303}
2304
2305VALUE
2306rb_fiber_new(rb_block_call_func_t func, VALUE obj)
2307{
2308 return rb_fiber_new_storage(func, obj, Qtrue);
2309}
2310
2311static VALUE
2312rb_fiber_s_schedule_kw(int argc, VALUE* argv, int kw_splat)
2313{
2314 rb_thread_t * th = GET_THREAD();
2315 VALUE scheduler = th->scheduler;
2316 VALUE fiber = Qnil;
2317
2318 if (scheduler != Qnil) {
2319 fiber = rb_fiber_scheduler_fiber(scheduler, argc, argv, kw_splat);
2320 }
2321 else {
2322 rb_raise(rb_eRuntimeError, "No scheduler is available!");
2323 }
2324
2325 return fiber;
2326}
2327
2328/*
2329 * call-seq:
2330 * Fiber.schedule { |*args| ... } -> fiber
2331 *
2332 * The method is <em>expected</em> to immediately run the provided block of code in a
2333 * separate non-blocking fiber.
2334 *
2335 * puts "Go to sleep!"
2336 *
2337 * Fiber.set_scheduler(MyScheduler.new)
2338 *
2339 * Fiber.schedule do
2340 * puts "Going to sleep"
2341 * sleep(1)
2342 * puts "I slept well"
2343 * end
2344 *
2345 * puts "Wakey-wakey, sleepyhead"
2346 *
2347 * Assuming MyScheduler is properly implemented, this program will produce:
2348 *
2349 * Go to sleep!
2350 * Going to sleep
2351 * Wakey-wakey, sleepyhead
2352 * ...1 sec pause here...
2353 * I slept well
2354 *
2355 * ...e.g. on the first blocking operation inside the Fiber (<tt>sleep(1)</tt>),
2356 * the control is yielded to the outside code (main fiber), and <em>at the end
2357 * of that execution</em>, the scheduler takes care of properly resuming all the
2358 * blocked fibers.
2359 *
2360 * Note that the behavior described above is how the method is <em>expected</em>
2361 * to behave, actual behavior is up to the current scheduler's implementation of
2362 * Fiber::Scheduler#fiber method. Ruby doesn't enforce this method to
2363 * behave in any particular way.
2364 *
2365 * If the scheduler is not set, the method raises
2366 * <tt>RuntimeError (No scheduler is available!)</tt>.
2367 *
2368 */
2369static VALUE
2370rb_fiber_s_schedule(int argc, VALUE *argv, VALUE obj)
2371{
2372 return rb_fiber_s_schedule_kw(argc, argv, rb_keyword_given_p());
2373}
2374
2375/*
2376 * call-seq:
2377 * Fiber.scheduler -> obj or nil
2378 *
2379 * Returns the Fiber scheduler, that was last set for the current thread with Fiber.set_scheduler.
2380 * Returns +nil+ if no scheduler is set (which is the default), and non-blocking fibers'
2381 * behavior is the same as blocking.
2382 * (see "Non-blocking fibers" section in class docs for details about the scheduler concept).
2383 *
2384 */
2385static VALUE
2386rb_fiber_s_scheduler(VALUE klass)
2387{
2388 return rb_fiber_scheduler_get();
2389}
2390
2391/*
2392 * call-seq:
2393 * Fiber.current_scheduler -> obj or nil
2394 *
2395 * Returns the Fiber scheduler, that was last set for the current thread with Fiber.set_scheduler
2396 * if and only if the current fiber is non-blocking.
2397 *
2398 */
2399static VALUE
2400rb_fiber_current_scheduler(VALUE klass)
2401{
2403}
2404
2405/*
2406 * call-seq:
2407 * Fiber.set_scheduler(scheduler) -> scheduler
2408 *
2409 * Sets the Fiber scheduler for the current thread. If the scheduler is set, non-blocking
2410 * fibers (created by Fiber.new with <tt>blocking: false</tt>, or by Fiber.schedule)
2411 * call that scheduler's hook methods on potentially blocking operations, and the current
2412 * thread will call scheduler's +close+ method on finalization (allowing the scheduler to
2413 * properly manage all non-finished fibers).
2414 *
2415 * +scheduler+ can be an object of any class corresponding to Fiber::Scheduler. Its
2416 * implementation is up to the user.
2417 *
2418 * See also the "Non-blocking fibers" section in class docs.
2419 *
2420 */
2421static VALUE
2422rb_fiber_set_scheduler(VALUE klass, VALUE scheduler)
2423{
2424 return rb_fiber_scheduler_set(scheduler);
2425}
2426
2427NORETURN(static void rb_fiber_terminate(rb_fiber_t *fiber, int need_interrupt, VALUE err));
2428
2429void
2430rb_fiber_start(rb_fiber_t *fiber)
2431{
2432 rb_thread_t * volatile th = fiber->cont.saved_ec.thread_ptr;
2433
2434 rb_proc_t *proc;
2435 enum ruby_tag_type state;
2436
2437 VM_ASSERT(th->ec == GET_EC());
2438 VM_ASSERT(FIBER_RESUMED_P(fiber));
2439
2440 if (fiber->blocking) {
2441 th->blocking += 1;
2442 }
2443
2444 EC_PUSH_TAG(th->ec);
2445 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2446 rb_context_t *cont = &VAR_FROM_MEMORY(fiber)->cont;
2447 int argc;
2448 const VALUE *argv, args = cont->value;
2449 GetProcPtr(fiber->first_proc, proc);
2450 argv = (argc = cont->argc) > 1 ? RARRAY_CONST_PTR(args) : &args;
2451 cont->value = Qnil;
2452 th->ec->errinfo = Qnil;
2453 th->ec->root_lep = rb_vm_proc_local_ep(fiber->first_proc);
2454 th->ec->root_svar = Qfalse;
2455
2456 EXEC_EVENT_HOOK(th->ec, RUBY_EVENT_FIBER_SWITCH, th->self, 0, 0, 0, Qnil);
2457 cont->value = rb_vm_invoke_proc(th->ec, proc, argc, argv, cont->kw_splat, VM_BLOCK_HANDLER_NONE);
2458 }
2459 EC_POP_TAG();
2460
2461 int need_interrupt = TRUE;
2462 VALUE err = Qfalse;
2463 if (state) {
2464 err = th->ec->errinfo;
2465 VM_ASSERT(FIBER_RESUMED_P(fiber));
2466
2467 if (state == TAG_RAISE) {
2468 // noop...
2469 }
2470 else if (state == TAG_FATAL && err == RUBY_FATAL_FIBER_KILLED) {
2471 need_interrupt = FALSE;
2472 err = Qfalse;
2473 }
2474 else if (state == TAG_FATAL) {
2475 rb_threadptr_pending_interrupt_enque(th, err);
2476 }
2477 else {
2478 err = rb_vm_make_jump_tag_but_local_jump(state, err);
2479 }
2480 }
2481
2482 rb_fiber_terminate(fiber, need_interrupt, err);
2483}
2484
2485// Set up a "root fiber", which is the fiber that every Ractor has.
2486void
2487rb_threadptr_root_fiber_setup(rb_thread_t *th)
2488{
2489 rb_fiber_t *fiber = ruby_mimcalloc(1, sizeof(rb_fiber_t));
2490 if (!fiber) {
2491 rb_bug("%s", strerror(errno)); /* ... is it possible to call rb_bug here? */
2492 }
2493 fiber->cont.type = FIBER_CONTEXT;
2494 fiber->cont.saved_ec.fiber_ptr = fiber;
2495 fiber->cont.saved_ec.thread_ptr = th;
2496 fiber->blocking = 1;
2497 fiber->killed = 0;
2498 fiber_status_set(fiber, FIBER_RESUMED); /* skip CREATED */
2499 th->ec = &fiber->cont.saved_ec;
2500 cont_init_jit_cont(&fiber->cont);
2501}
2502
2503void
2504rb_threadptr_root_fiber_release(rb_thread_t *th)
2505{
2506 if (th->root_fiber) {
2507 /* ignore. A root fiber object will free th->ec */
2508 }
2509 else {
2510 rb_execution_context_t *ec = rb_current_execution_context(false);
2511
2512 VM_ASSERT(th->ec->fiber_ptr->cont.type == FIBER_CONTEXT);
2513 VM_ASSERT(th->ec->fiber_ptr->cont.self == 0);
2514
2515 if (ec && th->ec == ec) {
2516 rb_ractor_set_current_ec(th->ractor, NULL);
2517 }
2518 fiber_free(th->ec->fiber_ptr);
2519 th->ec = NULL;
2520 }
2521}
2522
2523void
2524rb_threadptr_root_fiber_terminate(rb_thread_t *th)
2525{
2526 rb_fiber_t *fiber = th->ec->fiber_ptr;
2527
2528 fiber->status = FIBER_TERMINATED;
2529
2530 // The vm_stack is `alloca`ed on the thread stack, so it's gone too:
2531 rb_ec_clear_vm_stack(th->ec);
2532}
2533
2534static inline rb_fiber_t*
2535return_fiber(bool terminate)
2536{
2537 rb_fiber_t *fiber = fiber_current();
2538 rb_fiber_t *prev = fiber->prev;
2539
2540 if (prev) {
2541 fiber->prev = NULL;
2542 prev->resuming_fiber = NULL;
2543 return prev;
2544 }
2545 else {
2546 if (!terminate) {
2547 rb_raise(rb_eFiberError, "attempt to yield on a not resumed fiber");
2548 }
2549
2550 rb_thread_t *th = GET_THREAD();
2551 rb_fiber_t *root_fiber = th->root_fiber;
2552
2553 VM_ASSERT(root_fiber != NULL);
2554
2555 // search resuming fiber
2556 for (fiber = root_fiber; fiber->resuming_fiber; fiber = fiber->resuming_fiber) {
2557 }
2558
2559 return fiber;
2560 }
2561}
2562
2563VALUE
2564rb_fiber_current(void)
2565{
2566 return fiber_current()->cont.self;
2567}
2568
2569// Prepare to execute next_fiber on the given thread.
2570static inline void
2571fiber_store(rb_fiber_t *next_fiber, rb_thread_t *th)
2572{
2573 rb_fiber_t *fiber;
2574
2575 if (th->ec->fiber_ptr != NULL) {
2576 fiber = th->ec->fiber_ptr;
2577 }
2578 else {
2579 /* create root fiber */
2580 fiber = root_fiber_alloc(th);
2581 }
2582
2583 if (FIBER_CREATED_P(next_fiber)) {
2584 fiber_prepare_stack(next_fiber);
2585 }
2586
2587 VM_ASSERT(FIBER_RESUMED_P(fiber) || FIBER_TERMINATED_P(fiber));
2588 VM_ASSERT(FIBER_RUNNABLE_P(next_fiber));
2589
2590 if (FIBER_RESUMED_P(fiber)) fiber_status_set(fiber, FIBER_SUSPENDED);
2591
2592 fiber_status_set(next_fiber, FIBER_RESUMED);
2593 fiber_setcontext(next_fiber, fiber);
2594}
2595
2596static void
2597fiber_check_killed(rb_fiber_t *fiber)
2598{
2599 VM_ASSERT(fiber == fiber_current());
2600
2601 if (fiber->killed) {
2602 rb_thread_t *thread = fiber->cont.saved_ec.thread_ptr;
2603
2604 thread->ec->errinfo = RUBY_FATAL_FIBER_KILLED;
2605 EC_JUMP_TAG(thread->ec, RUBY_TAG_FATAL);
2606 }
2607}
2608
2609static inline VALUE
2610fiber_switch(rb_fiber_t *fiber, int argc, const VALUE *argv, int kw_splat, rb_fiber_t *resuming_fiber, bool yielding)
2611{
2612 VALUE value;
2613 rb_context_t *cont = &fiber->cont;
2614 rb_thread_t *th = GET_THREAD();
2615
2616 /* make sure the root_fiber object is available */
2617 if (th->root_fiber == NULL) root_fiber_alloc(th);
2618
2619 if (th->ec->fiber_ptr == fiber) {
2620 /* ignore fiber context switch
2621 * because destination fiber is the same as current fiber
2622 */
2623 return make_passing_arg(argc, argv);
2624 }
2625
2626 if (cont_thread_value(cont) != th->self) {
2627 rb_raise(rb_eFiberError, "fiber called across threads");
2628 }
2629
2630 if (FIBER_TERMINATED_P(fiber)) {
2631 value = rb_exc_new2(rb_eFiberError, "dead fiber called");
2632
2633 if (!FIBER_TERMINATED_P(th->ec->fiber_ptr)) {
2634 rb_exc_raise(value);
2635 VM_UNREACHABLE(fiber_switch);
2636 }
2637 else {
2638 /* th->ec->fiber_ptr is also dead => switch to root fiber */
2639 /* (this means we're being called from rb_fiber_terminate, */
2640 /* and the terminated fiber's return_fiber() is already dead) */
2641 VM_ASSERT(FIBER_SUSPENDED_P(th->root_fiber));
2642
2643 cont = &th->root_fiber->cont;
2644 cont->argc = -1;
2645 cont->value = value;
2646
2647 fiber_setcontext(th->root_fiber, th->ec->fiber_ptr);
2648
2649 VM_UNREACHABLE(fiber_switch);
2650 }
2651 }
2652
2653 VM_ASSERT(FIBER_RUNNABLE_P(fiber));
2654
2655 rb_fiber_t *current_fiber = fiber_current();
2656
2657 VM_ASSERT(!current_fiber->resuming_fiber);
2658
2659 if (resuming_fiber) {
2660 current_fiber->resuming_fiber = resuming_fiber;
2661 fiber->prev = fiber_current();
2662 fiber->yielding = 0;
2663 }
2664
2665 VM_ASSERT(!current_fiber->yielding);
2666 if (yielding) {
2667 current_fiber->yielding = 1;
2668 }
2669
2670 if (current_fiber->blocking) {
2671 th->blocking -= 1;
2672 }
2673
2674 cont->argc = argc;
2675 cont->kw_splat = kw_splat;
2676 cont->value = make_passing_arg(argc, argv);
2677
2678 fiber_store(fiber, th);
2679
2680 // We cannot free the stack until the pthread is joined:
2681#ifndef COROUTINE_PTHREAD_CONTEXT
2682 if (resuming_fiber && FIBER_TERMINATED_P(fiber)) {
2683 fiber_stack_release(fiber);
2684 }
2685#endif
2686
2687 if (fiber_current()->blocking) {
2688 th->blocking += 1;
2689 }
2690
2691 RUBY_VM_CHECK_INTS(th->ec);
2692
2693 EXEC_EVENT_HOOK(th->ec, RUBY_EVENT_FIBER_SWITCH, th->self, 0, 0, 0, Qnil);
2694
2695 current_fiber = th->ec->fiber_ptr;
2696 value = current_fiber->cont.value;
2697
2698 fiber_check_killed(current_fiber);
2699
2700 if (current_fiber->cont.argc == -1) {
2701 // Fiber#raise will trigger this path.
2702 rb_exc_raise(value);
2703 }
2704
2705 return value;
2706}
2707
2708VALUE
2709rb_fiber_transfer(VALUE fiber_value, int argc, const VALUE *argv)
2710{
2711 return fiber_switch(fiber_ptr(fiber_value), argc, argv, RB_NO_KEYWORDS, NULL, false);
2712}
2713
2714/*
2715 * call-seq:
2716 * fiber.blocking? -> true or false
2717 *
2718 * Returns +true+ if +fiber+ is blocking and +false+ otherwise.
2719 * Fiber is non-blocking if it was created via passing <tt>blocking: false</tt>
2720 * to Fiber.new, or via Fiber.schedule.
2721 *
2722 * Note that, even if the method returns +false+, the fiber behaves differently
2723 * only if Fiber.scheduler is set in the current thread.
2724 *
2725 * See the "Non-blocking fibers" section in class docs for details.
2726 *
2727 */
2728VALUE
2729rb_fiber_blocking_p(VALUE fiber)
2730{
2731 return RBOOL(fiber_ptr(fiber)->blocking);
2732}
2733
2734static VALUE
2735fiber_blocking_yield(VALUE fiber_value)
2736{
2737 rb_fiber_t *fiber = fiber_ptr(fiber_value);
2738 rb_thread_t * volatile th = fiber->cont.saved_ec.thread_ptr;
2739
2740 VM_ASSERT(fiber->blocking == 0);
2741
2742 // fiber->blocking is `unsigned int : 1`, so we use it as a boolean:
2743 fiber->blocking = 1;
2744
2745 // Once the fiber is blocking, and current, we increment the thread blocking state:
2746 th->blocking += 1;
2747
2748 return rb_yield(fiber_value);
2749}
2750
2751static VALUE
2752fiber_blocking_ensure(VALUE fiber_value)
2753{
2754 rb_fiber_t *fiber = fiber_ptr(fiber_value);
2755 rb_thread_t * volatile th = fiber->cont.saved_ec.thread_ptr;
2756
2757 // We are no longer blocking:
2758 fiber->blocking = 0;
2759 th->blocking -= 1;
2760
2761 return Qnil;
2762}
2763
2764/*
2765 * call-seq:
2766 * Fiber.blocking{|fiber| ...} -> result
2767 *
2768 * Forces the fiber to be blocking for the duration of the block. Returns the
2769 * result of the block.
2770 *
2771 * See the "Non-blocking fibers" section in class docs for details.
2772 *
2773 */
2774VALUE
2775rb_fiber_blocking(VALUE class)
2776{
2777 VALUE fiber_value = rb_fiber_current();
2778 rb_fiber_t *fiber = fiber_ptr(fiber_value);
2779
2780 // If we are already blocking, this is essentially a no-op:
2781 if (fiber->blocking) {
2782 return rb_yield(fiber_value);
2783 }
2784 else {
2785 return rb_ensure(fiber_blocking_yield, fiber_value, fiber_blocking_ensure, fiber_value);
2786 }
2787}
2788
2789/*
2790 * call-seq:
2791 * Fiber.blocking? -> false or 1
2792 *
2793 * Returns +false+ if the current fiber is non-blocking.
2794 * Fiber is non-blocking if it was created via passing <tt>blocking: false</tt>
2795 * to Fiber.new, or via Fiber.schedule.
2796 *
2797 * If the current Fiber is blocking, the method returns 1.
2798 * Future developments may allow for situations where larger integers
2799 * could be returned.
2800 *
2801 * Note that, even if the method returns +false+, Fiber behaves differently
2802 * only if Fiber.scheduler is set in the current thread.
2803 *
2804 * See the "Non-blocking fibers" section in class docs for details.
2805 *
2806 */
2807static VALUE
2808rb_fiber_s_blocking_p(VALUE klass)
2809{
2810 rb_thread_t *thread = GET_THREAD();
2811 unsigned blocking = thread->blocking;
2812
2813 if (blocking == 0)
2814 return Qfalse;
2815
2816 return INT2NUM(blocking);
2817}
2818
2819void
2820rb_fiber_close(rb_fiber_t *fiber)
2821{
2822 fiber_status_set(fiber, FIBER_TERMINATED);
2823}
2824
2825static void
2826rb_fiber_terminate(rb_fiber_t *fiber, int need_interrupt, VALUE error)
2827{
2828 VALUE value = fiber->cont.value;
2829
2830 VM_ASSERT(FIBER_RESUMED_P(fiber));
2831 rb_fiber_close(fiber);
2832
2833 fiber->cont.machine.stack = NULL;
2834 fiber->cont.machine.stack_size = 0;
2835
2836 rb_fiber_t *next_fiber = return_fiber(true);
2837
2838 if (need_interrupt) RUBY_VM_SET_INTERRUPT(&next_fiber->cont.saved_ec);
2839
2840 if (RTEST(error))
2841 fiber_switch(next_fiber, -1, &error, RB_NO_KEYWORDS, NULL, false);
2842 else
2843 fiber_switch(next_fiber, 1, &value, RB_NO_KEYWORDS, NULL, false);
2844 ruby_stop(0);
2845}
2846
2847static VALUE
2848fiber_resume_kw(rb_fiber_t *fiber, int argc, const VALUE *argv, int kw_splat)
2849{
2850 rb_fiber_t *current_fiber = fiber_current();
2851
2852 if (argc == -1 && FIBER_CREATED_P(fiber)) {
2853 rb_raise(rb_eFiberError, "cannot raise exception on unborn fiber");
2854 }
2855 else if (FIBER_TERMINATED_P(fiber)) {
2856 rb_raise(rb_eFiberError, "attempt to resume a terminated fiber");
2857 }
2858 else if (fiber == current_fiber) {
2859 rb_raise(rb_eFiberError, "attempt to resume the current fiber");
2860 }
2861 else if (fiber->prev != NULL) {
2862 rb_raise(rb_eFiberError, "attempt to resume a resumed fiber (double resume)");
2863 }
2864 else if (fiber->resuming_fiber) {
2865 rb_raise(rb_eFiberError, "attempt to resume a resuming fiber");
2866 }
2867 else if (fiber->prev == NULL &&
2868 (!fiber->yielding && fiber->status != FIBER_CREATED)) {
2869 rb_raise(rb_eFiberError, "attempt to resume a transferring fiber");
2870 }
2871
2872 return fiber_switch(fiber, argc, argv, kw_splat, fiber, false);
2873}
2874
2875VALUE
2876rb_fiber_resume_kw(VALUE self, int argc, const VALUE *argv, int kw_splat)
2877{
2878 return fiber_resume_kw(fiber_ptr(self), argc, argv, kw_splat);
2879}
2880
2881VALUE
2882rb_fiber_resume(VALUE self, int argc, const VALUE *argv)
2883{
2884 return fiber_resume_kw(fiber_ptr(self), argc, argv, RB_NO_KEYWORDS);
2885}
2886
2887VALUE
2888rb_fiber_yield_kw(int argc, const VALUE *argv, int kw_splat)
2889{
2890 return fiber_switch(return_fiber(false), argc, argv, kw_splat, NULL, true);
2891}
2892
2893VALUE
2894rb_fiber_yield(int argc, const VALUE *argv)
2895{
2896 return fiber_switch(return_fiber(false), argc, argv, RB_NO_KEYWORDS, NULL, true);
2897}
2898
2899void
2900rb_fiber_reset_root_local_storage(rb_thread_t *th)
2901{
2902 if (th->root_fiber && th->root_fiber != th->ec->fiber_ptr) {
2903 th->ec->local_storage = th->root_fiber->cont.saved_ec.local_storage;
2904 }
2905}
2906
2907/*
2908 * call-seq:
2909 * fiber.alive? -> true or false
2910 *
2911 * Returns true if the fiber can still be resumed (or transferred
2912 * to). After finishing execution of the fiber block this method will
2913 * always return +false+.
2914 */
2915VALUE
2916rb_fiber_alive_p(VALUE fiber_value)
2917{
2918 return RBOOL(!FIBER_TERMINATED_P(fiber_ptr(fiber_value)));
2919}
2920
2921/*
2922 * call-seq:
2923 * fiber.resume(args, ...) -> obj
2924 *
2925 * Resumes the fiber from the point at which the last Fiber.yield was
2926 * called, or starts running it if it is the first call to
2927 * #resume. Arguments passed to resume will be the value of the
2928 * Fiber.yield expression or will be passed as block parameters to
2929 * the fiber's block if this is the first #resume.
2930 *
2931 * Alternatively, when resume is called it evaluates to the arguments passed
2932 * to the next Fiber.yield statement inside the fiber's block
2933 * or to the block value if it runs to completion without any
2934 * Fiber.yield
2935 */
2936static VALUE
2937rb_fiber_m_resume(int argc, VALUE *argv, VALUE fiber)
2938{
2939 return rb_fiber_resume_kw(fiber, argc, argv, rb_keyword_given_p());
2940}
2941
2942/*
2943 * call-seq:
2944 * fiber.backtrace -> array
2945 * fiber.backtrace(start) -> array
2946 * fiber.backtrace(start, count) -> array
2947 * fiber.backtrace(start..end) -> array
2948 *
2949 * Returns the current execution stack of the fiber. +start+, +count+ and +end+ allow
2950 * to select only parts of the backtrace.
2951 *
2952 * def level3
2953 * Fiber.yield
2954 * end
2955 *
2956 * def level2
2957 * level3
2958 * end
2959 *
2960 * def level1
2961 * level2
2962 * end
2963 *
2964 * f = Fiber.new { level1 }
2965 *
2966 * # It is empty before the fiber started
2967 * f.backtrace
2968 * #=> []
2969 *
2970 * f.resume
2971 *
2972 * f.backtrace
2973 * #=> ["test.rb:2:in `yield'", "test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'", "test.rb:13:in `block in <main>'"]
2974 * p f.backtrace(1) # start from the item 1
2975 * #=> ["test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'", "test.rb:13:in `block in <main>'"]
2976 * p f.backtrace(2, 2) # start from item 2, take 2
2977 * #=> ["test.rb:6:in `level2'", "test.rb:10:in `level1'"]
2978 * p f.backtrace(1..3) # take items from 1 to 3
2979 * #=> ["test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'"]
2980 *
2981 * f.resume
2982 *
2983 * # It is nil after the fiber is finished
2984 * f.backtrace
2985 * #=> nil
2986 *
2987 */
2988static VALUE
2989rb_fiber_backtrace(int argc, VALUE *argv, VALUE fiber)
2990{
2991 return rb_vm_backtrace(argc, argv, &fiber_ptr(fiber)->cont.saved_ec);
2992}
2993
2994/*
2995 * call-seq:
2996 * fiber.backtrace_locations -> array
2997 * fiber.backtrace_locations(start) -> array
2998 * fiber.backtrace_locations(start, count) -> array
2999 * fiber.backtrace_locations(start..end) -> array
3000 *
3001 * Like #backtrace, but returns each line of the execution stack as a
3002 * Thread::Backtrace::Location. Accepts the same arguments as #backtrace.
3003 *
3004 * f = Fiber.new { Fiber.yield }
3005 * f.resume
3006 * loc = f.backtrace_locations.first
3007 * loc.label #=> "yield"
3008 * loc.path #=> "test.rb"
3009 * loc.lineno #=> 1
3010 *
3011 *
3012 */
3013static VALUE
3014rb_fiber_backtrace_locations(int argc, VALUE *argv, VALUE fiber)
3015{
3016 return rb_vm_backtrace_locations(argc, argv, &fiber_ptr(fiber)->cont.saved_ec);
3017}
3018
3019/*
3020 * call-seq:
3021 * fiber.transfer(args, ...) -> obj
3022 *
3023 * Transfer control to another fiber, resuming it from where it last
3024 * stopped or starting it if it was not resumed before. The calling
3025 * fiber will be suspended much like in a call to
3026 * Fiber.yield.
3027 *
3028 * The fiber which receives the transfer call treats it much like
3029 * a resume call. Arguments passed to transfer are treated like those
3030 * passed to resume.
3031 *
3032 * The two style of control passing to and from fiber (one is #resume and
3033 * Fiber::yield, another is #transfer to and from fiber) can't be freely
3034 * mixed.
3035 *
3036 * * If the Fiber's lifecycle had started with transfer, it will never
3037 * be able to yield or be resumed control passing, only
3038 * finish or transfer back. (It still can resume other fibers that
3039 * are allowed to be resumed.)
3040 * * If the Fiber's lifecycle had started with resume, it can yield
3041 * or transfer to another Fiber, but can receive control back only
3042 * the way compatible with the way it was given away: if it had
3043 * transferred, it only can be transferred back, and if it had
3044 * yielded, it only can be resumed back. After that, it again can
3045 * transfer or yield.
3046 *
3047 * If those rules are broken FiberError is raised.
3048 *
3049 * For an individual Fiber design, yield/resume is easier to use
3050 * (the Fiber just gives away control, it doesn't need to think
3051 * about who the control is given to), while transfer is more flexible
3052 * for complex cases, allowing to build arbitrary graphs of Fibers
3053 * dependent on each other.
3054 *
3055 *
3056 * Example:
3057 *
3058 * manager = nil # For local var to be visible inside worker block
3059 *
3060 * # This fiber would be started with transfer
3061 * # It can't yield, and can't be resumed
3062 * worker = Fiber.new { |work|
3063 * puts "Worker: starts"
3064 * puts "Worker: Performed #{work.inspect}, transferring back"
3065 * # Fiber.yield # this would raise FiberError: attempt to yield on a not resumed fiber
3066 * # manager.resume # this would raise FiberError: attempt to resume a resumed fiber (double resume)
3067 * manager.transfer(work.capitalize)
3068 * }
3069 *
3070 * # This fiber would be started with resume
3071 * # It can yield or transfer, and can be transferred
3072 * # back or resumed
3073 * manager = Fiber.new {
3074 * puts "Manager: starts"
3075 * puts "Manager: transferring 'something' to worker"
3076 * result = worker.transfer('something')
3077 * puts "Manager: worker returned #{result.inspect}"
3078 * # worker.resume # this would raise FiberError: attempt to resume a transferring fiber
3079 * Fiber.yield # this is OK, the fiber transferred from and to, now it can yield
3080 * puts "Manager: finished"
3081 * }
3082 *
3083 * puts "Starting the manager"
3084 * manager.resume
3085 * puts "Resuming the manager"
3086 * # manager.transfer # this would raise FiberError: attempt to transfer to a yielding fiber
3087 * manager.resume
3088 *
3089 * <em>produces</em>
3090 *
3091 * Starting the manager
3092 * Manager: starts
3093 * Manager: transferring 'something' to worker
3094 * Worker: starts
3095 * Worker: Performed "something", transferring back
3096 * Manager: worker returned "Something"
3097 * Resuming the manager
3098 * Manager: finished
3099 *
3100 */
3101static VALUE
3102rb_fiber_m_transfer(int argc, VALUE *argv, VALUE self)
3103{
3104 return rb_fiber_transfer_kw(self, argc, argv, rb_keyword_given_p());
3105}
3106
3107static VALUE
3108fiber_transfer_kw(rb_fiber_t *fiber, int argc, const VALUE *argv, int kw_splat)
3109{
3110 if (fiber->resuming_fiber) {
3111 rb_raise(rb_eFiberError, "attempt to transfer to a resuming fiber");
3112 }
3113
3114 if (fiber->yielding) {
3115 rb_raise(rb_eFiberError, "attempt to transfer to a yielding fiber");
3116 }
3117
3118 return fiber_switch(fiber, argc, argv, kw_splat, NULL, false);
3119}
3120
3121VALUE
3122rb_fiber_transfer_kw(VALUE self, int argc, const VALUE *argv, int kw_splat)
3123{
3124 return fiber_transfer_kw(fiber_ptr(self), argc, argv, kw_splat);
3125}
3126
3127/*
3128 * call-seq:
3129 * Fiber.yield(args, ...) -> obj
3130 *
3131 * Yields control back to the context that resumed the fiber, passing
3132 * along any arguments that were passed to it. The fiber will resume
3133 * processing at this point when #resume is called next.
3134 * Any arguments passed to the next #resume will be the value that
3135 * this Fiber.yield expression evaluates to.
3136 */
3137static VALUE
3138rb_fiber_s_yield(int argc, VALUE *argv, VALUE klass)
3139{
3140 return rb_fiber_yield_kw(argc, argv, rb_keyword_given_p());
3141}
3142
3143static VALUE
3144fiber_raise(rb_fiber_t *fiber, VALUE exception)
3145{
3146 if (fiber == fiber_current()) {
3147 rb_exc_raise(exception);
3148 }
3149 else if (fiber->resuming_fiber) {
3150 return fiber_raise(fiber->resuming_fiber, exception);
3151 }
3152 else if (FIBER_SUSPENDED_P(fiber) && !fiber->yielding) {
3153 return fiber_transfer_kw(fiber, -1, &exception, RB_NO_KEYWORDS);
3154 }
3155 else {
3156 return fiber_resume_kw(fiber, -1, &exception, RB_NO_KEYWORDS);
3157 }
3158}
3159
3160VALUE
3161rb_fiber_raise(VALUE fiber, int argc, const VALUE *argv)
3162{
3163 VALUE exception = rb_make_exception(argc, argv);
3164
3165 return fiber_raise(fiber_ptr(fiber), exception);
3166}
3167
3168/*
3169 * call-seq:
3170 * fiber.raise -> obj
3171 * fiber.raise(string) -> obj
3172 * fiber.raise(exception [, string [, array]]) -> obj
3173 *
3174 * Raises an exception in the fiber at the point at which the last
3175 * +Fiber.yield+ was called. If the fiber has not been started or has
3176 * already run to completion, raises +FiberError+. If the fiber is
3177 * yielding, it is resumed. If it is transferring, it is transferred into.
3178 * But if it is resuming, raises +FiberError+.
3179 *
3180 * With no arguments, raises a +RuntimeError+. With a single +String+
3181 * argument, raises a +RuntimeError+ with the string as a message. Otherwise,
3182 * the first parameter should be the name of an +Exception+ class (or an
3183 * object that returns an +Exception+ object when sent an +exception+
3184 * message). The optional second parameter sets the message associated with
3185 * the exception, and the third parameter is an array of callback information.
3186 * Exceptions are caught by the +rescue+ clause of <code>begin...end</code>
3187 * blocks.
3188 *
3189 * Raises +FiberError+ if called on a Fiber belonging to another +Thread+.
3190 *
3191 * See Kernel#raise for more information.
3192 */
3193static VALUE
3194rb_fiber_m_raise(int argc, VALUE *argv, VALUE self)
3195{
3196 return rb_fiber_raise(self, argc, argv);
3197}
3198
3199/*
3200 * call-seq:
3201 * fiber.kill -> nil
3202 *
3203 * Terminates the fiber by raising an uncatchable exception.
3204 * It only terminates the given fiber and no other fiber, returning +nil+ to
3205 * another fiber if that fiber was calling #resume or #transfer.
3206 *
3207 * <tt>Fiber#kill</tt> only interrupts another fiber when it is in Fiber.yield.
3208 * If called on the current fiber then it raises that exception at the <tt>Fiber#kill</tt> call site.
3209 *
3210 * If the fiber has not been started, transition directly to the terminated state.
3211 *
3212 * If the fiber is already terminated, does nothing.
3213 *
3214 * Raises FiberError if called on a fiber belonging to another thread.
3215 */
3216static VALUE
3217rb_fiber_m_kill(VALUE self)
3218{
3219 rb_fiber_t *fiber = fiber_ptr(self);
3220
3221 if (fiber->killed) return Qfalse;
3222 fiber->killed = 1;
3223
3224 if (fiber->status == FIBER_CREATED) {
3225 fiber->status = FIBER_TERMINATED;
3226 }
3227 else if (fiber->status != FIBER_TERMINATED) {
3228 if (fiber_current() == fiber) {
3229 fiber_check_killed(fiber);
3230 }
3231 else {
3232 fiber_raise(fiber_ptr(self), Qnil);
3233 }
3234 }
3235
3236 return self;
3237}
3238
3239/*
3240 * call-seq:
3241 * Fiber.current -> fiber
3242 *
3243 * Returns the current fiber. If you are not running in the context of
3244 * a fiber this method will return the root fiber.
3245 */
3246static VALUE
3247rb_fiber_s_current(VALUE klass)
3248{
3249 return rb_fiber_current();
3250}
3251
3252static VALUE
3253fiber_to_s(VALUE fiber_value)
3254{
3255 const rb_fiber_t *fiber = fiber_ptr(fiber_value);
3256 const rb_proc_t *proc;
3257 char status_info[0x20];
3258
3259 if (fiber->resuming_fiber) {
3260 snprintf(status_info, 0x20, " (%s by resuming)", fiber_status_name(fiber->status));
3261 }
3262 else {
3263 snprintf(status_info, 0x20, " (%s)", fiber_status_name(fiber->status));
3264 }
3265
3266 if (!rb_obj_is_proc(fiber->first_proc)) {
3267 VALUE str = rb_any_to_s(fiber_value);
3268 strlcat(status_info, ">", sizeof(status_info));
3269 rb_str_set_len(str, RSTRING_LEN(str)-1);
3270 rb_str_cat_cstr(str, status_info);
3271 return str;
3272 }
3273 GetProcPtr(fiber->first_proc, proc);
3274 return rb_block_to_s(fiber_value, &proc->block, status_info);
3275}
3276
3277#ifdef HAVE_WORKING_FORK
3278void
3279rb_fiber_atfork(rb_thread_t *th)
3280{
3281 if (th->root_fiber) {
3282 if (&th->root_fiber->cont.saved_ec != th->ec) {
3283 th->root_fiber = th->ec->fiber_ptr;
3284 }
3285 th->root_fiber->prev = 0;
3286 }
3287}
3288#endif
3289
3290#ifdef RB_EXPERIMENTAL_FIBER_POOL
3291static void
3292fiber_pool_free(void *ptr)
3293{
3294 struct fiber_pool * fiber_pool = ptr;
3295 RUBY_FREE_ENTER("fiber_pool");
3296
3297 fiber_pool_allocation_free(fiber_pool->allocations);
3298 ruby_xfree(fiber_pool);
3299
3300 RUBY_FREE_LEAVE("fiber_pool");
3301}
3302
3303static size_t
3304fiber_pool_memsize(const void *ptr)
3305{
3306 const struct fiber_pool * fiber_pool = ptr;
3307 size_t size = sizeof(*fiber_pool);
3308
3309 size += fiber_pool->count * fiber_pool->size;
3310
3311 return size;
3312}
3313
3314static const rb_data_type_t FiberPoolDataType = {
3315 "fiber_pool",
3316 {NULL, fiber_pool_free, fiber_pool_memsize,},
3317 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3318};
3319
3320static VALUE
3321fiber_pool_alloc(VALUE klass)
3322{
3323 struct fiber_pool *fiber_pool;
3324
3325 return TypedData_Make_Struct(klass, struct fiber_pool, &FiberPoolDataType, fiber_pool);
3326}
3327
3328static VALUE
3329rb_fiber_pool_initialize(int argc, VALUE* argv, VALUE self)
3330{
3331 rb_thread_t *th = GET_THREAD();
3332 VALUE size = Qnil, count = Qnil, vm_stack_size = Qnil;
3333 struct fiber_pool * fiber_pool = NULL;
3334
3335 // Maybe these should be keyword arguments.
3336 rb_scan_args(argc, argv, "03", &size, &count, &vm_stack_size);
3337
3338 if (NIL_P(size)) {
3339 size = SIZET2NUM(th->vm->default_params.fiber_machine_stack_size);
3340 }
3341
3342 if (NIL_P(count)) {
3343 count = INT2NUM(128);
3344 }
3345
3346 if (NIL_P(vm_stack_size)) {
3347 vm_stack_size = SIZET2NUM(th->vm->default_params.fiber_vm_stack_size);
3348 }
3349
3350 TypedData_Get_Struct(self, struct fiber_pool, &FiberPoolDataType, fiber_pool);
3351
3352 fiber_pool_initialize(fiber_pool, NUM2SIZET(size), NUM2SIZET(count), NUM2SIZET(vm_stack_size));
3353
3354 return self;
3355}
3356#endif
3357
3358/*
3359 * Document-class: FiberError
3360 *
3361 * Raised when an invalid operation is attempted on a Fiber, in
3362 * particular when attempting to call/resume a dead fiber,
3363 * attempting to yield from the root fiber, or calling a fiber across
3364 * threads.
3365 *
3366 * fiber = Fiber.new{}
3367 * fiber.resume #=> nil
3368 * fiber.resume #=> FiberError: dead fiber called
3369 */
3370
3371void
3372Init_Cont(void)
3373{
3374 rb_thread_t *th = GET_THREAD();
3375 size_t vm_stack_size = th->vm->default_params.fiber_vm_stack_size;
3376 size_t machine_stack_size = th->vm->default_params.fiber_machine_stack_size;
3377 size_t stack_size = machine_stack_size + vm_stack_size;
3378
3379#ifdef _WIN32
3380 SYSTEM_INFO info;
3381 GetSystemInfo(&info);
3382 pagesize = info.dwPageSize;
3383#else /* not WIN32 */
3384 pagesize = sysconf(_SC_PAGESIZE);
3385#endif
3386 SET_MACHINE_STACK_END(&th->ec->machine.stack_end);
3387
3388 fiber_pool_initialize(&shared_fiber_pool, stack_size, FIBER_POOL_INITIAL_SIZE, vm_stack_size);
3389
3390 fiber_initialize_keywords[0] = rb_intern_const("blocking");
3391 fiber_initialize_keywords[1] = rb_intern_const("pool");
3392 fiber_initialize_keywords[2] = rb_intern_const("storage");
3393
3394 const char *fiber_shared_fiber_pool_free_stacks = getenv("RUBY_SHARED_FIBER_POOL_FREE_STACKS");
3395 if (fiber_shared_fiber_pool_free_stacks) {
3396 shared_fiber_pool.free_stacks = atoi(fiber_shared_fiber_pool_free_stacks);
3397
3398 if (shared_fiber_pool.free_stacks < 0) {
3399 rb_warn("Setting RUBY_SHARED_FIBER_POOL_FREE_STACKS to a negative value is not allowed.");
3400 shared_fiber_pool.free_stacks = 0;
3401 }
3402
3403 if (shared_fiber_pool.free_stacks > 1) {
3404 rb_warn("Setting RUBY_SHARED_FIBER_POOL_FREE_STACKS to a value greater than 1 is operating system specific, and may cause crashes.");
3405 }
3406 }
3407
3408 rb_cFiber = rb_define_class("Fiber", rb_cObject);
3409 rb_define_alloc_func(rb_cFiber, fiber_alloc);
3410 rb_eFiberError = rb_define_class("FiberError", rb_eStandardError);
3411 rb_define_singleton_method(rb_cFiber, "yield", rb_fiber_s_yield, -1);
3412 rb_define_singleton_method(rb_cFiber, "current", rb_fiber_s_current, 0);
3413 rb_define_singleton_method(rb_cFiber, "blocking", rb_fiber_blocking, 0);
3414 rb_define_singleton_method(rb_cFiber, "[]", rb_fiber_storage_aref, 1);
3415 rb_define_singleton_method(rb_cFiber, "[]=", rb_fiber_storage_aset, 2);
3416
3417 rb_define_method(rb_cFiber, "initialize", rb_fiber_initialize, -1);
3418 rb_define_method(rb_cFiber, "blocking?", rb_fiber_blocking_p, 0);
3419 rb_define_method(rb_cFiber, "storage", rb_fiber_storage_get, 0);
3420 rb_define_method(rb_cFiber, "storage=", rb_fiber_storage_set, 1);
3421 rb_define_method(rb_cFiber, "resume", rb_fiber_m_resume, -1);
3422 rb_define_method(rb_cFiber, "raise", rb_fiber_m_raise, -1);
3423 rb_define_method(rb_cFiber, "kill", rb_fiber_m_kill, 0);
3424 rb_define_method(rb_cFiber, "backtrace", rb_fiber_backtrace, -1);
3425 rb_define_method(rb_cFiber, "backtrace_locations", rb_fiber_backtrace_locations, -1);
3426 rb_define_method(rb_cFiber, "to_s", fiber_to_s, 0);
3427 rb_define_alias(rb_cFiber, "inspect", "to_s");
3428 rb_define_method(rb_cFiber, "transfer", rb_fiber_m_transfer, -1);
3429 rb_define_method(rb_cFiber, "alive?", rb_fiber_alive_p, 0);
3430
3431 rb_define_singleton_method(rb_cFiber, "blocking?", rb_fiber_s_blocking_p, 0);
3432 rb_define_singleton_method(rb_cFiber, "scheduler", rb_fiber_s_scheduler, 0);
3433 rb_define_singleton_method(rb_cFiber, "set_scheduler", rb_fiber_set_scheduler, 1);
3434 rb_define_singleton_method(rb_cFiber, "current_scheduler", rb_fiber_current_scheduler, 0);
3435
3436 rb_define_singleton_method(rb_cFiber, "schedule", rb_fiber_s_schedule, -1);
3437
3438#ifdef RB_EXPERIMENTAL_FIBER_POOL
3439 /*
3440 * Document-class: Fiber::Pool
3441 * :nodoc: experimental
3442 */
3443 rb_cFiberPool = rb_define_class_under(rb_cFiber, "Pool", rb_cObject);
3444 rb_define_alloc_func(rb_cFiberPool, fiber_pool_alloc);
3445 rb_define_method(rb_cFiberPool, "initialize", rb_fiber_pool_initialize, -1);
3446#endif
3447
3448 rb_provide("fiber.so");
3449}
3450
3451RUBY_SYMBOL_EXPORT_BEGIN
3452
3453void
3454ruby_Init_Continuation_body(void)
3455{
3456 rb_cContinuation = rb_define_class("Continuation", rb_cObject);
3457 rb_undef_alloc_func(rb_cContinuation);
3458 rb_undef_method(CLASS_OF(rb_cContinuation), "new");
3459 rb_define_method(rb_cContinuation, "call", rb_cont_call, -1);
3460 rb_define_method(rb_cContinuation, "[]", rb_cont_call, -1);
3461 rb_define_global_function("callcc", rb_callcc, 0);
3462}
3463
3464RUBY_SYMBOL_EXPORT_END
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
#define RUBY_EVENT_FIBER_SWITCH
Encountered a Fiber#yield.
Definition event.h:59
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
Definition fl_type.h:898
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:980
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1012
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2345
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2166
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.
Definition class.c:2648
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.
Definition class.c:2635
int rb_keyword_given_p(void)
Determines if the current method is given a keyword argument.
Definition eval.c:950
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:2424
#define REALLOC_N
Old name of RB_REALLOC_N.
Definition memory.h:403
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define Qundef
Old name of RUBY_Qundef.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:402
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:203
#define rb_ary_new4
Old name of rb_ary_new_from_values.
Definition array.h:659
#define SIZET2NUM
Old name of RB_SIZE2NUM.
Definition size_t.h:62
#define rb_exc_new2
Old name of rb_exc_new_cstr.
Definition error.h:37
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:399
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#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.
Definition value_type.h:80
#define NUM2SIZET
Old name of RB_NUM2SIZE.
Definition size_t.h:61
void ruby_stop(int ex)
Calls ruby_cleanup() and exits the process.
Definition eval.c:288
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:476
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
Definition error.c:1380
void rb_syserr_fail(int e, const char *mesg)
Raises appropriate exception that represents a C errno.
Definition error.c:3877
VALUE rb_eStandardError
StandardError exception.
Definition error.c:1427
VALUE rb_eFrozenError
FrozenError exception.
Definition error.c:1429
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1430
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1428
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:466
@ RB_WARN_CATEGORY_EXPERIMENTAL
Warning is for experimental features.
Definition error.h:51
VALUE rb_any_to_s(VALUE obj)
Generates a textual representation of the given object.
Definition object.c:669
VALUE rb_obj_dup(VALUE obj)
Duplicates the given object.
Definition object.c:576
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
Definition load.c:715
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:836
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:119
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
Definition string.c:3268
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1656
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1291
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().
Definition symbol.h:284
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
Definition string.c:12474
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1354
rb_block_call_func * rb_block_call_func_t
Shorthand type that represents an iterator-written-in-C function pointer.
Definition iterator.h:88
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
#define ALLOCA_N(type, n)
Definition memory.h:292
#define RB_ALLOC(type)
Shorthand of RB_ALLOC_N with n=1.
Definition memory.h:213
VALUE rb_proc_new(type *q, VALUE w)
Creates a rb_cProc instance.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:52
#define DATA_PTR(obj)
Convenient getter macro.
Definition rdata.h:67
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:515
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:449
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:197
#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...
Definition rtypeddata.h:497
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
#define RB_NO_KEYWORDS
Do not pass keywords.
Definition scan_args.h:69
Scheduler APIs.
VALUE rb_fiber_scheduler_current(void)
Identical to rb_fiber_scheduler_get(), except it also returns RUBY_Qnil in case of a blocking fiber.
Definition scheduler.c:228
VALUE rb_fiber_scheduler_set(VALUE scheduler)
Destructively assigns the passed scheduler to that of the current thread that is calling this functio...
Definition scheduler.c:189
VALUE rb_fiber_scheduler_get(void)
Queries the current scheduler of the current thread that is calling this function.
Definition scheduler.c:143
VALUE rb_fiber_scheduler_fiber(VALUE scheduler, int argc, VALUE *argv, int kw_splat)
Create and schedule a non-blocking fiber.
Definition scheduler.c:777
#define RTEST
This is an old name of RB_TEST.
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.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:433
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376