15 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto d1bffb902e Merge pull request #6834 from Asmod4n/glib_hal
mruby-task GLib HAL
2026-05-17 23:31:33 +09:00
Asmod4n 17858cc5bd mruby-task GLib HAL
Lets mruby-task embed cleanly in any GLib-based event loop -- GTK,
libsoup, GStreamer, or anything else built on GMainContext. Tasks
become regular GSources, so the scheduler runs alongside whatever
else is on the loop without polling or busy-waiting.

Sleeping tasks cost zero CPU: the HAL parks until the next wakeup
deadline rather than ticking on a fixed cadence. Multiple mrb_states
on the same thread share one dispatcher and one ticker. Preemption,
Task.run, sleeper wakes, and foreign-loop integration all use the
same primitives, so embedders can mix Task.run with g_main_loop_run
freely.

ref mruby#6825
2026-05-17 13:44:20 +02:00
Yukihiro "Matz" Matsumoto e7f5387f5d gem.rb: detect external HAL by hal-<short>-* naming
67f137e201 introduced spec.hal_pattern on the target gem to
declare its expected HAL provider names.  But the decision to
override is naturally an opt-in choice belonging to the external
HAL gem (or build_config), not the target.  Drop hal_pattern and
hardcode the naming rule in resolve_external_hal!: a gem named
`hal-<short>-<conf>` overrides the gem whose last `-`-separated
segment is <short>.  No spec attribute on the target gem; no flag
on add_dependency; the convention does all the work.

The pre-be6413f0d8 convention was `hal-<platform>-<gem>` (e.g.,
hal-posix-task) for platform sort across gems.  Now that the
common platforms live under ports/, sorting by which target gem
a HAL is for is more useful, so the order is reversed:
`hal-<gem>-<conf>` (e.g., hal-task-glib).

ref #6825

Co-authored-by: Claude <noreply@anthropic.com>
2026-05-17 18:38:38 +09:00
Yukihiro "Matz" Matsumoto 67f137e201 gem.rb: hal_pattern for external HAL provider override
A gem can declare

  spec.hal_pattern = /\Ahal-.*-task\z/

to indicate that another gem whose name matches the pattern (and
which depends on this gem for headers) replaces the built-in
ports/<conf.ports>/ HAL implementation.  After all gems are set
up, List#resolve_external_hal! drops the target's ports/* objs
from its object list so the matching gem supplies the HAL
symbols.  Two or more matches is reported as a build error.

This restores the pre-be6413f0d8 ability to maintain an
out-of-tree HAL via add_dependency + naming convention, without
reintroducing the "HAL information scattered across gems"
problem: the parent gem still owns the scheduler, headers, and
bundled posix/win ports; external HAL gems are an explicit,
opt-in override.

Declare /\Ahal-.*-task\z/ for mruby-task -- the same naming
pattern used before be6413f0d8.

ref #6825

Co-authored-by: Claude <noreply@anthropic.com>
2026-05-17 17:33:26 +09:00
Yukihiro "Matz" Matsumoto 2ff6563484 mruby-task: restore Windows link library dropped during HAL migration
Commit be6413f0d86a ("mruby-task: migrate HAL to ports/ directories")
moved the Windows HAL source into mruby-task/ports/win/ but dropped
the linker.libraries declaration that previously lived in
hal-win-task/mrbgem.rake. Both mingw and MSVC builds now fail to link
task_hal.o/obj with undefined references to timeBeginPeriod,
timeEndPeriod, timeSetEvent, and timeKillEvent. Re-declare winmm
under a for_windows? guard.

Co-authored-by: Claude <noreply@anthropic.com>
2026-04-23 23:16:21 +09:00
Yukihiro "Matz" Matsumoto be6413f0d8 mruby-task: migrate HAL to ports/ directories
Move hal-posix-task and hal-win-task into
mruby-task/ports/posix/ and mruby-task/ports/win/.
Remove HAL auto-detection logic from mrbgem.rake. Update
cosmopolitan.rb to use conf.ports :posix instead of explicit
hal-* gem references.

Co-authored-by: Claude <noreply@anthropic.com>
2026-04-23 19:25:37 +09:00
Yukihiro "Matz" Matsumoto ad51bf848b mrbgem.rake: simplify hal selection logic
remove redundant visualcpp and mingw checks since for_windows? already
detects all windows builds including visual c++ and mingw.

ref #6653

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 11:11:27 +09:00
dearblue ea215bc19c Fixed HAL auto-detection order
Because MinGW was not recognized as Windows during cross-builds.
2025-10-25 21:06:16 +09:00
Yukihiro "Matz" Matsumoto e540547413 mruby-task: use hal-win-task for mingw builds
mingw provides posix compatibility for file i/o but not for signal
handling. hal-posix-task relies on SIGALRM, setitimer(), and
sigprocmask() which are not available on windows even through mingw.

changed hal selection for mruby-task to use hal-win-task for mingw,
while mruby-dir, mruby-io, and mruby-socket correctly use posix hals
for mingw since those features are supported.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 19:00:06 +09:00
Yukihiro "Matz" Matsumoto feca90ceab hal: fix selection to use toolchain instead of RUBY_PLATFORM
when building with MSVC on Windows, RUBY_PLATFORM (from the Ruby
installation running rake) may indicate "mingw" if Ruby was installed
via RubyInstaller, causing incorrect selection of POSIX HALs instead
of Windows HALs.

fixed by checking spec.build.primary_toolchain first:
- if toolchain is "visualcpp", select Windows HALs
- otherwise fall through to existing platform checks

this ensures MSVC builds use hal-win-* gems even when Ruby itself
was installed with MinGW.

affected gems:
- mruby-dir
- mruby-io
- mruby-socket
- mruby-task

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 18:20:32 +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 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 47dfb771cf mruby-task: fix cooperative task yielding with Task.pass
this patch fixes several critical issues in the task scheduler:

1. vm integration for computed goto dispatch mode:
   - added task switching check in NEXT macro for computed goto
   - previous implementation only worked with switch dispatch mode
   - now Task.pass properly yields control to other tasks

2. task lifecycle tracking:
   - added 'started' flag to mrb_task structure
   - fixed first-run detection to avoid popping callinfo multiple times
   - vm overwrites context status during execution, making it unreliable

3. removed mrblib/task.rb:
   - empty Ruby method stubs were overriding C implementations
   - all task methods now properly implemented in C

4. cleaned up task scheduler loop:
   - proper task completion detection using switching flag
   - round-robin scheduling for tasks at same priority
   - clean scheduler exit when all tasks complete

tasks now cooperatively yield with Task.pass and complete cleanly.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-08 23:55:21 +09:00
Yukihiro "Matz" Matsumoto ae0d7a0c16 mruby-task: add initial gem structure with api skeleton
create mruby-task gem directory structure with:
- mrbgem.rake: gem specification with task scheduler define
- include/task.h: tcb structure and core scheduler declarations
- src/task.c: implementation skeleton with empty method stubs
- mrblib/task.rb: ruby api documentation and task::stat class

all methods have empty bodies ready for implementation.

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