mruby-task: add gc protection and optimize task operations

Implements dual-mechanism GC protection and optimizes task lookup
using pointer arithmetic based on PicoRuby reference implementation.

GC Protection:
- Add mrb_gc_register/unregister to protect Task objects
- Implement mrb_task_mark_all() to mark task contexts during GC
- Store proc reference in mrb_task to prevent premature collection
- Integrate marking into gc.c root_scan_phase

Performance Optimizations:
- Add MRB2TASK macro for O(1) context-to-task conversion
- Optimize Task.current: O(n) queue search -> O(1) pointer arithmetic
- Optimize Task.pass: simplify to root context check
- Optimize Task.join: use MRB2TASK for current task lookup

Bug Fixes:
- Fix MRB_TASK_CREATED/STOPPED to use MRB_FIBER_TERMINATED
- Add safety check to prevent execution of terminated tasks
- Initialize callinfo PC to bytecode start in task_init_context

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-10-08 15:03:06 +09:00
parent e49f42570c
commit e7cbd8cc28
4 changed files with 117 additions and 14 deletions
+10
View File
@@ -28,6 +28,11 @@
#include <stdlib.h>
#endif
#ifdef MRB_USE_TASK_SCHEDULER
/* Weak stub for mrb_task_mark_all - actual implementation in task.c */
void __attribute__((weak)) mrb_task_mark_all(mrb_state *mrb) { (void)mrb; }
#endif
/*
= Tri-color Incremental Garbage Collection
@@ -956,6 +961,11 @@ root_scan_phase(mrb_state *mrb, mrb_gc *gc)
if (mrb->root_c != mrb->c) {
mark_context(mrb, mrb->root_c);
}
#ifdef MRB_USE_TASK_SCHEDULER
/* mark tasks - calls into task.c to mark all task queues */
mrb_task_mark_all(mrb);
#endif
}
static void