Ruby: cont.c Source File

Ruby 3.3.7p123 (2025-01-15 revision be31f993d7fa0219d85f7b3c694d454da4ecc10b)

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