Commit Graph

17781 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto f4dcc3dc3d mruby-io: refactor popen to use HAL functions
eliminates platform-specific popen implementations by using
mrb_io_hal_pipe and mrb_io_hal_spawn_process. removes io_cloexec_pipe,
io_pipe, and io_process_exec functions. io.pipe now also uses
mrb_io_hal_pipe. reduces platform conditionals and improves portability.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-15 14:17:27 +09:00
Yukihiro "Matz" Matsumoto 74ca22f281 mruby-io: introduce HAL for platform abstraction
separates platform-specific code into hal-posix-io and hal-win-io gems,
making mruby-io platform-independent. HAL interface defined in
mrbgems/mruby-io/include/io_hal.h covers file operations, I/O operations,
and process operations. follows mruby-task dependency pattern where HAL
gems depend on feature gem. ws2_32 library linked in hal-win-io gem.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-15 11:53:38 +09:00
Yukihiro "Matz" Matsumoto 87c8889726 mruby-task: move platform-specific sleep to hal
task.c used clock_gettime() directly, breaking portability. added
mrb_task_hal_sleep_us() to hal interface.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-15 08:00:34 +09:00
Yukihiro "Matz" Matsumoto bc10fbd49e mruby-compiler: fix memory leak in gen_literal_array
segment nodes allocated with mrbc_malloc were leaked if gen_string
raised an exception via longjmp. fix by avoiding allocation entirely:
temporarily modify tree structure by saving and clearing cdr pointer,
call gen_string, then restore cdr. no memory is allocated so nothing
leaks even on longjmp.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-15 07:50:51 +09:00
Yukihiro "Matz" Matsumoto 6ff5c7bfa9 mruby-task: rename mrb_tasks_run to mrb_task_run
follows mrb_{gem_name}_{operation} naming convention consistently
with other hal functions like mrb_task_hal_init. the plural form was
semantically correct but inconsistent with gem naming patterns.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-14 23:11:10 +09:00
Yukihiro "Matz" Matsumoto 4fa51c8ea0 task_hal.h: remove non-HAL function declarations
removes mrb_tasks_run and mrb_task_mark_all from task_hal.h as these
are core scheduler functions, not HAL interface functions. only
mrb_tick remains as it must be called by HAL timer callbacks.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-14 23:10:34 +09:00
Yukihiro "Matz" Matsumoto 9c100ab844 mruby-task: introduce HAL (hardware abstraction layer) for platform support
separates platform-specific timer and interrupt code into hal-posix-task
and hal-win-task gems. mruby-task now uses HAL interface defined in
task_hal.h, making it easier to port to new platforms.

hal-posix-task: uses sigalrm/setitimer for timer, sigprocmask for irq
protection, and SA_RESTART flag to prevent EINTR on system calls.

hal-win-task: uses multimedia timer API and critical_section for irq
protection.

both HALs support multiple mrb_state instances with single shared timer.
auto-detection loads appropriate HAL based on platform.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-14 09:14:59 +09:00
Yukihiro "Matz" Matsumoto 4181a42cd2 mruby-task: remove unnecessary mruby-fiber dependency
mruby-task uses mrb_context and mrb_fiber_state enum, but these are
part of core mruby, not the mruby-fiber gem. the dependency was not
needed.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-14 09:14:55 +09:00
Yukihiro "Matz" Matsumoto 4c1b2a59bd mruby-class-ext: fix crash in module comparison with invalid types
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-14 08:22:41 +09:00
Yukihiro "Matz" Matsumoto 4f52868923 mruby-bigint: clean up preprocessor directives in mpz_clear
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-13 11:30:15 +09:00
Yukihiro "Matz" Matsumoto f69fe329a9 mruby-bigint: fix mrb_bint_copy to properly clone bigints
mrb_bint_copy was creating reference to destination then destroying it
with mpz_init, causing copy to happen in orphaned memory. this made
clone return 0 instead of copying the bigint value.

fix extracts common mpz_t-to-rbigint transfer logic into bint_set
helper, used by both bint_new and mrb_bint_copy. eliminates code
duplication and properly copies source data to destination rbigint
structure, handling both embedded and heap storage cases.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-13 09:19:29 +09:00
Yukihiro "Matz" Matsumoto 003bdf5031 mruby-bigint: fix null pointer dereference in xor fast path
when xoring bigint with small integer, the fast path assumes source
bigint has allocated limbs. malformed bigints with sn > 0 but sz == 0
caused null pointer access. add defensive check to allocate storage
before accessing c.p[0].

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-13 09:09:47 +09:00
Yukihiro "Matz" Matsumoto f4d6e67656 throw.h: exclude arm64 from mingw64 builtin setjmp/longjmp; fix #6637
__builtin_setjmp/longjmp are x86/x86_64 specific gcc intrinsics
and not supported on arm64. windows arm64 with msys2 clangarm64
now correctly falls through to standard setjmp/longjmp.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-13 08:41:34 +09:00
Yukihiro "Matz" Matsumoto c04fdf862b mruby-strftime: add readme documentation
add comprehensive readme covering usage, format specifiers,
features, and implementation details.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-12 23:01:28 +09:00
Yukihiro "Matz" Matsumoto b31e22f0bc mruby-strftime: implement time#strftime method
add new mruby-strftime gem providing time#strftime for formatting
time objects using standard format specifiers.

implementation features:
- uses mrb_time_get_tm() api for accessing time components
- handles nul bytes in format strings correctly
- dynamic buffer allocation for variable-length output
- comprehensive test coverage including edge cases

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-12 20:05:08 +09:00
Yukihiro "Matz" Matsumoto daaaafeff8 mruby-time: add mrb_time_get_tm() API for accessing struct tm
add public api function to retrieve struct tm from time object.
this enables other gems to access time components for formatting
while maintaining encapsulation of internal mrb_time structure.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-12 09:33:49 +09:00
Yukihiro "Matz" Matsumoto 572c674c2b mruby-task: fix memory leak in task context cleanup; ref #6641
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 23:01:33 +09:00
Yukihiro "Matz" Matsumoto 48e7592801 mruby-bigint: fix clang warning for struct initialization
add extra braces for nested struct initialization to satisfy
clang's -Wmissing-braces warning.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 13:59:54 +09:00
Yukihiro "Matz" Matsumoto 99d4629d59 mruby-pack: add explicit casts to fix msvc warnings
added explicit (int) casts when passing mrb_int count to pack/unpack
functions that expect int parameters. fixes C4244 warnings on windows
msvc builds where mrb_int is 64-bit but int is 32-bit.

count is validated to not exceed INT_MAX by read_tmpl, making these
casts safe.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 12:47:54 +09:00
Yukihiro "Matz" Matsumoto d1a48c03e2 gc.c: add mrbc stub for mrb_task_mark_all
added forward declaration in gc.c and stub implementation in mrbc stub.c
for mrb_task_mark_all to avoid link errors when mrbc is built without
mruby-task gem.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 12:41:39 +09:00
Yukihiro "Matz" Matsumoto 7469b79aa8 mruby-task: link with winmm library on windows
windows multimedia timer api requires linking with winmm.lib. added
conditional linker library using spec.for_windows? to match mruby
build system conventions.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 12:23:33 +09:00
Yukihiro "Matz" Matsumoto 34bf0c1fd2 mruby-task: add macos and windows hal support
extended posix platform detection to include macos via __APPLE__ and
__MACH__ defines. implemented full windows hal using multimedia timer
(timeSetEvent) and CRITICAL_SECTION for thread synchronization. added
task_count_update stub for unsupported platforms with clear warnings.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 11:52:58 +09:00
Yukihiro "Matz" Matsumoto 14b761e200 vm.c: suppress GCC 12+ dangling pointer warning for jmpbuf
add pragma to suppress -Wdangling-pointer warning for intentional
stack variable address storage in exception handling. the pointer
is safely managed and cleared before function returns.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 11:10:16 +09:00
Yukihiro "Matz" Matsumoto 4499daf88e mruby.h: simplify task state definitions using fiber state aliases
remove MRB_TASK_CREATED and MRB_TASK_STOPPED from mrb_fiber_state enum
and define them as aliases to MRB_FIBER_CREATED and MRB_FIBER_TERMINATED.

this makes the relationship between tasks and fibers clearer and avoids
artificially extending the enum with semantically equivalent values.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 10:47:40 +09:00
Yukihiro "Matz" Matsumoto 872c3bcea4 mruby-task: remove unnecessary ifdef guards
remove MRB_USE_TASK_SCHEDULER ifdef guards from task.h and task.c
since the macro is always defined when compiling this gem

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 10:34:10 +09:00
Yukihiro "Matz" Matsumoto 8d61b67dc1 mruby-task: fix c++ compatibility warnings
- remove redundant MRB_TASK_CREATED/STOPPED macros from task.h since
  they are now properly defined in mrb_fiber_state enum in mruby.h
- declare kw_names array separately to avoid taking address of
  temporary array in c++ compilation

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 10:29:41 +09:00
Yukihiro "Matz" Matsumoto 76d6c1b16e mruby-task: remove unused task_count variable
remove unused task_count variable in mrb_task_mark_all to fix compiler
warning.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 09:02:44 +09:00
Yukihiro "Matz" Matsumoto b50dbfe868 mruby-task: update readme API documentation
clarify that task.new name parameter must be string, document
task#name returns "(noname)" for unnamed tasks, and provide full
structure of task.stat return value.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 08:57:11 +09:00
Yukihiro "Matz" Matsumoto eb13ffdc9d mruby-task: add comprehensive test suite
add tests for sleep/usleep validation, task creation, status/inspect
methods, control methods, task.stat, priority handling, and name
handling.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 08:44:11 +09:00
Yukihiro "Matz" Matsumoto dd06920640 mruby-task: fix validation bugs in task creation
fix uninitialized kwargs array causing crashes, add type validation for
name (must be String) and priority (must be Integer) parameters, return
"(noname)" for unnamed tasks.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 08:43:16 +09:00
Yukihiro "Matz" Matsumoto 2a124a704f mruby-task: add comprehensive examples demonstrating task features
added six new examples:
- simple.rb: basic task creation and execution
- priority.rb: priority-based scheduling
- suspend_resume.rb: manual task control
- inspection.rb: task status and inspection methods
- statistics.rb: scheduler monitoring with Task.stat
- producer_consumer.rb: task coordination pattern

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-10 17:44:31 +09:00
Yukihiro "Matz" Matsumoto 2d713c647e mruby-task: implement task.stat method
replaced stub with full implementation that returns a hash containing
scheduler statistics:

- tick: current tick counter
- wakeup_tick: next scheduled wakeup time
- dormant/ready/waiting/suspended: per-queue statistics

each queue stat includes:
- count: number of tasks in queue
- tasks: array of task objects in that queue

implements helper function mrb_stat_sub() to walk queues and collect
task information. uses irq disable/enable to ensure consistent snapshot.

returns hash directly as requested, not wrapped in stat object.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-10 17:30:06 +09:00
Yukihiro "Matz" Matsumoto 33e5af432b mruby-task: implement task#inspect method
added inspect method that returns formatted string showing:
- task pointer address
- task name (string/symbol), or "(unnamed)" for nil/other types
- task status (RUNNING, READY, WAITING, SUSPENDED, DORMANT, UNKNOWN)

format matches original implementation: #<Task:0x12345678 name:STATUS>

avoids mrb_funcall during inspection to prevent vm state issues.
handles string and symbol names directly, treats other types as unnamed.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-10 17:06:22 +09:00
Yukihiro "Matz" Matsumoto 6d640b77d6 mruby-task: implement task#status method
replaced stub implementation with proper status reporting that returns
symbols representing task state:
- :RUNNING for executing tasks
- :READY for tasks ready to execute
- :WAITING for tasks waiting (sleeping, blocked, etc.)
- :SUSPENDED for manually suspended tasks
- :DORMANT for terminated tasks
- :UNKNOWN for invalid states

implementation matches original mruby-task design using ternary operators
and MRB_SYM() macros for efficient symbol lookup.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-10 16:43:57 +09:00
Yukihiro "Matz" Matsumoto 77b6febc19 mruby-task: improve sleep implementation with efficiency and safety
improved sleep_us_impl() in several ways:

1. dynamic sleep intervals: now sleeps for actual remaining time instead
   of fixed 1ms polling, reducing unnecessary wakeups and improving
   efficiency for longer sleeps

2. error handling: added checks for clock_gettime() failures with fallback
   to usleep(), and input validation to handle negative values

3. overflow prevention: use named constant USEC_PER_MSEC instead of
   literal 1000 for microsecond-to-nanosecond conversion, and validate
   input before conversion

4. wraparound handling: fixed tick comparison at line 580 to use signed
   arithmetic like other tick comparisons in the codebase

5. code clarity: added time conversion constants (NSEC_PER_MSEC,
   NSEC_PER_SEC, USEC_PER_MSEC) to replace magic numbers

all tests pass.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-10 16:30:50 +09:00
Yukihiro "Matz" Matsumoto 0ea429e29e mruby-task: fix sleep from root context to use real wall-clock time
when sleep is called from root context (not within a task), it was
instantly advancing the simulated tick counter instead of actually
delaying. this caused task_pass.rb example to run tasks 0-5 instantly
without proper delays between iterations.

fixed by using clock_gettime() to track elapsed real time and sleeping
in 1ms intervals. also clear switching_ flag when returning from root
context sleep to prevent unwanted context switches.

removed find_earliest_wakeup_tick() function and time-advancing logic
from task_run_one_iteration() as real delays are now handled by sleep
itself.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-10 12:23:36 +09:00
Yukihiro "Matz" Matsumoto 1d90c19a36 mruby-task: rename constants for improved readability
Renamed constants to use more descriptive underscores:
- MRB_TASKSTATUS_* -> MRB_TASK_STATUS_*
- MRB_TASKREASON_* -> MRB_TASK_REASON_*

This improves code readability by making the constant names clearer.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-09 16:57:08 +09:00
Yukihiro "Matz" Matsumoto fd07c36feb mruby-task: refactor to eliminate code duplication
Eliminated approximately 160 lines of duplicated code (~10% of file) by
extracting common patterns into reusable helpers. This improves
maintainability by consolidating task execution logic, validation
patterns, and state transitions into single locations.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-09 16:54:37 +09:00
Yukihiro "Matz" Matsumoto 745e577b0e mruby-task: treat root context as main task
Implement main task wrapper following Fiber's pattern, where root context
is represented by a special task object. This matches PicoRuby behavior
where Task.current always returns a task object, even from root context.

The main task is lazy-allocated on first Task.current call from root,
stored in mrb->task.main_task, and has name "main", status RUNNING,
priority 0. It wraps the root context without allocating a separate
execution context.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-09 16:22:25 +09:00
Yukihiro "Matz" Matsumoto 6461af9cca mruby-task: support Task.pass from root context
Enable Task.pass to work from root context by implementing mini-scheduler
iteration. When called from root context, Task.pass now runs one task
iteration, allowing cooperative multitasking without Task.run. This matches
PicoRuby behavior.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-09 12:52:14 +09:00
Yukihiro "Matz" Matsumoto 61f99385ba mruby-task: fix crash by setting vmexec flag during task execution
Fix task termination crash caused by fiber_terminate freeing task
context resources. When a task completes, the VM would call
fiber_terminate which frees cibase/stbase, then next resume attempt
crashes dereferencing NULL pointers.

Solution unifies task and fiber lifecycle management:
- Set vmexec flag before calling mrb_vm_exec to prevent fiber_terminate
  from being called during normal task completion
- Save proc/pc to local variables to avoid CI_PROC_SET macro corruption
- Add termination check in mrb_task_free to prevent double-free

Tasks now follow the same execution pattern as Fiber, leveraging
VM's built-in context management.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-09 10:58:23 +09:00
Yukihiro "Matz" Matsumoto 8eb275b665 mruby-task: simplify stack marking using mrb_gc_mark_value
Replace manual mrb_immediate_p check with mrb_gc_mark_value macro
which already includes the immediate check internally.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-09 07:49:51 +09:00
Yukihiro "Matz" Matsumoto e7cbd8cc28 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>
2025-10-08 23:55:23 +09:00
Yukihiro "Matz" Matsumoto e49f42570c mruby-task: optimize timer control for single-task workloads
dynamically enable/disable timer interrupts based on scheduler state.
timer disabled when only one runnable task exists.
timer enabled when multiple tasks need preemption or sleeping tasks need wakeup.

use counter arrays to track ready/waiting tasks per vm.
separate platform-specific timer control from generic decision logic.
update counters at all task state transitions.

eliminates 250 interrupts/second in single-task workloads.
improves cpu efficiency and power consumption.
simplifies porting to new platforms.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-08 23:55:23 +09:00
Yukihiro "Matz" Matsumoto cba7ecd65f mruby-cmath: use MRB_SYM() and mrb_define_module_function_id()
unified declaration and initialization of cmath variable.
used mrb_define_module_id for module definition.
optimized all 18 function definitions with symbol id api.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-08 23:55:23 +09:00
Yukihiro "Matz" Matsumoto e1f7a40d5a mruby-math: use mrb_define_module_id instead of mrb_define_module
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-08 23:55:23 +09:00
Yukihiro "Matz" Matsumoto 9736e37a31 mruby-math: rename mrb_math variable to math
unify declaration with initialization for cleaner code.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-08 23:55:23 +09:00
Yukihiro "Matz" Matsumoto 3a395b9e0a mruby-math: use MRB_SYM() and mrb_define_module_function_id()
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-08 23:55:22 +09:00
Yukihiro "Matz" Matsumoto 7de526b075 mruby-sprintf: use MRB_SYM() and mrb_define_module_function_id()
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-08 23:55:22 +09:00
Yukihiro "Matz" Matsumoto b416d29010 mruby-sleep,mruby-task: define sleep methods as module functions
change sleep, usleep, sleep_ms from private methods to module functions
to match cruby behavior where sleep can be called as both bare sleep and
kernel.sleep.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-08 23:55:22 +09:00