Ruby: cont.c Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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