1 /**********************************************************************
6 created at: Thu May 23 09:03:43 2007
8 Copyright (C) 2007 Koichi Sasada
10 **********************************************************************/
12 #include "ruby/internal/config.h"
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>
23 extern int madvise(caddr_t
, size_t, int);
28 #include "eval_intern.h"
30 #include "internal/cont.h"
31 #include "internal/thread.h"
32 #include "internal/error.h"
33 #include "internal/gc.h"
34 #include "internal/proc.h"
35 #include "internal/sanitizers.h"
36 #include "internal/warnings.h"
37 #include "ruby/fiber/scheduler.h"
43 #include "ractor_core.h"
45 static const int DEBUG
= 0;
47 #define RB_PAGE_SIZE (pagesize)
48 #define RB_PAGE_MASK (~(RB_PAGE_SIZE - 1))
51 static const rb_data_type_t cont_data_type
, fiber_data_type
;
52 static VALUE rb_cContinuation
;
53 static VALUE rb_cFiber
;
54 static VALUE rb_eFiberError
;
55 #ifdef RB_EXPERIMENTAL_FIBER_POOL
56 static VALUE rb_cFiberPool
;
59 #define CAPTURE_JUST_VALID_VM_STACK 1
61 // Defined in `coroutine/$arch/Context.h`:
62 #ifdef COROUTINE_LIMITED_ADDRESS_SPACE
63 #define FIBER_POOL_ALLOCATION_FREE
64 #define FIBER_POOL_INITIAL_SIZE 8
65 #define FIBER_POOL_ALLOCATION_MAXIMUM_SIZE 32
67 #define FIBER_POOL_INITIAL_SIZE 32
68 #define FIBER_POOL_ALLOCATION_MAXIMUM_SIZE 1024
70 #ifdef RB_EXPERIMENTAL_FIBER_POOL
71 #define FIBER_POOL_ALLOCATION_FREE
75 CONTINUATION_CONTEXT
= 0,
79 struct cont_saved_vm_stack
{
81 #ifdef CAPTURE_JUST_VALID_VM_STACK
82 size_t slen
; /* length of stack (head of ec->vm_stack) */
83 size_t clen
; /* length of control frames (tail of ec->vm_stack) */
89 // Represents a single stack.
90 struct fiber_pool_stack
{
91 // A pointer to the memory allocation (lowest address) for the stack.
94 // The current stack pointer, taking into account the direction of the stack.
97 // The size of the stack excluding any guard pages.
100 // The available stack capacity w.r.t. the current stack offset.
103 // The pool this stack should be allocated from.
104 struct fiber_pool
* pool
;
106 // If the stack is allocated, the allocation it came from.
107 struct fiber_pool_allocation
* allocation
;
110 // A linked list of vacant (unused) stacks.
111 // This structure is stored in the first page of a stack if it is not in use.
112 // @sa fiber_pool_vacancy_pointer
113 struct fiber_pool_vacancy
{
114 // Details about the vacant stack:
115 struct fiber_pool_stack stack
;
117 // The vacancy linked list.
118 #ifdef FIBER_POOL_ALLOCATION_FREE
119 struct fiber_pool_vacancy
* previous
;
121 struct fiber_pool_vacancy
* next
;
124 // Manages singly linked list of mapped regions of memory which contains 1 more more stack:
126 // base = +-------------------------------+-----------------------+ +
127 // |VM Stack |VM Stack | | |
130 // +-------------------------------+ | |
131 // |Machine Stack |Machine Stack | | |
134 // | | | . . . . | | size
140 // +-------------------------------+ | |
141 // |Guard Page |Guard Page | | |
142 // +-------------------------------+-----------------------+ v
144 // +------------------------------------------------------->
148 struct fiber_pool_allocation
{
149 // A pointer to the memory mapped region.
152 // The size of the individual stacks.
155 // The stride of individual stacks (including any guard pages or other accounting details).
158 // The number of stacks that were allocated.
161 #ifdef FIBER_POOL_ALLOCATION_FREE
162 // The number of stacks used in this allocation.
166 struct fiber_pool
* pool
;
168 // The allocation linked list.
169 #ifdef FIBER_POOL_ALLOCATION_FREE
170 struct fiber_pool_allocation
* previous
;
172 struct fiber_pool_allocation
* next
;
175 // A fiber pool manages vacant stacks to reduce the overhead of creating fibers.
177 // A singly-linked list of allocations which contain 1 or more stacks each.
178 struct fiber_pool_allocation
* allocations
;
180 // Free list that provides O(1) stack "allocation".
181 struct fiber_pool_vacancy
* vacancies
;
183 // The size of the stack allocations (excluding any guard page).
186 // The total number of stacks that have been allocated in this pool.
189 // The initial number of stacks to allocate.
190 size_t initial_count
;
192 // Whether to madvise(free) the stack or not.
193 // If this value is set to 1, the stack will be madvise(free)ed
194 // (or equivalent), where possible, when it is returned to the pool.
197 // The number of stacks that have been used in this pool.
200 // The amount to allocate for the vm_stack.
201 size_t vm_stack_size
;
204 // Continuation contexts used by JITs
206 rb_execution_context_t
*ec
; // continuation ec
207 struct rb_jit_cont
*prev
, *next
; // used to form lists
210 // Doubly linked list for enumerating all on-stack ISEQs.
211 static struct rb_jit_cont
*first_jit_cont
;
213 typedef struct rb_context_struct
{
214 enum context_type type
;
220 struct cont_saved_vm_stack saved_vm_stack
;
227 rb_execution_context_t saved_ec
;
229 struct rb_jit_cont
*jit_cont
; // Continuation contexts for JITs
234 * [Fiber.new] ------> FIBER_CREATED ----> [Fiber#kill] --> |
237 * +--> FIBER_RESUMED ----> [return] ------> |
238 * [Fiber#resume] | | [Fiber.yield/transfer] |
239 * [Fiber#transfer] | v |
240 * +--- FIBER_SUSPENDED --> [Fiber#kill] --> |
243 * FIBER_TERMINATED <-------------------+
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))
258 struct rb_fiber_struct
{
261 struct rb_fiber_struct
*prev
;
262 struct rb_fiber_struct
*resuming_fiber
;
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;
269 unsigned int killed
: 1;
271 struct coroutine_context context
;
272 struct fiber_pool_stack stack
;
275 static struct fiber_pool shared_fiber_pool
= {NULL
, NULL
, 0, 0, 0, 0};
278 rb_free_shared_fiber_pool(void)
280 xfree(shared_fiber_pool
.allocations
);
283 static ID fiber_initialize_keywords
[3] = {0};
286 * FreeBSD require a first (i.e. addr) argument of mmap(2) is not NULL
287 * if MAP_STACK is passed.
288 * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=158755
290 #if defined(MAP_STACK) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
291 #define FIBER_STACK_FLAGS (MAP_PRIVATE | MAP_ANON | MAP_STACK)
293 #define FIBER_STACK_FLAGS (MAP_PRIVATE | MAP_ANON)
296 #define ERRNOMSG strerror(errno)
298 // Locates the stack vacancy details for the given stack.
299 inline static struct fiber_pool_vacancy
*
300 fiber_pool_vacancy_pointer(void * base
, size_t size
)
302 STACK_GROW_DIR_DETECTION
;
304 return (struct fiber_pool_vacancy
*)(
305 (char*)base
+ STACK_DIR_UPPER(0, size
- RB_PAGE_SIZE
)
309 #if defined(COROUTINE_SANITIZE_ADDRESS)
310 // Compute the base pointer for a vacant stack, for the area which can be poisoned.
312 fiber_pool_stack_poison_base(struct fiber_pool_stack
* stack
)
314 STACK_GROW_DIR_DETECTION
;
316 return (char*)stack
->base
+ STACK_DIR_UPPER(RB_PAGE_SIZE
, 0);
319 // Compute the size of the vacant stack, for the area that can be poisoned.
321 fiber_pool_stack_poison_size(struct fiber_pool_stack
* stack
)
323 return stack
->size
- RB_PAGE_SIZE
;
327 // Reset the current stack pointer and available size of the given stack.
329 fiber_pool_stack_reset(struct fiber_pool_stack
* stack
)
331 STACK_GROW_DIR_DETECTION
;
333 stack
->current
= (char*)stack
->base
+ STACK_DIR_UPPER(0, stack
->size
);
334 stack
->available
= stack
->size
;
337 // A pointer to the base of the current unused portion of the stack.
339 fiber_pool_stack_base(struct fiber_pool_stack
* stack
)
341 STACK_GROW_DIR_DETECTION
;
343 VM_ASSERT(stack
->current
);
345 return STACK_DIR_UPPER(stack
->current
, (char*)stack
->current
- stack
->available
);
348 // Allocate some memory from the stack. Used to allocate vm_stack inline with machine stack.
349 // @sa fiber_initialize_coroutine
351 fiber_pool_stack_alloca(struct fiber_pool_stack
* stack
, size_t offset
)
353 STACK_GROW_DIR_DETECTION
;
355 if (DEBUG
) fprintf(stderr
, "fiber_pool_stack_alloca(%p): %"PRIuSIZE
"/%"PRIuSIZE
"\n", (void*)stack
, offset
, stack
->available
);
356 VM_ASSERT(stack
->available
>= offset
);
358 // The pointer to the memory being allocated:
359 void * pointer
= STACK_DIR_UPPER(stack
->current
, (char*)stack
->current
- offset
);
361 // Move the stack pointer:
362 stack
->current
= STACK_DIR_UPPER((char*)stack
->current
+ offset
, (char*)stack
->current
- offset
);
363 stack
->available
-= offset
;
368 // Reset the current stack pointer and available size of the given stack.
370 fiber_pool_vacancy_reset(struct fiber_pool_vacancy
* vacancy
)
372 fiber_pool_stack_reset(&vacancy
->stack
);
374 // Consume one page of the stack because it's used for the vacancy list:
375 fiber_pool_stack_alloca(&vacancy
->stack
, RB_PAGE_SIZE
);
378 inline static struct fiber_pool_vacancy
*
379 fiber_pool_vacancy_push(struct fiber_pool_vacancy
* vacancy
, struct fiber_pool_vacancy
* head
)
381 vacancy
->next
= head
;
383 #ifdef FIBER_POOL_ALLOCATION_FREE
385 head
->previous
= vacancy
;
386 vacancy
->previous
= NULL
;
393 #ifdef FIBER_POOL_ALLOCATION_FREE
395 fiber_pool_vacancy_remove(struct fiber_pool_vacancy
* vacancy
)
398 vacancy
->next
->previous
= vacancy
->previous
;
401 if (vacancy
->previous
) {
402 vacancy
->previous
->next
= vacancy
->next
;
405 // It's the head of the list:
406 vacancy
->stack
.pool
->vacancies
= vacancy
->next
;
410 inline static struct fiber_pool_vacancy
*
411 fiber_pool_vacancy_pop(struct fiber_pool
* pool
)
413 struct fiber_pool_vacancy
* vacancy
= pool
->vacancies
;
416 fiber_pool_vacancy_remove(vacancy
);
422 inline static struct fiber_pool_vacancy
*
423 fiber_pool_vacancy_pop(struct fiber_pool
* pool
)
425 struct fiber_pool_vacancy
* vacancy
= pool
->vacancies
;
428 pool
->vacancies
= vacancy
->next
;
435 // Initialize the vacant stack. The [base, size] allocation should not include the guard page.
436 // @param base The pointer to the lowest address of the allocated memory.
437 // @param size The size of the allocated memory.
438 inline static struct fiber_pool_vacancy
*
439 fiber_pool_vacancy_initialize(struct fiber_pool
* fiber_pool
, struct fiber_pool_vacancy
* vacancies
, void * base
, size_t size
)
441 struct fiber_pool_vacancy
* vacancy
= fiber_pool_vacancy_pointer(base
, size
);
443 vacancy
->stack
.base
= base
;
444 vacancy
->stack
.size
= size
;
446 fiber_pool_vacancy_reset(vacancy
);
448 vacancy
->stack
.pool
= fiber_pool
;
450 return fiber_pool_vacancy_push(vacancy
, vacancies
);
453 // Allocate a maximum of count stacks, size given by stride.
454 // @param count the number of stacks to allocate / were allocated.
455 // @param stride the size of the individual stacks.
456 // @return [void *] the allocated memory or NULL if allocation failed.
458 fiber_pool_allocate_memory(size_t * count
, size_t stride
)
460 // We use a divide-by-2 strategy to try and allocate memory. We are trying
461 // to allocate `count` stacks. In normal situation, this won't fail. But
462 // if we ran out of address space, or we are allocating more memory than
463 // the system would allow (e.g. overcommit * physical memory + swap), we
464 // divide count by two and try again. This condition should only be
465 // encountered in edge cases, but we handle it here gracefully.
468 void * base
= VirtualAlloc(0, (*count
)*stride
, MEM_COMMIT
, PAGE_READWRITE
);
471 *count
= (*count
) >> 1;
478 void * base
= mmap(NULL
, (*count
)*stride
, PROT_READ
| PROT_WRITE
, FIBER_STACK_FLAGS
, -1, 0);
480 if (base
== MAP_FAILED
) {
481 // If the allocation fails, count = count / 2, and try again.
482 *count
= (*count
) >> 1;
485 #if defined(MADV_FREE_REUSE)
486 // On Mac MADV_FREE_REUSE is necessary for the task_info api
487 // to keep the accounting accurate as possible when a page is marked as reusable
488 // it can possibly not occurring at first call thus re-iterating if necessary.
489 while (madvise(base
, (*count
)*stride
, MADV_FREE_REUSE
) == -1 && errno
== EAGAIN
);
499 // Given an existing fiber pool, expand it by the specified number of stacks.
500 // @param count the maximum number of stacks to allocate.
501 // @return the allocated fiber pool.
502 // @sa fiber_pool_allocation_free
503 static struct fiber_pool_allocation
*
504 fiber_pool_expand(struct fiber_pool
* fiber_pool
, size_t count
)
506 STACK_GROW_DIR_DETECTION
;
508 size_t size
= fiber_pool
->size
;
509 size_t stride
= size
+ RB_PAGE_SIZE
;
511 // Allocate the memory required for the stacks:
512 void * base
= fiber_pool_allocate_memory(&count
, stride
);
515 rb_raise(rb_eFiberError
, "can't alloc machine stack to fiber (%"PRIuSIZE
" x %"PRIuSIZE
" bytes): %s", count
, size
, ERRNOMSG
);
518 struct fiber_pool_vacancy
* vacancies
= fiber_pool
->vacancies
;
519 struct fiber_pool_allocation
* allocation
= RB_ALLOC(struct fiber_pool_allocation
);
521 // Initialize fiber pool allocation:
522 allocation
->base
= base
;
523 allocation
->size
= size
;
524 allocation
->stride
= stride
;
525 allocation
->count
= count
;
526 #ifdef FIBER_POOL_ALLOCATION_FREE
527 allocation
->used
= 0;
529 allocation
->pool
= fiber_pool
;
532 fprintf(stderr
, "fiber_pool_expand(%"PRIuSIZE
"): %p, %"PRIuSIZE
"/%"PRIuSIZE
" x [%"PRIuSIZE
":%"PRIuSIZE
"]\n",
533 count
, (void*)fiber_pool
, fiber_pool
->used
, fiber_pool
->count
, size
, fiber_pool
->vm_stack_size
);
536 // Iterate over all stacks, initializing the vacancy list:
537 for (size_t i
= 0; i
< count
; i
+= 1) {
538 void * base
= (char*)allocation
->base
+ (stride
* i
);
539 void * page
= (char*)base
+ STACK_DIR_UPPER(size
, 0);
544 if (!VirtualProtect(page
, RB_PAGE_SIZE
, PAGE_READWRITE
| PAGE_GUARD
, &old_protect
)) {
545 VirtualFree(allocation
->base
, 0, MEM_RELEASE
);
546 rb_raise(rb_eFiberError
, "can't set a guard page: %s", ERRNOMSG
);
549 if (mprotect(page
, RB_PAGE_SIZE
, PROT_NONE
) < 0) {
550 munmap(allocation
->base
, count
*stride
);
551 rb_raise(rb_eFiberError
, "can't set a guard page: %s", ERRNOMSG
);
555 vacancies
= fiber_pool_vacancy_initialize(
556 fiber_pool
, vacancies
,
557 (char*)base
+ STACK_DIR_UPPER(0, RB_PAGE_SIZE
),
561 #ifdef FIBER_POOL_ALLOCATION_FREE
562 vacancies
->stack
.allocation
= allocation
;
566 // Insert the allocation into the head of the pool:
567 allocation
->next
= fiber_pool
->allocations
;
569 #ifdef FIBER_POOL_ALLOCATION_FREE
570 if (allocation
->next
) {
571 allocation
->next
->previous
= allocation
;
574 allocation
->previous
= NULL
;
577 fiber_pool
->allocations
= allocation
;
578 fiber_pool
->vacancies
= vacancies
;
579 fiber_pool
->count
+= count
;
584 // Initialize the specified fiber pool with the given number of stacks.
585 // @param vm_stack_size The size of the vm stack to allocate.
587 fiber_pool_initialize(struct fiber_pool
* fiber_pool
, size_t size
, size_t count
, size_t vm_stack_size
)
589 VM_ASSERT(vm_stack_size
< size
);
591 fiber_pool
->allocations
= NULL
;
592 fiber_pool
->vacancies
= NULL
;
593 fiber_pool
->size
= ((size
/ RB_PAGE_SIZE
) + 1) * RB_PAGE_SIZE
;
594 fiber_pool
->count
= 0;
595 fiber_pool
->initial_count
= count
;
596 fiber_pool
->free_stacks
= 1;
597 fiber_pool
->used
= 0;
599 fiber_pool
->vm_stack_size
= vm_stack_size
;
601 fiber_pool_expand(fiber_pool
, count
);
604 #ifdef FIBER_POOL_ALLOCATION_FREE
605 // Free the list of fiber pool allocations.
607 fiber_pool_allocation_free(struct fiber_pool_allocation
* allocation
)
609 STACK_GROW_DIR_DETECTION
;
611 VM_ASSERT(allocation
->used
== 0);
613 if (DEBUG
) fprintf(stderr
, "fiber_pool_allocation_free: %p base=%p count=%"PRIuSIZE
"\n", (void*)allocation
, allocation
->base
, allocation
->count
);
616 for (i
= 0; i
< allocation
->count
; i
+= 1) {
617 void * base
= (char*)allocation
->base
+ (allocation
->stride
* i
) + STACK_DIR_UPPER(0, RB_PAGE_SIZE
);
619 struct fiber_pool_vacancy
* vacancy
= fiber_pool_vacancy_pointer(base
, allocation
->size
);
621 // Pop the vacant stack off the free list:
622 fiber_pool_vacancy_remove(vacancy
);
626 VirtualFree(allocation
->base
, 0, MEM_RELEASE
);
628 munmap(allocation
->base
, allocation
->stride
* allocation
->count
);
631 if (allocation
->previous
) {
632 allocation
->previous
->next
= allocation
->next
;
635 // We are the head of the list, so update the pool:
636 allocation
->pool
->allocations
= allocation
->next
;
639 if (allocation
->next
) {
640 allocation
->next
->previous
= allocation
->previous
;
643 allocation
->pool
->count
-= allocation
->count
;
645 ruby_xfree(allocation
);
649 // Acquire a stack from the given fiber pool. If none are available, allocate more.
650 static struct fiber_pool_stack
651 fiber_pool_stack_acquire(struct fiber_pool
* fiber_pool
)
653 struct fiber_pool_vacancy
* vacancy
= fiber_pool_vacancy_pop(fiber_pool
);
655 if (DEBUG
) fprintf(stderr
, "fiber_pool_stack_acquire: %p used=%"PRIuSIZE
"\n", (void*)fiber_pool
->vacancies
, fiber_pool
->used
);
658 const size_t maximum
= FIBER_POOL_ALLOCATION_MAXIMUM_SIZE
;
659 const size_t minimum
= fiber_pool
->initial_count
;
661 size_t count
= fiber_pool
->count
;
662 if (count
> maximum
) count
= maximum
;
663 if (count
< minimum
) count
= minimum
;
665 fiber_pool_expand(fiber_pool
, count
);
667 // The free list should now contain some stacks:
668 VM_ASSERT(fiber_pool
->vacancies
);
670 vacancy
= fiber_pool_vacancy_pop(fiber_pool
);
674 VM_ASSERT(vacancy
->stack
.base
);
676 #if defined(COROUTINE_SANITIZE_ADDRESS)
677 __asan_unpoison_memory_region(fiber_pool_stack_poison_base(&vacancy
->stack
), fiber_pool_stack_poison_size(&vacancy
->stack
));
680 // Take the top item from the free list:
681 fiber_pool
->used
+= 1;
683 #ifdef FIBER_POOL_ALLOCATION_FREE
684 vacancy
->stack
.allocation
->used
+= 1;
687 fiber_pool_stack_reset(&vacancy
->stack
);
689 return vacancy
->stack
;
692 // We advise the operating system that the stack memory pages are no longer being used.
693 // This introduce some performance overhead but allows system to relaim memory when there is pressure.
695 fiber_pool_stack_free(struct fiber_pool_stack
* stack
)
697 void * base
= fiber_pool_stack_base(stack
);
698 size_t size
= stack
->available
;
700 // If this is not true, the vacancy information will almost certainly be destroyed:
701 VM_ASSERT(size
<= (stack
->size
- RB_PAGE_SIZE
));
703 int advice
= stack
->pool
->free_stacks
>> 1;
705 if (DEBUG
) fprintf(stderr
, "fiber_pool_stack_free: %p+%"PRIuSIZE
" [base=%p, size=%"PRIuSIZE
"] advice=%d\n", base
, size
, stack
->base
, stack
->size
, advice
);
707 // The pages being used by the stack can be returned back to the system.
708 // That doesn't change the page mapping, but it does allow the system to
709 // reclaim the physical memory.
710 // Since we no longer care about the data itself, we don't need to page
711 // out to disk, since that is costly. Not all systems support that, so
712 // we try our best to select the most efficient implementation.
713 // In addition, it's actually slightly desirable to not do anything here,
714 // but that results in higher memory usage.
717 // WebAssembly doesn't support madvise, so we just don't do anything.
718 #elif VM_CHECK_MODE > 0 && defined(MADV_DONTNEED)
719 if (!advice
) advice
= MADV_DONTNEED
;
720 // This immediately discards the pages and the memory is reset to zero.
721 madvise(base
, size
, advice
);
722 #elif defined(MADV_FREE_REUSABLE)
723 if (!advice
) advice
= MADV_FREE_REUSABLE
;
724 // Darwin / macOS / iOS.
725 // Acknowledge the kernel down to the task info api we make this
726 // page reusable for future use.
727 // As for MADV_FREE_REUSABLE below we ensure in the rare occasions the task was not
728 // completed at the time of the call to re-iterate.
729 while (madvise(base
, size
, advice
) == -1 && errno
== EAGAIN
);
730 #elif defined(MADV_FREE)
731 if (!advice
) advice
= MADV_FREE
;
733 madvise(base
, size
, advice
);
734 #elif defined(MADV_DONTNEED)
735 if (!advice
) advice
= MADV_DONTNEED
;
737 madvise(base
, size
, advice
);
738 #elif defined(POSIX_MADV_DONTNEED)
739 if (!advice
) advice
= POSIX_MADV_DONTNEED
;
741 posix_madvise(base
, size
, advice
);
742 #elif defined(_WIN32)
743 VirtualAlloc(base
, size
, MEM_RESET
, PAGE_READWRITE
);
744 // Not available in all versions of Windows.
745 //DiscardVirtualMemory(base, size);
748 #if defined(COROUTINE_SANITIZE_ADDRESS)
749 __asan_poison_memory_region(fiber_pool_stack_poison_base(stack
), fiber_pool_stack_poison_size(stack
));
753 // Release and return a stack to the vacancy list.
755 fiber_pool_stack_release(struct fiber_pool_stack
* stack
)
757 struct fiber_pool
* pool
= stack
->pool
;
758 struct fiber_pool_vacancy
* vacancy
= fiber_pool_vacancy_pointer(stack
->base
, stack
->size
);
760 if (DEBUG
) fprintf(stderr
, "fiber_pool_stack_release: %p used=%"PRIuSIZE
"\n", stack
->base
, stack
->pool
->used
);
762 // Copy the stack details into the vacancy area:
763 vacancy
->stack
= *stack
;
764 // After this point, be careful about updating/using state in stack, since it's copied to the vacancy area.
766 // Reset the stack pointers and reserve space for the vacancy data:
767 fiber_pool_vacancy_reset(vacancy
);
769 // Push the vacancy into the vancancies list:
770 pool
->vacancies
= fiber_pool_vacancy_push(vacancy
, pool
->vacancies
);
773 #ifdef FIBER_POOL_ALLOCATION_FREE
774 struct fiber_pool_allocation
* allocation
= stack
->allocation
;
776 allocation
->used
-= 1;
778 // Release address space and/or dirty memory:
779 if (allocation
->used
== 0) {
780 fiber_pool_allocation_free(allocation
);
782 else if (stack
->pool
->free_stacks
) {
783 fiber_pool_stack_free(&vacancy
->stack
);
786 // This is entirely optional, but clears the dirty flag from the stack
787 // memory, so it won't get swapped to disk when there is memory pressure:
788 if (stack
->pool
->free_stacks
) {
789 fiber_pool_stack_free(&vacancy
->stack
);
795 ec_switch(rb_thread_t
*th
, rb_fiber_t
*fiber
)
797 rb_execution_context_t
*ec
= &fiber
->cont
.saved_ec
;
798 #ifdef RUBY_ASAN_ENABLED
799 ec
->machine
.asan_fake_stack_handle
= asan_get_thread_fake_stack_handle();
801 rb_ractor_set_current_ec(th
->ractor
, th
->ec
= ec
);
802 // ruby_current_execution_context_ptr = th->ec = ec;
805 * timer-thread may set trap interrupt on previous th->ec at any time;
806 * ensure we do not delay (or lose) the trap interrupt handling.
808 if (th
->vm
->ractor
.main_thread
== th
&&
809 rb_signal_buff_size() > 0) {
810 RUBY_VM_SET_TRAP_INTERRUPT(ec
);
813 VM_ASSERT(ec
->fiber_ptr
->cont
.self
== 0 || ec
->vm_stack
!= NULL
);
817 fiber_restore_thread(rb_thread_t
*th
, rb_fiber_t
*fiber
)
819 ec_switch(th
, fiber
);
820 VM_ASSERT(th
->ec
->fiber_ptr
== fiber
);
824 fiber_entry(struct coroutine_context
* from
, struct coroutine_context
* to
)
826 rb_fiber_t
*fiber
= to
->argument
;
828 #if defined(COROUTINE_SANITIZE_ADDRESS)
829 // Address sanitizer will copy the previous stack base and stack size into
830 // the "from" fiber. `coroutine_initialize_main` doesn't generally know the
831 // stack bounds (base + size). Therefore, the main fiber `stack_base` and
832 // `stack_size` will be NULL/0. It's specifically important in that case to
833 // get the (base+size) of the previous fiber and save it, so that later when
834 // we return to the main coroutine, we don't supply (NULL, 0) to
835 // __sanitizer_start_switch_fiber which royally messes up the internal state
836 // of ASAN and causes (sometimes) the following message:
837 // "WARNING: ASan is ignoring requested __asan_handle_no_return"
838 __sanitizer_finish_switch_fiber(to
->fake_stack
, (const void**)&from
->stack_base
, &from
->stack_size
);
841 rb_thread_t
*thread
= fiber
->cont
.saved_ec
.thread_ptr
;
843 #ifdef COROUTINE_PTHREAD_CONTEXT
844 ruby_thread_set_native(thread
);
847 fiber_restore_thread(thread
, fiber
);
849 rb_fiber_start(fiber
);
851 #ifndef COROUTINE_PTHREAD_CONTEXT
852 VM_UNREACHABLE(fiber_entry
);
856 // Initialize a fiber's coroutine's machine stack and vm stack.
858 fiber_initialize_coroutine(rb_fiber_t
*fiber
, size_t * vm_stack_size
)
860 struct fiber_pool
* fiber_pool
= fiber
->stack
.pool
;
861 rb_execution_context_t
*sec
= &fiber
->cont
.saved_ec
;
862 void * vm_stack
= NULL
;
864 VM_ASSERT(fiber_pool
!= NULL
);
866 fiber
->stack
= fiber_pool_stack_acquire(fiber_pool
);
867 vm_stack
= fiber_pool_stack_alloca(&fiber
->stack
, fiber_pool
->vm_stack_size
);
868 *vm_stack_size
= fiber_pool
->vm_stack_size
;
870 coroutine_initialize(&fiber
->context
, fiber_entry
, fiber_pool_stack_base(&fiber
->stack
), fiber
->stack
.available
);
872 // The stack for this execution context is the one we allocated:
873 sec
->machine
.stack_start
= fiber
->stack
.current
;
874 sec
->machine
.stack_maxsize
= fiber
->stack
.available
;
876 fiber
->context
.argument
= (void*)fiber
;
881 // Release the stack from the fiber, it's execution context, and return it to
884 fiber_stack_release(rb_fiber_t
* fiber
)
886 rb_execution_context_t
*ec
= &fiber
->cont
.saved_ec
;
888 if (DEBUG
) fprintf(stderr
, "fiber_stack_release: %p, stack.base=%p\n", (void*)fiber
, fiber
->stack
.base
);
890 // Return the stack back to the fiber pool if it wasn't already:
891 if (fiber
->stack
.base
) {
892 fiber_pool_stack_release(&fiber
->stack
);
893 fiber
->stack
.base
= NULL
;
896 // The stack is no longer associated with this execution context:
897 rb_ec_clear_vm_stack(ec
);
901 fiber_status_name(enum fiber_status s
)
904 case FIBER_CREATED
: return "created";
905 case FIBER_RESUMED
: return "resumed";
906 case FIBER_SUSPENDED
: return "suspended";
907 case FIBER_TERMINATED
: return "terminated";
909 VM_UNREACHABLE(fiber_status_name
);
914 fiber_verify(const rb_fiber_t
*fiber
)
916 #if VM_CHECK_MODE > 0
917 VM_ASSERT(fiber
->cont
.saved_ec
.fiber_ptr
== fiber
);
919 switch (fiber
->status
) {
921 VM_ASSERT(fiber
->cont
.saved_ec
.vm_stack
!= NULL
);
923 case FIBER_SUSPENDED
:
924 VM_ASSERT(fiber
->cont
.saved_ec
.vm_stack
!= NULL
);
927 case FIBER_TERMINATED
:
931 VM_UNREACHABLE(fiber_verify
);
937 fiber_status_set(rb_fiber_t
*fiber
, enum fiber_status s
)
939 // if (DEBUG) fprintf(stderr, "fiber: %p, status: %s -> %s\n", (void *)fiber, fiber_status_name(fiber->status), fiber_status_name(s));
940 VM_ASSERT(!FIBER_TERMINATED_P(fiber
));
941 VM_ASSERT(fiber
->status
!= s
);
946 static rb_context_t
*
951 TypedData_Get_Struct(obj
, rb_context_t
, &cont_data_type
, cont
);
961 TypedData_Get_Struct(obj
, rb_fiber_t
, &fiber_data_type
, fiber
);
962 if (!fiber
) rb_raise(rb_eFiberError
, "uninitialized fiber");
967 NOINLINE(static VALUE
cont_capture(volatile int *volatile stat
));
969 #define THREAD_MUST_BE_RUNNING(th) do { \
970 if (!(th)->ec->tag) rb_raise(rb_eThreadError, "not running thread"); \
974 rb_fiber_threadptr(const rb_fiber_t
*fiber
)
976 return fiber
->cont
.saved_ec
.thread_ptr
;
980 cont_thread_value(const rb_context_t
*cont
)
982 return cont
->saved_ec
.thread_ptr
->self
;
986 cont_compact(void *ptr
)
988 rb_context_t
*cont
= ptr
;
991 cont
->self
= rb_gc_location(cont
->self
);
993 cont
->value
= rb_gc_location(cont
->value
);
994 rb_execution_context_update(&cont
->saved_ec
);
1000 rb_context_t
*cont
= ptr
;
1002 RUBY_MARK_ENTER("cont");
1004 rb_gc_mark_movable(cont
->self
);
1006 rb_gc_mark_movable(cont
->value
);
1008 rb_execution_context_mark(&cont
->saved_ec
);
1009 rb_gc_mark(cont_thread_value(cont
));
1011 if (cont
->saved_vm_stack
.ptr
) {
1012 #ifdef CAPTURE_JUST_VALID_VM_STACK
1013 rb_gc_mark_locations(cont
->saved_vm_stack
.ptr
,
1014 cont
->saved_vm_stack
.ptr
+ cont
->saved_vm_stack
.slen
+ cont
->saved_vm_stack
.clen
);
1016 rb_gc_mark_locations(cont
->saved_vm_stack
.ptr
,
1017 cont
->saved_vm_stack
.ptr
, cont
->saved_ec
.stack_size
);
1021 if (cont
->machine
.stack
) {
1022 if (cont
->type
== CONTINUATION_CONTEXT
) {
1024 rb_gc_mark_locations(cont
->machine
.stack
,
1025 cont
->machine
.stack
+ cont
->machine
.stack_size
);
1028 /* fiber machine context is marked as part of rb_execution_context_mark, no need to
1029 * do anything here. */
1033 RUBY_MARK_LEAVE("cont");
1038 fiber_is_root_p(const rb_fiber_t
*fiber
)
1040 return fiber
== fiber
->cont
.saved_ec
.thread_ptr
->root_fiber
;
1044 static void jit_cont_free(struct rb_jit_cont
*cont
);
1047 cont_free(void *ptr
)
1049 rb_context_t
*cont
= ptr
;
1051 RUBY_FREE_ENTER("cont");
1053 if (cont
->type
== CONTINUATION_CONTEXT
) {
1054 ruby_xfree(cont
->saved_ec
.vm_stack
);
1055 RUBY_FREE_UNLESS_NULL(cont
->machine
.stack
);
1058 rb_fiber_t
*fiber
= (rb_fiber_t
*)cont
;
1059 coroutine_destroy(&fiber
->context
);
1060 fiber_stack_release(fiber
);
1063 RUBY_FREE_UNLESS_NULL(cont
->saved_vm_stack
.ptr
);
1065 VM_ASSERT(cont
->jit_cont
!= NULL
);
1066 jit_cont_free(cont
->jit_cont
);
1067 /* free rb_cont_t or rb_fiber_t */
1069 RUBY_FREE_LEAVE("cont");
1073 cont_memsize(const void *ptr
)
1075 const rb_context_t
*cont
= ptr
;
1078 size
= sizeof(*cont
);
1079 if (cont
->saved_vm_stack
.ptr
) {
1080 #ifdef CAPTURE_JUST_VALID_VM_STACK
1081 size_t n
= (cont
->saved_vm_stack
.slen
+ cont
->saved_vm_stack
.clen
);
1083 size_t n
= cont
->saved_ec
.vm_stack_size
;
1085 size
+= n
* sizeof(*cont
->saved_vm_stack
.ptr
);
1088 if (cont
->machine
.stack
) {
1089 size
+= cont
->machine
.stack_size
* sizeof(*cont
->machine
.stack
);
1096 rb_fiber_update_self(rb_fiber_t
*fiber
)
1098 if (fiber
->cont
.self
) {
1099 fiber
->cont
.self
= rb_gc_location(fiber
->cont
.self
);
1102 rb_execution_context_update(&fiber
->cont
.saved_ec
);
1107 rb_fiber_mark_self(const rb_fiber_t
*fiber
)
1109 if (fiber
->cont
.self
) {
1110 rb_gc_mark_movable(fiber
->cont
.self
);
1113 rb_execution_context_mark(&fiber
->cont
.saved_ec
);
1118 fiber_compact(void *ptr
)
1120 rb_fiber_t
*fiber
= ptr
;
1121 fiber
->first_proc
= rb_gc_location(fiber
->first_proc
);
1123 if (fiber
->prev
) rb_fiber_update_self(fiber
->prev
);
1125 cont_compact(&fiber
->cont
);
1126 fiber_verify(fiber
);
1130 fiber_mark(void *ptr
)
1132 rb_fiber_t
*fiber
= ptr
;
1133 RUBY_MARK_ENTER("cont");
1134 fiber_verify(fiber
);
1135 rb_gc_mark_movable(fiber
->first_proc
);
1136 if (fiber
->prev
) rb_fiber_mark_self(fiber
->prev
);
1137 cont_mark(&fiber
->cont
);
1138 RUBY_MARK_LEAVE("cont");
1142 fiber_free(void *ptr
)
1144 rb_fiber_t
*fiber
= ptr
;
1145 RUBY_FREE_ENTER("fiber");
1147 if (DEBUG
) fprintf(stderr
, "fiber_free: %p[%p]\n", (void *)fiber
, fiber
->stack
.base
);
1149 if (fiber
->cont
.saved_ec
.local_storage
) {
1150 rb_id_table_free(fiber
->cont
.saved_ec
.local_storage
);
1153 cont_free(&fiber
->cont
);
1154 RUBY_FREE_LEAVE("fiber");
1158 fiber_memsize(const void *ptr
)
1160 const rb_fiber_t
*fiber
= ptr
;
1161 size_t size
= sizeof(*fiber
);
1162 const rb_execution_context_t
*saved_ec
= &fiber
->cont
.saved_ec
;
1163 const rb_thread_t
*th
= rb_ec_thread_ptr(saved_ec
);
1166 * vm.c::thread_memsize already counts th->ec->local_storage
1168 if (saved_ec
->local_storage
&& fiber
!= th
->root_fiber
) {
1169 size
+= rb_id_table_memsize(saved_ec
->local_storage
);
1170 size
+= rb_obj_memsize_of(saved_ec
->storage
);
1173 size
+= cont_memsize(&fiber
->cont
);
1178 rb_obj_is_fiber(VALUE obj
)
1180 return RBOOL(rb_typeddata_is_kind_of(obj
, &fiber_data_type
));
1184 cont_save_machine_stack(rb_thread_t
*th
, rb_context_t
*cont
)
1188 SET_MACHINE_STACK_END(&th
->ec
->machine
.stack_end
);
1190 if (th
->ec
->machine
.stack_start
> th
->ec
->machine
.stack_end
) {
1191 size
= cont
->machine
.stack_size
= th
->ec
->machine
.stack_start
- th
->ec
->machine
.stack_end
;
1192 cont
->machine
.stack_src
= th
->ec
->machine
.stack_end
;
1195 size
= cont
->machine
.stack_size
= th
->ec
->machine
.stack_end
- th
->ec
->machine
.stack_start
;
1196 cont
->machine
.stack_src
= th
->ec
->machine
.stack_start
;
1199 if (cont
->machine
.stack
) {
1200 REALLOC_N(cont
->machine
.stack
, VALUE
, size
);
1203 cont
->machine
.stack
= ALLOC_N(VALUE
, size
);
1206 FLUSH_REGISTER_WINDOWS
;
1207 asan_unpoison_memory_region(cont
->machine
.stack_src
, size
, false);
1208 MEMCPY(cont
->machine
.stack
, cont
->machine
.stack_src
, VALUE
, size
);
1211 static const rb_data_type_t cont_data_type
= {
1213 {cont_mark
, cont_free
, cont_memsize
, cont_compact
},
1214 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1218 cont_save_thread(rb_context_t
*cont
, rb_thread_t
*th
)
1220 rb_execution_context_t
*sec
= &cont
->saved_ec
;
1222 VM_ASSERT(th
->status
== THREAD_RUNNABLE
);
1224 /* save thread context */
1227 /* saved_ec->machine.stack_end should be NULL */
1228 /* because it may happen GC afterward */
1229 sec
->machine
.stack_end
= NULL
;
1232 static rb_nativethread_lock_t jit_cont_lock
;
1234 // Register a new continuation with execution context `ec`. Return JIT info about
1235 // the continuation.
1236 static struct rb_jit_cont
*
1237 jit_cont_new(rb_execution_context_t
*ec
)
1239 struct rb_jit_cont
*cont
;
1241 // We need to use calloc instead of something like ZALLOC to avoid triggering GC here.
1242 // When this function is called from rb_thread_alloc through rb_threadptr_root_fiber_setup,
1243 // the thread is still being prepared and marking it causes SEGV.
1244 cont
= calloc(1, sizeof(struct rb_jit_cont
));
1249 rb_native_mutex_lock(&jit_cont_lock
);
1250 if (first_jit_cont
== NULL
) {
1251 cont
->next
= cont
->prev
= NULL
;
1255 cont
->next
= first_jit_cont
;
1256 first_jit_cont
->prev
= cont
;
1258 first_jit_cont
= cont
;
1259 rb_native_mutex_unlock(&jit_cont_lock
);
1264 // Unregister continuation `cont`.
1266 jit_cont_free(struct rb_jit_cont
*cont
)
1270 rb_native_mutex_lock(&jit_cont_lock
);
1271 if (cont
== first_jit_cont
) {
1272 first_jit_cont
= cont
->next
;
1273 if (first_jit_cont
!= NULL
)
1274 first_jit_cont
->prev
= NULL
;
1277 cont
->prev
->next
= cont
->next
;
1278 if (cont
->next
!= NULL
)
1279 cont
->next
->prev
= cont
->prev
;
1281 rb_native_mutex_unlock(&jit_cont_lock
);
1286 // Call a given callback against all on-stack ISEQs.
1288 rb_jit_cont_each_iseq(rb_iseq_callback callback
, void *data
)
1290 struct rb_jit_cont
*cont
;
1291 for (cont
= first_jit_cont
; cont
!= NULL
; cont
= cont
->next
) {
1292 if (cont
->ec
->vm_stack
== NULL
)
1295 const rb_control_frame_t
*cfp
= cont
->ec
->cfp
;
1296 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(cont
->ec
, cfp
)) {
1297 if (cfp
->pc
&& cfp
->iseq
&& imemo_type((VALUE
)cfp
->iseq
) == imemo_iseq
) {
1298 callback(cfp
->iseq
, data
);
1300 cfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp
);
1306 // Update the jit_return of all CFPs to leave_exit unless it's leave_exception or not set.
1307 // This prevents jit_exec_exception from jumping to the caller after invalidation.
1309 rb_yjit_cancel_jit_return(void *leave_exit
, void *leave_exception
)
1311 struct rb_jit_cont
*cont
;
1312 for (cont
= first_jit_cont
; cont
!= NULL
; cont
= cont
->next
) {
1313 if (cont
->ec
->vm_stack
== NULL
)
1316 const rb_control_frame_t
*cfp
= cont
->ec
->cfp
;
1317 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(cont
->ec
, cfp
)) {
1318 if (cfp
->jit_return
&& cfp
->jit_return
!= leave_exception
) {
1319 ((rb_control_frame_t
*)cfp
)->jit_return
= leave_exit
;
1321 cfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp
);
1327 // Finish working with jit_cont.
1329 rb_jit_cont_finish(void)
1331 struct rb_jit_cont
*cont
, *next
;
1332 for (cont
= first_jit_cont
; cont
!= NULL
; cont
= next
) {
1334 free(cont
); // Don't use xfree because it's allocated by calloc.
1336 rb_native_mutex_destroy(&jit_cont_lock
);
1340 cont_init_jit_cont(rb_context_t
*cont
)
1342 VM_ASSERT(cont
->jit_cont
== NULL
);
1343 // We always allocate this since YJIT may be enabled later
1344 cont
->jit_cont
= jit_cont_new(&(cont
->saved_ec
));
1347 struct rb_execution_context_struct
*
1348 rb_fiberptr_get_ec(struct rb_fiber_struct
*fiber
)
1350 return &fiber
->cont
.saved_ec
;
1354 cont_init(rb_context_t
*cont
, rb_thread_t
*th
)
1356 /* save thread context */
1357 cont_save_thread(cont
, th
);
1358 cont
->saved_ec
.thread_ptr
= th
;
1359 cont
->saved_ec
.local_storage
= NULL
;
1360 cont
->saved_ec
.local_storage_recursive_hash
= Qnil
;
1361 cont
->saved_ec
.local_storage_recursive_hash_for_trace
= Qnil
;
1362 cont_init_jit_cont(cont
);
1365 static rb_context_t
*
1366 cont_new(VALUE klass
)
1369 volatile VALUE contval
;
1370 rb_thread_t
*th
= GET_THREAD();
1372 THREAD_MUST_BE_RUNNING(th
);
1373 contval
= TypedData_Make_Struct(klass
, rb_context_t
, &cont_data_type
, cont
);
1374 cont
->self
= contval
;
1375 cont_init(cont
, th
);
1380 rb_fiberptr_self(struct rb_fiber_struct
*fiber
)
1382 return fiber
->cont
.self
;
1386 rb_fiberptr_blocking(struct rb_fiber_struct
*fiber
)
1388 return fiber
->blocking
;
1391 // Initialize the jit_cont_lock
1393 rb_jit_cont_init(void)
1395 rb_native_mutex_initialize(&jit_cont_lock
);
1400 show_vm_stack(const rb_execution_context_t
*ec
)
1402 VALUE
*p
= ec
->vm_stack
;
1403 while (p
< ec
->cfp
->sp
) {
1404 fprintf(stderr
, "%3d ", (int)(p
- ec
->vm_stack
));
1405 rb_obj_info_dump(*p
);
1411 show_vm_pcs(const rb_control_frame_t
*cfp
,
1412 const rb_control_frame_t
*end_of_cfp
)
1415 while (cfp
!= end_of_cfp
) {
1418 pc
= cfp
->pc
- ISEQ_BODY(cfp
->iseq
)->iseq_encoded
;
1420 fprintf(stderr
, "%2d pc: %d\n", i
++, pc
);
1421 cfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp
);
1427 cont_capture(volatile int *volatile stat
)
1429 rb_context_t
*volatile cont
;
1430 rb_thread_t
*th
= GET_THREAD();
1431 volatile VALUE contval
;
1432 const rb_execution_context_t
*ec
= th
->ec
;
1434 THREAD_MUST_BE_RUNNING(th
);
1435 rb_vm_stack_to_heap(th
->ec
);
1436 cont
= cont_new(rb_cContinuation
);
1437 contval
= cont
->self
;
1439 #ifdef CAPTURE_JUST_VALID_VM_STACK
1440 cont
->saved_vm_stack
.slen
= ec
->cfp
->sp
- ec
->vm_stack
;
1441 cont
->saved_vm_stack
.clen
= ec
->vm_stack
+ ec
->vm_stack_size
- (VALUE
*)ec
->cfp
;
1442 cont
->saved_vm_stack
.ptr
= ALLOC_N(VALUE
, cont
->saved_vm_stack
.slen
+ cont
->saved_vm_stack
.clen
);
1443 MEMCPY(cont
->saved_vm_stack
.ptr
,
1445 VALUE
, cont
->saved_vm_stack
.slen
);
1446 MEMCPY(cont
->saved_vm_stack
.ptr
+ cont
->saved_vm_stack
.slen
,
1449 cont
->saved_vm_stack
.clen
);
1451 cont
->saved_vm_stack
.ptr
= ALLOC_N(VALUE
, ec
->vm_stack_size
);
1452 MEMCPY(cont
->saved_vm_stack
.ptr
, ec
->vm_stack
, VALUE
, ec
->vm_stack_size
);
1454 // At this point, `cfp` is valid but `vm_stack` should be cleared:
1455 rb_ec_set_vm_stack(&cont
->saved_ec
, NULL
, 0);
1456 VM_ASSERT(cont
->saved_ec
.cfp
!= NULL
);
1457 cont_save_machine_stack(th
, cont
);
1459 if (ruby_setjmp(cont
->jmpbuf
)) {
1462 VAR_INITIALIZED(cont
);
1463 value
= cont
->value
;
1464 if (cont
->argc
== -1) rb_exc_raise(value
);
1476 cont_restore_thread(rb_context_t
*cont
)
1478 rb_thread_t
*th
= GET_THREAD();
1480 /* restore thread context */
1481 if (cont
->type
== CONTINUATION_CONTEXT
) {
1483 rb_execution_context_t
*sec
= &cont
->saved_ec
;
1484 rb_fiber_t
*fiber
= NULL
;
1486 if (sec
->fiber_ptr
!= NULL
) {
1487 fiber
= sec
->fiber_ptr
;
1489 else if (th
->root_fiber
) {
1490 fiber
= th
->root_fiber
;
1493 if (fiber
&& th
->ec
!= &fiber
->cont
.saved_ec
) {
1494 ec_switch(th
, fiber
);
1497 if (th
->ec
->trace_arg
!= sec
->trace_arg
) {
1498 rb_raise(rb_eRuntimeError
, "can't call across trace_func");
1502 #ifdef CAPTURE_JUST_VALID_VM_STACK
1503 MEMCPY(th
->ec
->vm_stack
,
1504 cont
->saved_vm_stack
.ptr
,
1505 VALUE
, cont
->saved_vm_stack
.slen
);
1506 MEMCPY(th
->ec
->vm_stack
+ th
->ec
->vm_stack_size
- cont
->saved_vm_stack
.clen
,
1507 cont
->saved_vm_stack
.ptr
+ cont
->saved_vm_stack
.slen
,
1508 VALUE
, cont
->saved_vm_stack
.clen
);
1510 MEMCPY(th
->ec
->vm_stack
, cont
->saved_vm_stack
.ptr
, VALUE
, sec
->vm_stack_size
);
1512 /* other members of ec */
1514 th
->ec
->cfp
= sec
->cfp
;
1515 th
->ec
->raised_flag
= sec
->raised_flag
;
1516 th
->ec
->tag
= sec
->tag
;
1517 th
->ec
->root_lep
= sec
->root_lep
;
1518 th
->ec
->root_svar
= sec
->root_svar
;
1519 th
->ec
->errinfo
= sec
->errinfo
;
1521 VM_ASSERT(th
->ec
->vm_stack
!= NULL
);
1525 fiber_restore_thread(th
, (rb_fiber_t
*)cont
);
1529 NOINLINE(static void fiber_setcontext(rb_fiber_t
*new_fiber
, rb_fiber_t
*old_fiber
));
1532 fiber_setcontext(rb_fiber_t
*new_fiber
, rb_fiber_t
*old_fiber
)
1534 rb_thread_t
*th
= GET_THREAD();
1536 /* save old_fiber's machine stack - to ensure efficient garbage collection */
1537 if (!FIBER_TERMINATED_P(old_fiber
)) {
1538 STACK_GROW_DIR_DETECTION
;
1539 SET_MACHINE_STACK_END(&th
->ec
->machine
.stack_end
);
1540 if (STACK_DIR_UPPER(0, 1)) {
1541 old_fiber
->cont
.machine
.stack_size
= th
->ec
->machine
.stack_start
- th
->ec
->machine
.stack_end
;
1542 old_fiber
->cont
.machine
.stack
= th
->ec
->machine
.stack_end
;
1545 old_fiber
->cont
.machine
.stack_size
= th
->ec
->machine
.stack_end
- th
->ec
->machine
.stack_start
;
1546 old_fiber
->cont
.machine
.stack
= th
->ec
->machine
.stack_start
;
1550 /* these values are used in rb_gc_mark_machine_context to mark the fiber's stack. */
1551 old_fiber
->cont
.saved_ec
.machine
.stack_start
= th
->ec
->machine
.stack_start
;
1552 old_fiber
->cont
.saved_ec
.machine
.stack_end
= FIBER_TERMINATED_P(old_fiber
) ? NULL
: th
->ec
->machine
.stack_end
;
1555 // 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);
1557 #if defined(COROUTINE_SANITIZE_ADDRESS)
1558 __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
);
1561 /* swap machine context */
1562 struct coroutine_context
* from
= coroutine_transfer(&old_fiber
->context
, &new_fiber
->context
);
1564 #if defined(COROUTINE_SANITIZE_ADDRESS)
1565 __sanitizer_finish_switch_fiber(old_fiber
->context
.fake_stack
, NULL
, NULL
);
1569 rb_syserr_fail(errno
, "coroutine_transfer");
1572 /* restore thread context */
1573 fiber_restore_thread(th
, old_fiber
);
1575 // It's possible to get here, and new_fiber is already freed.
1576 // 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);
1579 NOINLINE(NORETURN(static void cont_restore_1(rb_context_t
*)));
1582 cont_restore_1(rb_context_t
*cont
)
1584 cont_restore_thread(cont
);
1586 /* restore machine stack */
1587 #if defined(_M_AMD64) && !defined(__MINGW64__)
1589 /* workaround for x64 SEH */
1592 _JUMP_BUFFER
*bp
= (void*)&cont
->jmpbuf
;
1593 bp
->Frame
= ((_JUMP_BUFFER
*)((void*)&buf
))->Frame
;
1596 if (cont
->machine
.stack_src
) {
1597 FLUSH_REGISTER_WINDOWS
;
1598 MEMCPY(cont
->machine
.stack_src
, cont
->machine
.stack
,
1599 VALUE
, cont
->machine
.stack_size
);
1602 ruby_longjmp(cont
->jmpbuf
, 1);
1605 NORETURN(NOINLINE(static void cont_restore_0(rb_context_t
*, VALUE
*)));
1608 cont_restore_0(rb_context_t
*cont
, VALUE
*addr_in_prev_frame
)
1610 if (cont
->machine
.stack_src
) {
1612 #define STACK_PAD_SIZE 1
1614 #define STACK_PAD_SIZE 1024
1616 VALUE space
[STACK_PAD_SIZE
];
1618 #if !STACK_GROW_DIRECTION
1619 if (addr_in_prev_frame
> &space
[0]) {
1620 /* Stack grows downward */
1622 #if STACK_GROW_DIRECTION <= 0
1623 volatile VALUE
*const end
= cont
->machine
.stack_src
;
1624 if (&space
[0] > end
) {
1626 volatile VALUE
*sp
= ALLOCA_N(VALUE
, &space
[0] - end
);
1627 // We need to make sure that the stack pointer is moved,
1628 // but some compilers may remove the allocation by optimization.
1629 // We hope that the following read/write will prevent such an optimization.
1633 cont_restore_0(cont
, &space
[0]);
1637 #if !STACK_GROW_DIRECTION
1640 /* Stack grows upward */
1642 #if STACK_GROW_DIRECTION >= 0
1643 volatile VALUE
*const end
= cont
->machine
.stack_src
+ cont
->machine
.stack_size
;
1644 if (&space
[STACK_PAD_SIZE
] < end
) {
1646 volatile VALUE
*sp
= ALLOCA_N(VALUE
, end
- &space
[STACK_PAD_SIZE
]);
1649 cont_restore_0(cont
, &space
[STACK_PAD_SIZE
-1]);
1653 #if !STACK_GROW_DIRECTION
1657 cont_restore_1(cont
);
1661 * Document-class: Continuation
1663 * Continuation objects are generated by Kernel#callcc,
1664 * after having +require+d <i>continuation</i>. They hold
1665 * a return address and execution context, allowing a nonlocal return
1666 * to the end of the #callcc block from anywhere within a
1667 * program. Continuations are somewhat analogous to a structured
1668 * version of C's <code>setjmp/longjmp</code> (although they contain
1669 * more state, so you might consider them closer to threads).
1673 * require "continuation"
1674 * arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ]
1675 * callcc{|cc| $cc = cc}
1676 * puts(message = arr.shift)
1677 * $cc.call unless message =~ /Max/
1679 * <em>produces:</em>
1686 * Also you can call callcc in other methods:
1688 * require "continuation"
1691 * arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ]
1692 * cc = callcc { |cc| cc }
1694 * return cc, arr.size
1699 * c.call(c) if size > 1
1704 * This (somewhat contrived) example allows the inner loop to abandon
1707 * require "continuation"
1711 * for j in i*5...(i+1)*5
1712 * cont.call() if j == 17
1719 * <em>produces:</em>
1729 * callcc {|cont| block } -> obj
1731 * Generates a Continuation object, which it passes to
1732 * the associated block. You need to <code>require
1733 * 'continuation'</code> before using this method. Performing a
1734 * <em>cont</em><code>.call</code> will cause the #callcc
1735 * to return (as will falling through the end of the block). The
1736 * value returned by the #callcc is the value of the
1737 * block, or the value passed to <em>cont</em><code>.call</code>. See
1738 * class Continuation for more details. Also see
1739 * Kernel#throw for an alternative mechanism for
1740 * unwinding a call stack.
1744 rb_callcc(VALUE self
)
1746 volatile int called
;
1747 volatile VALUE val
= cont_capture(&called
);
1753 return rb_yield(val
);
1756 #ifdef RUBY_ASAN_ENABLED
1757 /* callcc can't possibly work with ASAN; see bug #20273. Also this function
1758 * definition below avoids a "defined and not used" warning. */
1759 MAYBE_UNUSED(static void notusing_callcc(void)) { rb_callcc(Qnil
); }
1760 # define rb_callcc rb_f_notimplement
1765 make_passing_arg(int argc
, const VALUE
*argv
)
1775 return rb_ary_new4(argc
, argv
);
1779 typedef VALUE
e_proc(VALUE
);
1781 NORETURN(static VALUE
rb_cont_call(int argc
, VALUE
*argv
, VALUE contval
));
1785 * cont.call(args, ...)
1788 * Invokes the continuation. The program continues from the end of
1789 * the #callcc block. If no arguments are given, the original #callcc
1790 * returns +nil+. If one argument is given, #callcc returns
1791 * it. Otherwise, an array containing <i>args</i> is returned.
1793 * callcc {|cont| cont.call } #=> nil
1794 * callcc {|cont| cont.call 1 } #=> 1
1795 * callcc {|cont| cont.call 1, 2, 3 } #=> [1, 2, 3]
1799 rb_cont_call(int argc
, VALUE
*argv
, VALUE contval
)
1801 rb_context_t
*cont
= cont_ptr(contval
);
1802 rb_thread_t
*th
= GET_THREAD();
1804 if (cont_thread_value(cont
) != th
->self
) {
1805 rb_raise(rb_eRuntimeError
, "continuation called across threads");
1807 if (cont
->saved_ec
.fiber_ptr
) {
1808 if (th
->ec
->fiber_ptr
!= cont
->saved_ec
.fiber_ptr
) {
1809 rb_raise(rb_eRuntimeError
, "continuation called across fiber");
1814 cont
->value
= make_passing_arg(argc
, argv
);
1816 cont_restore_0(cont
, &contval
);
1817 UNREACHABLE_RETURN(Qnil
);
1825 * Document-class: Fiber
1827 * Fibers are primitives for implementing light weight cooperative
1828 * concurrency in Ruby. Basically they are a means of creating code blocks
1829 * that can be paused and resumed, much like threads. The main difference
1830 * is that they are never preempted and that the scheduling must be done by
1831 * the programmer and not the VM.
1833 * As opposed to other stackless light weight concurrency models, each fiber
1834 * comes with a stack. This enables the fiber to be paused from deeply
1835 * nested function calls within the fiber block. See the ruby(1)
1836 * manpage to configure the size of the fiber stack(s).
1838 * When a fiber is created it will not run automatically. Rather it must
1839 * be explicitly asked to run using the Fiber#resume method.
1840 * The code running inside the fiber can give up control by calling
1841 * Fiber.yield in which case it yields control back to caller (the
1842 * caller of the Fiber#resume).
1844 * Upon yielding or termination the Fiber returns the value of the last
1845 * executed expression
1849 * fiber = Fiber.new do
1862 * FiberError: dead fiber called
1864 * The Fiber#resume method accepts an arbitrary number of parameters,
1865 * if it is the first call to #resume then they will be passed as
1866 * block arguments. Otherwise they will be the return value of the
1867 * call to Fiber.yield
1871 * fiber = Fiber.new do |first|
1872 * second = Fiber.yield first + 2
1875 * puts fiber.resume 10
1876 * puts fiber.resume 1_000_000
1877 * puts fiber.resume "The fiber will be dead before I can cause trouble"
1883 * FiberError: dead fiber called
1885 * == Non-blocking Fibers
1887 * The concept of <em>non-blocking fiber</em> was introduced in Ruby 3.0.
1888 * A non-blocking fiber, when reaching a operation that would normally block
1889 * the fiber (like <code>sleep</code>, or wait for another process or I/O)
1890 * will yield control to other fibers and allow the <em>scheduler</em> to
1891 * handle blocking and waking up (resuming) this fiber when it can proceed.
1893 * For a Fiber to behave as non-blocking, it need to be created in Fiber.new with
1894 * <tt>blocking: false</tt> (which is the default), and Fiber.scheduler
1895 * should be set with Fiber.set_scheduler. If Fiber.scheduler is not set in
1896 * the current thread, blocking and non-blocking fibers' behavior is identical.
1898 * Ruby doesn't provide a scheduler class: it is expected to be implemented by
1899 * the user and correspond to Fiber::Scheduler.
1901 * There is also Fiber.schedule method, which is expected to immediately perform
1902 * the given block in a non-blocking manner. Its actual implementation is up to
1907 static const rb_data_type_t fiber_data_type
= {
1909 {fiber_mark
, fiber_free
, fiber_memsize
, fiber_compact
,},
1910 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1914 fiber_alloc(VALUE klass
)
1916 return TypedData_Wrap_Struct(klass
, &fiber_data_type
, 0);
1920 fiber_t_alloc(VALUE fiber_value
, unsigned int blocking
)
1923 rb_thread_t
*th
= GET_THREAD();
1925 if (DATA_PTR(fiber_value
) != 0) {
1926 rb_raise(rb_eRuntimeError
, "cannot initialize twice");
1929 THREAD_MUST_BE_RUNNING(th
);
1930 fiber
= ZALLOC(rb_fiber_t
);
1931 fiber
->cont
.self
= fiber_value
;
1932 fiber
->cont
.type
= FIBER_CONTEXT
;
1933 fiber
->blocking
= blocking
;
1935 cont_init(&fiber
->cont
, th
);
1937 fiber
->cont
.saved_ec
.fiber_ptr
= fiber
;
1938 rb_ec_clear_vm_stack(&fiber
->cont
.saved_ec
);
1942 /* fiber->status == 0 == CREATED
1943 * So that we don't need to set status: fiber_status_set(fiber, FIBER_CREATED); */
1944 VM_ASSERT(FIBER_CREATED_P(fiber
));
1946 DATA_PTR(fiber_value
) = fiber
;
1952 root_fiber_alloc(rb_thread_t
*th
)
1954 VALUE fiber_value
= fiber_alloc(rb_cFiber
);
1955 rb_fiber_t
*fiber
= th
->ec
->fiber_ptr
;
1957 VM_ASSERT(DATA_PTR(fiber_value
) == NULL
);
1958 VM_ASSERT(fiber
->cont
.type
== FIBER_CONTEXT
);
1959 VM_ASSERT(FIBER_RESUMED_P(fiber
));
1961 th
->root_fiber
= fiber
;
1962 DATA_PTR(fiber_value
) = fiber
;
1963 fiber
->cont
.self
= fiber_value
;
1965 coroutine_initialize_main(&fiber
->context
);
1970 static inline rb_fiber_t
*
1973 rb_execution_context_t
*ec
= GET_EC();
1974 if (ec
->fiber_ptr
->cont
.self
== 0) {
1975 root_fiber_alloc(rb_ec_thread_ptr(ec
));
1977 return ec
->fiber_ptr
;
1981 current_fiber_storage(void)
1983 rb_execution_context_t
*ec
= GET_EC();
1988 inherit_fiber_storage(void)
1990 return rb_obj_dup(current_fiber_storage());
1994 fiber_storage_set(struct rb_fiber_struct
*fiber
, VALUE storage
)
1996 fiber
->cont
.saved_ec
.storage
= storage
;
2000 fiber_storage_get(rb_fiber_t
*fiber
, int allocate
)
2002 VALUE storage
= fiber
->cont
.saved_ec
.storage
;
2003 if (storage
== Qnil
&& allocate
) {
2004 storage
= rb_hash_new();
2005 fiber_storage_set(fiber
, storage
);
2011 storage_access_must_be_from_same_fiber(VALUE self
)
2013 rb_fiber_t
*fiber
= fiber_ptr(self
);
2014 rb_fiber_t
*current
= fiber_current();
2015 if (fiber
!= current
) {
2016 rb_raise(rb_eArgError
, "Fiber storage can only be accessed from the Fiber it belongs to");
2021 * call-seq: fiber.storage -> hash (dup)
2023 * Returns a copy of the storage hash for the fiber. The method can only be called on the
2027 rb_fiber_storage_get(VALUE self
)
2029 storage_access_must_be_from_same_fiber(self
);
2031 VALUE storage
= fiber_storage_get(fiber_ptr(self
), FALSE
);
2033 if (storage
== Qnil
) {
2037 return rb_obj_dup(storage
);
2042 fiber_storage_validate_each(VALUE key
, VALUE value
, VALUE _argument
)
2044 Check_Type(key
, T_SYMBOL
);
2050 fiber_storage_validate(VALUE value
)
2052 // nil is an allowed value and will be lazily initialized.
2053 if (value
== Qnil
) return;
2055 if (!RB_TYPE_P(value
, T_HASH
)) {
2056 rb_raise(rb_eTypeError
, "storage must be a hash");
2059 if (RB_OBJ_FROZEN(value
)) {
2060 rb_raise(rb_eFrozenError
, "storage must not be frozen");
2063 rb_hash_foreach(value
, fiber_storage_validate_each
, Qundef
);
2067 * call-seq: fiber.storage = hash
2069 * Sets the storage hash for the fiber. This feature is experimental
2070 * and may change in the future. The method can only be called on the
2073 * You should be careful about using this method as you may inadvertently clear
2074 * important fiber-storage state. You should mostly prefer to assign specific
2075 * keys in the storage using Fiber::[]=.
2077 * You can also use <tt>Fiber.new(storage: nil)</tt> to create a fiber with an empty
2082 * while request = request_queue.pop
2083 * # Reset the per-request state:
2084 * Fiber.current.storage = nil
2085 * handle_request(request)
2089 rb_fiber_storage_set(VALUE self
, VALUE value
)
2091 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL
)) {
2092 rb_category_warn(RB_WARN_CATEGORY_EXPERIMENTAL
,
2093 "Fiber#storage= is experimental and may be removed in the future!");
2096 storage_access_must_be_from_same_fiber(self
);
2097 fiber_storage_validate(value
);
2099 fiber_ptr(self
)->cont
.saved_ec
.storage
= rb_obj_dup(value
);
2104 * call-seq: Fiber[key] -> value
2106 * Returns the value of the fiber storage variable identified by +key+.
2108 * The +key+ must be a symbol, and the value is set by Fiber#[]= or
2111 * See also Fiber::[]=.
2114 rb_fiber_storage_aref(VALUE
class, VALUE key
)
2116 Check_Type(key
, T_SYMBOL
);
2118 VALUE storage
= fiber_storage_get(fiber_current(), FALSE
);
2119 if (storage
== Qnil
) return Qnil
;
2121 return rb_hash_aref(storage
, key
);
2125 * call-seq: Fiber[key] = value
2127 * Assign +value+ to the fiber storage variable identified by +key+.
2128 * The variable is created if it doesn't exist.
2130 * +key+ must be a Symbol, otherwise a TypeError is raised.
2132 * See also Fiber::[].
2135 rb_fiber_storage_aset(VALUE
class, VALUE key
, VALUE value
)
2137 Check_Type(key
, T_SYMBOL
);
2139 VALUE storage
= fiber_storage_get(fiber_current(), value
!= Qnil
);
2140 if (storage
== Qnil
) return Qnil
;
2142 if (value
== Qnil
) {
2143 return rb_hash_delete(storage
, key
);
2146 return rb_hash_aset(storage
, key
, value
);
2151 fiber_initialize(VALUE self
, VALUE proc
, struct fiber_pool
* fiber_pool
, unsigned int blocking
, VALUE storage
)
2153 if (storage
== Qundef
|| storage
== Qtrue
) {
2154 // The default, inherit storage (dup) from the current fiber:
2155 storage
= inherit_fiber_storage();
2157 else /* nil, hash, etc. */ {
2158 fiber_storage_validate(storage
);
2159 storage
= rb_obj_dup(storage
);
2162 rb_fiber_t
*fiber
= fiber_t_alloc(self
, blocking
);
2164 fiber
->cont
.saved_ec
.storage
= storage
;
2165 fiber
->first_proc
= proc
;
2166 fiber
->stack
.base
= NULL
;
2167 fiber
->stack
.pool
= fiber_pool
;
2173 fiber_prepare_stack(rb_fiber_t
*fiber
)
2175 rb_context_t
*cont
= &fiber
->cont
;
2176 rb_execution_context_t
*sec
= &cont
->saved_ec
;
2178 size_t vm_stack_size
= 0;
2179 VALUE
*vm_stack
= fiber_initialize_coroutine(fiber
, &vm_stack_size
);
2181 /* initialize cont */
2182 cont
->saved_vm_stack
.ptr
= NULL
;
2183 rb_ec_initialize_vm_stack(sec
, vm_stack
, vm_stack_size
/ sizeof(VALUE
));
2186 sec
->local_storage
= NULL
;
2187 sec
->local_storage_recursive_hash
= Qnil
;
2188 sec
->local_storage_recursive_hash_for_trace
= Qnil
;
2191 static struct fiber_pool
*
2192 rb_fiber_pool_default(VALUE pool
)
2194 return &shared_fiber_pool
;
2197 VALUE
rb_fiber_inherit_storage(struct rb_execution_context_struct
*ec
, struct rb_fiber_struct
*fiber
)
2199 VALUE storage
= rb_obj_dup(ec
->storage
);
2200 fiber
->cont
.saved_ec
.storage
= storage
;
2206 rb_fiber_initialize_kw(int argc
, VALUE
* argv
, VALUE self
, int kw_splat
)
2209 VALUE blocking
= Qfalse
;
2210 VALUE storage
= Qundef
;
2212 if (kw_splat
!= RB_NO_KEYWORDS
) {
2213 VALUE options
= Qnil
;
2214 VALUE arguments
[3] = {Qundef
};
2216 argc
= rb_scan_args_kw(kw_splat
, argc
, argv
, ":", &options
);
2217 rb_get_kwargs(options
, fiber_initialize_keywords
, 0, 3, arguments
);
2219 if (!UNDEF_P(arguments
[0])) {
2220 blocking
= arguments
[0];
2223 if (!UNDEF_P(arguments
[1])) {
2224 pool
= arguments
[1];
2227 storage
= arguments
[2];
2230 return fiber_initialize(self
, rb_block_proc(), rb_fiber_pool_default(pool
), RTEST(blocking
), storage
);
2235 * Fiber.new(blocking: false, storage: true) { |*args| ... } -> fiber
2237 * Creates new Fiber. Initially, the fiber is not running and can be resumed
2238 * with #resume. Arguments to the first #resume call will be passed to the
2241 * f = Fiber.new do |initial|
2244 * puts "current: #{current.inspect}"
2245 * current = Fiber.yield
2248 * f.resume(100) # prints: current: 100
2249 * f.resume(1, 2, 3) # prints: current: [1, 2, 3]
2250 * f.resume # prints: current: nil
2251 * # ... and so on ...
2253 * If <tt>blocking: false</tt> is passed to <tt>Fiber.new</tt>, _and_ current
2254 * thread has a Fiber.scheduler defined, the Fiber becomes non-blocking (see
2255 * "Non-blocking Fibers" section in class docs).
2257 * If the <tt>storage</tt> is unspecified, the default is to inherit a copy of
2258 * the storage from the current fiber. This is the same as specifying
2259 * <tt>storage: true</tt>.
2268 * If the given <tt>storage</tt> is <tt>nil</tt>, this function will lazy
2269 * initialize the internal storage, which starts as an empty hash.
2271 * Fiber[:x] = "Hello World"
2272 * Fiber.new(storage: nil) do
2276 * Otherwise, the given <tt>storage</tt> is used as the new fiber's storage,
2277 * and it must be an instance of Hash.
2279 * Explicitly using <tt>storage: true</tt> is currently experimental and may
2280 * change in the future.
2283 rb_fiber_initialize(int argc
, VALUE
* argv
, VALUE self
)
2285 return rb_fiber_initialize_kw(argc
, argv
, self
, rb_keyword_given_p());
2289 rb_fiber_new_storage(rb_block_call_func_t func
, VALUE obj
, VALUE storage
)
2291 return fiber_initialize(fiber_alloc(rb_cFiber
), rb_proc_new(func
, obj
), rb_fiber_pool_default(Qnil
), 0, storage
);
2295 rb_fiber_new(rb_block_call_func_t func
, VALUE obj
)
2297 return rb_fiber_new_storage(func
, obj
, Qtrue
);
2301 rb_fiber_s_schedule_kw(int argc
, VALUE
* argv
, int kw_splat
)
2303 rb_thread_t
* th
= GET_THREAD();
2304 VALUE scheduler
= th
->scheduler
;
2307 if (scheduler
!= Qnil
) {
2308 fiber
= rb_fiber_scheduler_fiber(scheduler
, argc
, argv
, kw_splat
);
2311 rb_raise(rb_eRuntimeError
, "No scheduler is available!");
2319 * Fiber.schedule { |*args| ... } -> fiber
2321 * The method is <em>expected</em> to immediately run the provided block of code in a
2322 * separate non-blocking fiber.
2324 * puts "Go to sleep!"
2326 * Fiber.set_scheduler(MyScheduler.new)
2329 * puts "Going to sleep"
2331 * puts "I slept well"
2334 * puts "Wakey-wakey, sleepyhead"
2336 * Assuming MyScheduler is properly implemented, this program will produce:
2340 * Wakey-wakey, sleepyhead
2341 * ...1 sec pause here...
2344 * ...e.g. on the first blocking operation inside the Fiber (<tt>sleep(1)</tt>),
2345 * the control is yielded to the outside code (main fiber), and <em>at the end
2346 * of that execution</em>, the scheduler takes care of properly resuming all the
2349 * Note that the behavior described above is how the method is <em>expected</em>
2350 * to behave, actual behavior is up to the current scheduler's implementation of
2351 * Fiber::Scheduler#fiber method. Ruby doesn't enforce this method to
2352 * behave in any particular way.
2354 * If the scheduler is not set, the method raises
2355 * <tt>RuntimeError (No scheduler is available!)</tt>.
2359 rb_fiber_s_schedule(int argc
, VALUE
*argv
, VALUE obj
)
2361 return rb_fiber_s_schedule_kw(argc
, argv
, rb_keyword_given_p());
2366 * Fiber.scheduler -> obj or nil
2368 * Returns the Fiber scheduler, that was last set for the current thread with Fiber.set_scheduler.
2369 * Returns +nil+ if no scheduler is set (which is the default), and non-blocking fibers'
2370 * behavior is the same as blocking.
2371 * (see "Non-blocking fibers" section in class docs for details about the scheduler concept).
2375 rb_fiber_s_scheduler(VALUE klass
)
2377 return rb_fiber_scheduler_get();
2382 * Fiber.current_scheduler -> obj or nil
2384 * Returns the Fiber scheduler, that was last set for the current thread with Fiber.set_scheduler
2385 * if and only if the current fiber is non-blocking.
2389 rb_fiber_current_scheduler(VALUE klass
)
2391 return rb_fiber_scheduler_current();
2396 * Fiber.set_scheduler(scheduler) -> scheduler
2398 * Sets the Fiber scheduler for the current thread. If the scheduler is set, non-blocking
2399 * fibers (created by Fiber.new with <tt>blocking: false</tt>, or by Fiber.schedule)
2400 * call that scheduler's hook methods on potentially blocking operations, and the current
2401 * thread will call scheduler's +close+ method on finalization (allowing the scheduler to
2402 * properly manage all non-finished fibers).
2404 * +scheduler+ can be an object of any class corresponding to Fiber::Scheduler. Its
2405 * implementation is up to the user.
2407 * See also the "Non-blocking fibers" section in class docs.
2411 rb_fiber_set_scheduler(VALUE klass
, VALUE scheduler
)
2413 return rb_fiber_scheduler_set(scheduler
);
2416 NORETURN(static void rb_fiber_terminate(rb_fiber_t
*fiber
, int need_interrupt
, VALUE err
));
2419 rb_fiber_start(rb_fiber_t
*fiber
)
2421 rb_thread_t
* volatile th
= fiber
->cont
.saved_ec
.thread_ptr
;
2424 enum ruby_tag_type state
;
2426 VM_ASSERT(th
->ec
== GET_EC());
2427 VM_ASSERT(FIBER_RESUMED_P(fiber
));
2429 if (fiber
->blocking
) {
2433 EC_PUSH_TAG(th
->ec
);
2434 if ((state
= EC_EXEC_TAG()) == TAG_NONE
) {
2435 rb_context_t
*cont
= &VAR_FROM_MEMORY(fiber
)->cont
;
2437 const VALUE
*argv
, args
= cont
->value
;
2438 GetProcPtr(fiber
->first_proc
, proc
);
2439 argv
= (argc
= cont
->argc
) > 1 ? RARRAY_CONST_PTR(args
) : &args
;
2441 th
->ec
->errinfo
= Qnil
;
2442 th
->ec
->root_lep
= rb_vm_proc_local_ep(fiber
->first_proc
);
2443 th
->ec
->root_svar
= Qfalse
;
2445 EXEC_EVENT_HOOK(th
->ec
, RUBY_EVENT_FIBER_SWITCH
, th
->self
, 0, 0, 0, Qnil
);
2446 cont
->value
= rb_vm_invoke_proc(th
->ec
, proc
, argc
, argv
, cont
->kw_splat
, VM_BLOCK_HANDLER_NONE
);
2450 int need_interrupt
= TRUE
;
2453 err
= th
->ec
->errinfo
;
2454 VM_ASSERT(FIBER_RESUMED_P(fiber
));
2456 if (state
== TAG_RAISE
) {
2459 else if (state
== TAG_FATAL
&& err
== RUBY_FATAL_FIBER_KILLED
) {
2460 need_interrupt
= FALSE
;
2463 else if (state
== TAG_FATAL
) {
2464 rb_threadptr_pending_interrupt_enque(th
, err
);
2467 err
= rb_vm_make_jump_tag_but_local_jump(state
, err
);
2471 rb_fiber_terminate(fiber
, need_interrupt
, err
);
2474 // Set up a "root fiber", which is the fiber that every Ractor has.
2476 rb_threadptr_root_fiber_setup(rb_thread_t
*th
)
2478 rb_fiber_t
*fiber
= ruby_mimcalloc(1, sizeof(rb_fiber_t
));
2480 rb_bug("%s", strerror(errno
)); /* ... is it possible to call rb_bug here? */
2482 fiber
->cont
.type
= FIBER_CONTEXT
;
2483 fiber
->cont
.saved_ec
.fiber_ptr
= fiber
;
2484 fiber
->cont
.saved_ec
.thread_ptr
= th
;
2485 fiber
->blocking
= 1;
2487 fiber_status_set(fiber
, FIBER_RESUMED
); /* skip CREATED */
2488 th
->ec
= &fiber
->cont
.saved_ec
;
2489 cont_init_jit_cont(&fiber
->cont
);
2493 rb_threadptr_root_fiber_release(rb_thread_t
*th
)
2495 if (th
->root_fiber
) {
2496 /* ignore. A root fiber object will free th->ec */
2499 rb_execution_context_t
*ec
= rb_current_execution_context(false);
2501 VM_ASSERT(th
->ec
->fiber_ptr
->cont
.type
== FIBER_CONTEXT
);
2502 VM_ASSERT(th
->ec
->fiber_ptr
->cont
.self
== 0);
2504 if (ec
&& th
->ec
== ec
) {
2505 rb_ractor_set_current_ec(th
->ractor
, NULL
);
2507 fiber_free(th
->ec
->fiber_ptr
);
2513 rb_threadptr_root_fiber_terminate(rb_thread_t
*th
)
2515 rb_fiber_t
*fiber
= th
->ec
->fiber_ptr
;
2517 fiber
->status
= FIBER_TERMINATED
;
2519 // The vm_stack is `alloca`ed on the thread stack, so it's gone too:
2520 rb_ec_clear_vm_stack(th
->ec
);
2523 static inline rb_fiber_t
*
2524 return_fiber(bool terminate
)
2526 rb_fiber_t
*fiber
= fiber_current();
2527 rb_fiber_t
*prev
= fiber
->prev
;
2531 prev
->resuming_fiber
= NULL
;
2536 rb_raise(rb_eFiberError
, "attempt to yield on a not resumed fiber");
2539 rb_thread_t
*th
= GET_THREAD();
2540 rb_fiber_t
*root_fiber
= th
->root_fiber
;
2542 VM_ASSERT(root_fiber
!= NULL
);
2544 // search resuming fiber
2545 for (fiber
= root_fiber
; fiber
->resuming_fiber
; fiber
= fiber
->resuming_fiber
) {
2553 rb_fiber_current(void)
2555 return fiber_current()->cont
.self
;
2558 // Prepare to execute next_fiber on the given thread.
2560 fiber_store(rb_fiber_t
*next_fiber
, rb_thread_t
*th
)
2564 if (th
->ec
->fiber_ptr
!= NULL
) {
2565 fiber
= th
->ec
->fiber_ptr
;
2568 /* create root fiber */
2569 fiber
= root_fiber_alloc(th
);
2572 if (FIBER_CREATED_P(next_fiber
)) {
2573 fiber_prepare_stack(next_fiber
);
2576 VM_ASSERT(FIBER_RESUMED_P(fiber
) || FIBER_TERMINATED_P(fiber
));
2577 VM_ASSERT(FIBER_RUNNABLE_P(next_fiber
));
2579 if (FIBER_RESUMED_P(fiber
)) fiber_status_set(fiber
, FIBER_SUSPENDED
);
2581 fiber_status_set(next_fiber
, FIBER_RESUMED
);
2582 fiber_setcontext(next_fiber
, fiber
);
2586 fiber_check_killed(rb_fiber_t
*fiber
)
2588 VM_ASSERT(fiber
== fiber_current());
2590 if (fiber
->killed
) {
2591 rb_thread_t
*thread
= fiber
->cont
.saved_ec
.thread_ptr
;
2593 thread
->ec
->errinfo
= RUBY_FATAL_FIBER_KILLED
;
2594 EC_JUMP_TAG(thread
->ec
, RUBY_TAG_FATAL
);
2599 fiber_switch(rb_fiber_t
*fiber
, int argc
, const VALUE
*argv
, int kw_splat
, rb_fiber_t
*resuming_fiber
, bool yielding
)
2602 rb_context_t
*cont
= &fiber
->cont
;
2603 rb_thread_t
*th
= GET_THREAD();
2605 /* make sure the root_fiber object is available */
2606 if (th
->root_fiber
== NULL
) root_fiber_alloc(th
);
2608 if (th
->ec
->fiber_ptr
== fiber
) {
2609 /* ignore fiber context switch
2610 * because destination fiber is the same as current fiber
2612 return make_passing_arg(argc
, argv
);
2615 if (cont_thread_value(cont
) != th
->self
) {
2616 rb_raise(rb_eFiberError
, "fiber called across threads");
2619 if (FIBER_TERMINATED_P(fiber
)) {
2620 value
= rb_exc_new2(rb_eFiberError
, "dead fiber called");
2622 if (!FIBER_TERMINATED_P(th
->ec
->fiber_ptr
)) {
2623 rb_exc_raise(value
);
2624 VM_UNREACHABLE(fiber_switch
);
2627 /* th->ec->fiber_ptr is also dead => switch to root fiber */
2628 /* (this means we're being called from rb_fiber_terminate, */
2629 /* and the terminated fiber's return_fiber() is already dead) */
2630 VM_ASSERT(FIBER_SUSPENDED_P(th
->root_fiber
));
2632 cont
= &th
->root_fiber
->cont
;
2634 cont
->value
= value
;
2636 fiber_setcontext(th
->root_fiber
, th
->ec
->fiber_ptr
);
2638 VM_UNREACHABLE(fiber_switch
);
2642 VM_ASSERT(FIBER_RUNNABLE_P(fiber
));
2644 rb_fiber_t
*current_fiber
= fiber_current();
2646 VM_ASSERT(!current_fiber
->resuming_fiber
);
2648 if (resuming_fiber
) {
2649 current_fiber
->resuming_fiber
= resuming_fiber
;
2650 fiber
->prev
= fiber_current();
2651 fiber
->yielding
= 0;
2654 VM_ASSERT(!current_fiber
->yielding
);
2656 current_fiber
->yielding
= 1;
2659 if (current_fiber
->blocking
) {
2664 cont
->kw_splat
= kw_splat
;
2665 cont
->value
= make_passing_arg(argc
, argv
);
2667 fiber_store(fiber
, th
);
2669 // We cannot free the stack until the pthread is joined:
2670 #ifndef COROUTINE_PTHREAD_CONTEXT
2671 if (resuming_fiber
&& FIBER_TERMINATED_P(fiber
)) {
2672 fiber_stack_release(fiber
);
2676 if (fiber_current()->blocking
) {
2680 RUBY_VM_CHECK_INTS(th
->ec
);
2682 EXEC_EVENT_HOOK(th
->ec
, RUBY_EVENT_FIBER_SWITCH
, th
->self
, 0, 0, 0, Qnil
);
2684 current_fiber
= th
->ec
->fiber_ptr
;
2685 value
= current_fiber
->cont
.value
;
2687 fiber_check_killed(current_fiber
);
2689 if (current_fiber
->cont
.argc
== -1) {
2690 // Fiber#raise will trigger this path.
2691 rb_exc_raise(value
);
2698 rb_fiber_transfer(VALUE fiber_value
, int argc
, const VALUE
*argv
)
2700 return fiber_switch(fiber_ptr(fiber_value
), argc
, argv
, RB_NO_KEYWORDS
, NULL
, false);
2705 * fiber.blocking? -> true or false
2707 * Returns +true+ if +fiber+ is blocking and +false+ otherwise.
2708 * Fiber is non-blocking if it was created via passing <tt>blocking: false</tt>
2709 * to Fiber.new, or via Fiber.schedule.
2711 * Note that, even if the method returns +false+, the fiber behaves differently
2712 * only if Fiber.scheduler is set in the current thread.
2714 * See the "Non-blocking fibers" section in class docs for details.
2718 rb_fiber_blocking_p(VALUE fiber
)
2720 return RBOOL(fiber_ptr(fiber
)->blocking
);
2724 fiber_blocking_yield(VALUE fiber_value
)
2726 rb_fiber_t
*fiber
= fiber_ptr(fiber_value
);
2727 rb_thread_t
* volatile th
= fiber
->cont
.saved_ec
.thread_ptr
;
2729 VM_ASSERT(fiber
->blocking
== 0);
2731 // fiber->blocking is `unsigned int : 1`, so we use it as a boolean:
2732 fiber
->blocking
= 1;
2734 // Once the fiber is blocking, and current, we increment the thread blocking state:
2737 return rb_yield(fiber_value
);
2741 fiber_blocking_ensure(VALUE fiber_value
)
2743 rb_fiber_t
*fiber
= fiber_ptr(fiber_value
);
2744 rb_thread_t
* volatile th
= fiber
->cont
.saved_ec
.thread_ptr
;
2746 // We are no longer blocking:
2747 fiber
->blocking
= 0;
2755 * Fiber.blocking{|fiber| ...} -> result
2757 * Forces the fiber to be blocking for the duration of the block. Returns the
2758 * result of the block.
2760 * See the "Non-blocking fibers" section in class docs for details.
2764 rb_fiber_blocking(VALUE
class)
2766 VALUE fiber_value
= rb_fiber_current();
2767 rb_fiber_t
*fiber
= fiber_ptr(fiber_value
);
2769 // If we are already blocking, this is essentially a no-op:
2770 if (fiber
->blocking
) {
2771 return rb_yield(fiber_value
);
2774 return rb_ensure(fiber_blocking_yield
, fiber_value
, fiber_blocking_ensure
, fiber_value
);
2780 * Fiber.blocking? -> false or 1
2782 * Returns +false+ if the current fiber is non-blocking.
2783 * Fiber is non-blocking if it was created via passing <tt>blocking: false</tt>
2784 * to Fiber.new, or via Fiber.schedule.
2786 * If the current Fiber is blocking, the method returns 1.
2787 * Future developments may allow for situations where larger integers
2788 * could be returned.
2790 * Note that, even if the method returns +false+, Fiber behaves differently
2791 * only if Fiber.scheduler is set in the current thread.
2793 * See the "Non-blocking fibers" section in class docs for details.
2797 rb_fiber_s_blocking_p(VALUE klass
)
2799 rb_thread_t
*thread
= GET_THREAD();
2800 unsigned blocking
= thread
->blocking
;
2805 return INT2NUM(blocking
);
2809 rb_fiber_close(rb_fiber_t
*fiber
)
2811 fiber_status_set(fiber
, FIBER_TERMINATED
);
2815 rb_fiber_terminate(rb_fiber_t
*fiber
, int need_interrupt
, VALUE error
)
2817 VALUE value
= fiber
->cont
.value
;
2819 VM_ASSERT(FIBER_RESUMED_P(fiber
));
2820 rb_fiber_close(fiber
);
2822 fiber
->cont
.machine
.stack
= NULL
;
2823 fiber
->cont
.machine
.stack_size
= 0;
2825 rb_fiber_t
*next_fiber
= return_fiber(true);
2827 if (need_interrupt
) RUBY_VM_SET_INTERRUPT(&next_fiber
->cont
.saved_ec
);
2830 fiber_switch(next_fiber
, -1, &error
, RB_NO_KEYWORDS
, NULL
, false);
2832 fiber_switch(next_fiber
, 1, &value
, RB_NO_KEYWORDS
, NULL
, false);
2837 fiber_resume_kw(rb_fiber_t
*fiber
, int argc
, const VALUE
*argv
, int kw_splat
)
2839 rb_fiber_t
*current_fiber
= fiber_current();
2841 if (argc
== -1 && FIBER_CREATED_P(fiber
)) {
2842 rb_raise(rb_eFiberError
, "cannot raise exception on unborn fiber");
2844 else if (FIBER_TERMINATED_P(fiber
)) {
2845 rb_raise(rb_eFiberError
, "attempt to resume a terminated fiber");
2847 else if (fiber
== current_fiber
) {
2848 rb_raise(rb_eFiberError
, "attempt to resume the current fiber");
2850 else if (fiber
->prev
!= NULL
) {
2851 rb_raise(rb_eFiberError
, "attempt to resume a resumed fiber (double resume)");
2853 else if (fiber
->resuming_fiber
) {
2854 rb_raise(rb_eFiberError
, "attempt to resume a resuming fiber");
2856 else if (fiber
->prev
== NULL
&&
2857 (!fiber
->yielding
&& fiber
->status
!= FIBER_CREATED
)) {
2858 rb_raise(rb_eFiberError
, "attempt to resume a transferring fiber");
2861 return fiber_switch(fiber
, argc
, argv
, kw_splat
, fiber
, false);
2865 rb_fiber_resume_kw(VALUE self
, int argc
, const VALUE
*argv
, int kw_splat
)
2867 return fiber_resume_kw(fiber_ptr(self
), argc
, argv
, kw_splat
);
2871 rb_fiber_resume(VALUE self
, int argc
, const VALUE
*argv
)
2873 return fiber_resume_kw(fiber_ptr(self
), argc
, argv
, RB_NO_KEYWORDS
);
2877 rb_fiber_yield_kw(int argc
, const VALUE
*argv
, int kw_splat
)
2879 return fiber_switch(return_fiber(false), argc
, argv
, kw_splat
, NULL
, true);
2883 rb_fiber_yield(int argc
, const VALUE
*argv
)
2885 return fiber_switch(return_fiber(false), argc
, argv
, RB_NO_KEYWORDS
, NULL
, true);
2889 rb_fiber_reset_root_local_storage(rb_thread_t
*th
)
2891 if (th
->root_fiber
&& th
->root_fiber
!= th
->ec
->fiber_ptr
) {
2892 th
->ec
->local_storage
= th
->root_fiber
->cont
.saved_ec
.local_storage
;
2898 * fiber.alive? -> true or false
2900 * Returns true if the fiber can still be resumed (or transferred
2901 * to). After finishing execution of the fiber block this method will
2902 * always return +false+.
2905 rb_fiber_alive_p(VALUE fiber_value
)
2907 return RBOOL(!FIBER_TERMINATED_P(fiber_ptr(fiber_value
)));
2912 * fiber.resume(args, ...) -> obj
2914 * Resumes the fiber from the point at which the last Fiber.yield was
2915 * called, or starts running it if it is the first call to
2916 * #resume. Arguments passed to resume will be the value of the
2917 * Fiber.yield expression or will be passed as block parameters to
2918 * the fiber's block if this is the first #resume.
2920 * Alternatively, when resume is called it evaluates to the arguments passed
2921 * to the next Fiber.yield statement inside the fiber's block
2922 * or to the block value if it runs to completion without any
2926 rb_fiber_m_resume(int argc
, VALUE
*argv
, VALUE fiber
)
2928 return rb_fiber_resume_kw(fiber
, argc
, argv
, rb_keyword_given_p());
2933 * fiber.backtrace -> array
2934 * fiber.backtrace(start) -> array
2935 * fiber.backtrace(start, count) -> array
2936 * fiber.backtrace(start..end) -> array
2938 * Returns the current execution stack of the fiber. +start+, +count+ and +end+ allow
2939 * to select only parts of the backtrace.
2953 * f = Fiber.new { level1 }
2955 * # It is empty before the fiber started
2962 * #=> ["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>'"]
2963 * p f.backtrace(1) # start from the item 1
2964 * #=> ["test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'", "test.rb:13:in `block in <main>'"]
2965 * p f.backtrace(2, 2) # start from item 2, take 2
2966 * #=> ["test.rb:6:in `level2'", "test.rb:10:in `level1'"]
2967 * p f.backtrace(1..3) # take items from 1 to 3
2968 * #=> ["test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'"]
2972 * # It is nil after the fiber is finished
2978 rb_fiber_backtrace(int argc
, VALUE
*argv
, VALUE fiber
)
2980 return rb_vm_backtrace(argc
, argv
, &fiber_ptr(fiber
)->cont
.saved_ec
);
2985 * fiber.backtrace_locations -> array
2986 * fiber.backtrace_locations(start) -> array
2987 * fiber.backtrace_locations(start, count) -> array
2988 * fiber.backtrace_locations(start..end) -> array
2990 * Like #backtrace, but returns each line of the execution stack as a
2991 * Thread::Backtrace::Location. Accepts the same arguments as #backtrace.
2993 * f = Fiber.new { Fiber.yield }
2995 * loc = f.backtrace_locations.first
2996 * loc.label #=> "yield"
2997 * loc.path #=> "test.rb"
3003 rb_fiber_backtrace_locations(int argc
, VALUE
*argv
, VALUE fiber
)
3005 return rb_vm_backtrace_locations(argc
, argv
, &fiber_ptr(fiber
)->cont
.saved_ec
);
3010 * fiber.transfer(args, ...) -> obj
3012 * Transfer control to another fiber, resuming it from where it last
3013 * stopped or starting it if it was not resumed before. The calling
3014 * fiber will be suspended much like in a call to
3017 * The fiber which receives the transfer call treats it much like
3018 * a resume call. Arguments passed to transfer are treated like those
3021 * The two style of control passing to and from fiber (one is #resume and
3022 * Fiber::yield, another is #transfer to and from fiber) can't be freely
3025 * * If the Fiber's lifecycle had started with transfer, it will never
3026 * be able to yield or be resumed control passing, only
3027 * finish or transfer back. (It still can resume other fibers that
3028 * are allowed to be resumed.)
3029 * * If the Fiber's lifecycle had started with resume, it can yield
3030 * or transfer to another Fiber, but can receive control back only
3031 * the way compatible with the way it was given away: if it had
3032 * transferred, it only can be transferred back, and if it had
3033 * yielded, it only can be resumed back. After that, it again can
3034 * transfer or yield.
3036 * If those rules are broken FiberError is raised.
3038 * For an individual Fiber design, yield/resume is easier to use
3039 * (the Fiber just gives away control, it doesn't need to think
3040 * about who the control is given to), while transfer is more flexible
3041 * for complex cases, allowing to build arbitrary graphs of Fibers
3042 * dependent on each other.
3047 * manager = nil # For local var to be visible inside worker block
3049 * # This fiber would be started with transfer
3050 * # It can't yield, and can't be resumed
3051 * worker = Fiber.new { |work|
3052 * puts "Worker: starts"
3053 * puts "Worker: Performed #{work.inspect}, transferring back"
3054 * # Fiber.yield # this would raise FiberError: attempt to yield on a not resumed fiber
3055 * # manager.resume # this would raise FiberError: attempt to resume a resumed fiber (double resume)
3056 * manager.transfer(work.capitalize)
3059 * # This fiber would be started with resume
3060 * # It can yield or transfer, and can be transferred
3062 * manager = Fiber.new {
3063 * puts "Manager: starts"
3064 * puts "Manager: transferring 'something' to worker"
3065 * result = worker.transfer('something')
3066 * puts "Manager: worker returned #{result.inspect}"
3067 * # worker.resume # this would raise FiberError: attempt to resume a transferring fiber
3068 * Fiber.yield # this is OK, the fiber transferred from and to, now it can yield
3069 * puts "Manager: finished"
3072 * puts "Starting the manager"
3074 * puts "Resuming the manager"
3075 * # manager.transfer # this would raise FiberError: attempt to transfer to a yielding fiber
3080 * Starting the manager
3082 * Manager: transferring 'something' to worker
3084 * Worker: Performed "something", transferring back
3085 * Manager: worker returned "Something"
3086 * Resuming the manager
3091 rb_fiber_m_transfer(int argc
, VALUE
*argv
, VALUE self
)
3093 return rb_fiber_transfer_kw(self
, argc
, argv
, rb_keyword_given_p());
3097 fiber_transfer_kw(rb_fiber_t
*fiber
, int argc
, const VALUE
*argv
, int kw_splat
)
3099 if (fiber
->resuming_fiber
) {
3100 rb_raise(rb_eFiberError
, "attempt to transfer to a resuming fiber");
3103 if (fiber
->yielding
) {
3104 rb_raise(rb_eFiberError
, "attempt to transfer to a yielding fiber");
3107 return fiber_switch(fiber
, argc
, argv
, kw_splat
, NULL
, false);
3111 rb_fiber_transfer_kw(VALUE self
, int argc
, const VALUE
*argv
, int kw_splat
)
3113 return fiber_transfer_kw(fiber_ptr(self
), argc
, argv
, kw_splat
);
3118 * Fiber.yield(args, ...) -> obj
3120 * Yields control back to the context that resumed the fiber, passing
3121 * along any arguments that were passed to it. The fiber will resume
3122 * processing at this point when #resume is called next.
3123 * Any arguments passed to the next #resume will be the value that
3124 * this Fiber.yield expression evaluates to.
3127 rb_fiber_s_yield(int argc
, VALUE
*argv
, VALUE klass
)
3129 return rb_fiber_yield_kw(argc
, argv
, rb_keyword_given_p());
3133 fiber_raise(rb_fiber_t
*fiber
, VALUE exception
)
3135 if (fiber
== fiber_current()) {
3136 rb_exc_raise(exception
);
3138 else if (fiber
->resuming_fiber
) {
3139 return fiber_raise(fiber
->resuming_fiber
, exception
);
3141 else if (FIBER_SUSPENDED_P(fiber
) && !fiber
->yielding
) {
3142 return fiber_transfer_kw(fiber
, -1, &exception
, RB_NO_KEYWORDS
);
3145 return fiber_resume_kw(fiber
, -1, &exception
, RB_NO_KEYWORDS
);
3150 rb_fiber_raise(VALUE fiber
, int argc
, const VALUE
*argv
)
3152 VALUE exception
= rb_make_exception(argc
, argv
);
3154 return fiber_raise(fiber_ptr(fiber
), exception
);
3159 * fiber.raise -> obj
3160 * fiber.raise(string) -> obj
3161 * fiber.raise(exception [, string [, array]]) -> obj
3163 * Raises an exception in the fiber at the point at which the last
3164 * +Fiber.yield+ was called. If the fiber has not been started or has
3165 * already run to completion, raises +FiberError+. If the fiber is
3166 * yielding, it is resumed. If it is transferring, it is transferred into.
3167 * But if it is resuming, raises +FiberError+.
3169 * With no arguments, raises a +RuntimeError+. With a single +String+
3170 * argument, raises a +RuntimeError+ with the string as a message. Otherwise,
3171 * the first parameter should be the name of an +Exception+ class (or an
3172 * object that returns an +Exception+ object when sent an +exception+
3173 * message). The optional second parameter sets the message associated with
3174 * the exception, and the third parameter is an array of callback information.
3175 * Exceptions are caught by the +rescue+ clause of <code>begin...end</code>
3178 * Raises +FiberError+ if called on a Fiber belonging to another +Thread+.
3180 * See Kernel#raise for more information.
3183 rb_fiber_m_raise(int argc
, VALUE
*argv
, VALUE self
)
3185 return rb_fiber_raise(self
, argc
, argv
);
3192 * Terminates the fiber by raising an uncatchable exception.
3193 * It only terminates the given fiber and no other fiber, returning +nil+ to
3194 * another fiber if that fiber was calling #resume or #transfer.
3196 * <tt>Fiber#kill</tt> only interrupts another fiber when it is in Fiber.yield.
3197 * If called on the current fiber then it raises that exception at the <tt>Fiber#kill</tt> call site.
3199 * If the fiber has not been started, transition directly to the terminated state.
3201 * If the fiber is already terminated, does nothing.
3203 * Raises FiberError if called on a fiber belonging to another thread.
3206 rb_fiber_m_kill(VALUE self
)
3208 rb_fiber_t
*fiber
= fiber_ptr(self
);
3210 if (fiber
->killed
) return Qfalse
;
3213 if (fiber
->status
== FIBER_CREATED
) {
3214 fiber
->status
= FIBER_TERMINATED
;
3216 else if (fiber
->status
!= FIBER_TERMINATED
) {
3217 if (fiber_current() == fiber
) {
3218 fiber_check_killed(fiber
);
3221 fiber_raise(fiber_ptr(self
), Qnil
);
3230 * Fiber.current -> fiber
3232 * Returns the current fiber. If you are not running in the context of
3233 * a fiber this method will return the root fiber.
3236 rb_fiber_s_current(VALUE klass
)
3238 return rb_fiber_current();
3242 fiber_to_s(VALUE fiber_value
)
3244 const rb_fiber_t
*fiber
= fiber_ptr(fiber_value
);
3245 const rb_proc_t
*proc
;
3246 char status_info
[0x20];
3248 if (fiber
->resuming_fiber
) {
3249 snprintf(status_info
, 0x20, " (%s by resuming)", fiber_status_name(fiber
->status
));
3252 snprintf(status_info
, 0x20, " (%s)", fiber_status_name(fiber
->status
));
3255 if (!rb_obj_is_proc(fiber
->first_proc
)) {
3256 VALUE str
= rb_any_to_s(fiber_value
);
3257 strlcat(status_info
, ">", sizeof(status_info
));
3258 rb_str_set_len(str
, RSTRING_LEN(str
)-1);
3259 rb_str_cat_cstr(str
, status_info
);
3262 GetProcPtr(fiber
->first_proc
, proc
);
3263 return rb_block_to_s(fiber_value
, &proc
->block
, status_info
);
3266 #ifdef HAVE_WORKING_FORK
3268 rb_fiber_atfork(rb_thread_t
*th
)
3270 if (th
->root_fiber
) {
3271 if (&th
->root_fiber
->cont
.saved_ec
!= th
->ec
) {
3272 th
->root_fiber
= th
->ec
->fiber_ptr
;
3274 th
->root_fiber
->prev
= 0;
3279 #ifdef RB_EXPERIMENTAL_FIBER_POOL
3281 fiber_pool_free(void *ptr
)
3283 struct fiber_pool
* fiber_pool
= ptr
;
3284 RUBY_FREE_ENTER("fiber_pool");
3286 fiber_pool_allocation_free(fiber_pool
->allocations
);
3287 ruby_xfree(fiber_pool
);
3289 RUBY_FREE_LEAVE("fiber_pool");
3293 fiber_pool_memsize(const void *ptr
)
3295 const struct fiber_pool
* fiber_pool
= ptr
;
3296 size_t size
= sizeof(*fiber_pool
);
3298 size
+= fiber_pool
->count
* fiber_pool
->size
;
3303 static const rb_data_type_t FiberPoolDataType
= {
3305 {NULL
, fiber_pool_free
, fiber_pool_memsize
,},
3306 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3310 fiber_pool_alloc(VALUE klass
)
3312 struct fiber_pool
*fiber_pool
;
3314 return TypedData_Make_Struct(klass
, struct fiber_pool
, &FiberPoolDataType
, fiber_pool
);
3318 rb_fiber_pool_initialize(int argc
, VALUE
* argv
, VALUE self
)
3320 rb_thread_t
*th
= GET_THREAD();
3321 VALUE size
= Qnil
, count
= Qnil
, vm_stack_size
= Qnil
;
3322 struct fiber_pool
* fiber_pool
= NULL
;
3324 // Maybe these should be keyword arguments.
3325 rb_scan_args(argc
, argv
, "03", &size
, &count
, &vm_stack_size
);
3328 size
= SIZET2NUM(th
->vm
->default_params
.fiber_machine_stack_size
);
3332 count
= INT2NUM(128);
3335 if (NIL_P(vm_stack_size
)) {
3336 vm_stack_size
= SIZET2NUM(th
->vm
->default_params
.fiber_vm_stack_size
);
3339 TypedData_Get_Struct(self
, struct fiber_pool
, &FiberPoolDataType
, fiber_pool
);
3341 fiber_pool_initialize(fiber_pool
, NUM2SIZET(size
), NUM2SIZET(count
), NUM2SIZET(vm_stack_size
));
3348 * Document-class: FiberError
3350 * Raised when an invalid operation is attempted on a Fiber, in
3351 * particular when attempting to call/resume a dead fiber,
3352 * attempting to yield from the root fiber, or calling a fiber across
3355 * fiber = Fiber.new{}
3356 * fiber.resume #=> nil
3357 * fiber.resume #=> FiberError: dead fiber called
3363 rb_thread_t
*th
= GET_THREAD();
3364 size_t vm_stack_size
= th
->vm
->default_params
.fiber_vm_stack_size
;
3365 size_t machine_stack_size
= th
->vm
->default_params
.fiber_machine_stack_size
;
3366 size_t stack_size
= machine_stack_size
+ vm_stack_size
;
3370 GetSystemInfo(&info
);
3371 pagesize
= info
.dwPageSize
;
3372 #else /* not WIN32 */
3373 pagesize
= sysconf(_SC_PAGESIZE
);
3375 SET_MACHINE_STACK_END(&th
->ec
->machine
.stack_end
);
3377 fiber_pool_initialize(&shared_fiber_pool
, stack_size
, FIBER_POOL_INITIAL_SIZE
, vm_stack_size
);
3379 fiber_initialize_keywords
[0] = rb_intern_const("blocking");
3380 fiber_initialize_keywords
[1] = rb_intern_const("pool");
3381 fiber_initialize_keywords
[2] = rb_intern_const("storage");
3383 const char *fiber_shared_fiber_pool_free_stacks
= getenv("RUBY_SHARED_FIBER_POOL_FREE_STACKS");
3384 if (fiber_shared_fiber_pool_free_stacks
) {
3385 shared_fiber_pool
.free_stacks
= atoi(fiber_shared_fiber_pool_free_stacks
);
3387 if (shared_fiber_pool
.free_stacks
< 0) {
3388 rb_warn("Setting RUBY_SHARED_FIBER_POOL_FREE_STACKS to a negative value is not allowed.");
3389 shared_fiber_pool
.free_stacks
= 0;
3392 if (shared_fiber_pool
.free_stacks
> 1) {
3393 rb_warn("Setting RUBY_SHARED_FIBER_POOL_FREE_STACKS to a value greater than 1 is operating system specific, and may cause crashes.");
3397 rb_cFiber
= rb_define_class("Fiber", rb_cObject
);
3398 rb_define_alloc_func(rb_cFiber
, fiber_alloc
);
3399 rb_eFiberError
= rb_define_class("FiberError", rb_eStandardError
);
3400 rb_define_singleton_method(rb_cFiber
, "yield", rb_fiber_s_yield
, -1);
3401 rb_define_singleton_method(rb_cFiber
, "current", rb_fiber_s_current
, 0);
3402 rb_define_singleton_method(rb_cFiber
, "blocking", rb_fiber_blocking
, 0);
3403 rb_define_singleton_method(rb_cFiber
, "[]", rb_fiber_storage_aref
, 1);
3404 rb_define_singleton_method(rb_cFiber
, "[]=", rb_fiber_storage_aset
, 2);
3406 rb_define_method(rb_cFiber
, "initialize", rb_fiber_initialize
, -1);
3407 rb_define_method(rb_cFiber
, "blocking?", rb_fiber_blocking_p
, 0);
3408 rb_define_method(rb_cFiber
, "storage", rb_fiber_storage_get
, 0);
3409 rb_define_method(rb_cFiber
, "storage=", rb_fiber_storage_set
, 1);
3410 rb_define_method(rb_cFiber
, "resume", rb_fiber_m_resume
, -1);
3411 rb_define_method(rb_cFiber
, "raise", rb_fiber_m_raise
, -1);
3412 rb_define_method(rb_cFiber
, "kill", rb_fiber_m_kill
, 0);
3413 rb_define_method(rb_cFiber
, "backtrace", rb_fiber_backtrace
, -1);
3414 rb_define_method(rb_cFiber
, "backtrace_locations", rb_fiber_backtrace_locations
, -1);
3415 rb_define_method(rb_cFiber
, "to_s", fiber_to_s
, 0);
3416 rb_define_alias(rb_cFiber
, "inspect", "to_s");
3417 rb_define_method(rb_cFiber
, "transfer", rb_fiber_m_transfer
, -1);
3418 rb_define_method(rb_cFiber
, "alive?", rb_fiber_alive_p
, 0);
3420 rb_define_singleton_method(rb_cFiber
, "blocking?", rb_fiber_s_blocking_p
, 0);
3421 rb_define_singleton_method(rb_cFiber
, "scheduler", rb_fiber_s_scheduler
, 0);
3422 rb_define_singleton_method(rb_cFiber
, "set_scheduler", rb_fiber_set_scheduler
, 1);
3423 rb_define_singleton_method(rb_cFiber
, "current_scheduler", rb_fiber_current_scheduler
, 0);
3425 rb_define_singleton_method(rb_cFiber
, "schedule", rb_fiber_s_schedule
, -1);
3427 #ifdef RB_EXPERIMENTAL_FIBER_POOL
3428 rb_cFiberPool
= rb_define_class_under(rb_cFiber
, "Pool", rb_cObject
);
3429 rb_define_alloc_func(rb_cFiberPool
, fiber_pool_alloc
);
3430 rb_define_method(rb_cFiberPool
, "initialize", rb_fiber_pool_initialize
, -1);
3433 rb_provide("fiber.so");
3436 RUBY_SYMBOL_EXPORT_BEGIN
3439 ruby_Init_Continuation_body(void)
3441 rb_cContinuation
= rb_define_class("Continuation", rb_cObject
);
3442 rb_undef_alloc_func(rb_cContinuation
);
3443 rb_undef_method(CLASS_OF(rb_cContinuation
), "new");
3444 rb_define_method(rb_cContinuation
, "call", rb_cont_call
, -1);
3445 rb_define_method(rb_cContinuation
, "[]", rb_cont_call
, -1);
3446 rb_define_global_function("callcc", rb_callcc
, 0);
3449 RUBY_SYMBOL_EXPORT_END