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