From be6413f0d84b919c68fab0ddc34aeff0da0c734e Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 28 Mar 2026 07:25:14 +0900 Subject: [PATCH] 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 --- build_config/cosmopolitan.rb | 6 +- mrbgems/hal-posix-task/README.md | 102 ---------------- mrbgems/hal-posix-task/mrbgem.rake | 8 -- mrbgems/hal-win-task/README.md | 109 ------------------ mrbgems/hal-win-task/mrbgem.rake | 11 -- mrbgems/mruby-task/mrbgem.rake | 28 ----- .../src => mruby-task/ports/posix}/task_hal.c | 18 --- .../src => mruby-task/ports/win}/task_hal.c | 18 --- 8 files changed, 2 insertions(+), 298 deletions(-) delete mode 100644 mrbgems/hal-posix-task/README.md delete mode 100644 mrbgems/hal-posix-task/mrbgem.rake delete mode 100644 mrbgems/hal-win-task/README.md delete mode 100644 mrbgems/hal-win-task/mrbgem.rake rename mrbgems/{hal-posix-task/src => mruby-task/ports/posix}/task_hal.c (94%) rename mrbgems/{hal-win-task/src => mruby-task/ports/win}/task_hal.c (91%) diff --git a/build_config/cosmopolitan.rb b/build_config/cosmopolitan.rb index 756b4d15c..c7f46b432 100644 --- a/build_config/cosmopolitan.rb +++ b/build_config/cosmopolitan.rb @@ -64,10 +64,8 @@ MRuby::Build.new do |conf| # APE binaries use .com extension conf.exts.executable = '.com' - # Cosmopolitan provides POSIX compatibility, explicitly select POSIX HALs - conf.gem core: 'hal-posix-io' - conf.gem core: 'hal-posix-socket' - conf.gem core: 'hal-posix-dir' + # Cosmopolitan provides POSIX compatibility + conf.ports :posix # Standard library conf.gembox 'stdlib' diff --git a/mrbgems/hal-posix-task/README.md b/mrbgems/hal-posix-task/README.md deleted file mode 100644 index 157ebdba2..000000000 --- a/mrbgems/hal-posix-task/README.md +++ /dev/null @@ -1,102 +0,0 @@ -# hal-posix-task - -POSIX Hardware Abstraction Layer (HAL) implementation for mruby-task. - -## Description - -Provides timer and interrupt support for the mruby-task cooperative scheduler on POSIX-compliant platforms. Uses `SIGALRM` and `setitimer()` for periodic timer ticks, and `sigprocmask()` for interrupt protection. - -## Supported Platforms - -- Linux -- macOS -- BSD (FreeBSD, OpenBSD, NetBSD) -- Other POSIX-compliant Unix systems - -## Requirements - -- POSIX-compliant operating system -- Signal support (`SIGALRM`, `sigaction`, `sigprocmask`) -- Timer support (`setitimer`, `ITIMER_REAL`) - -## Usage - -### Explicit HAL Selection (Recommended) - -```ruby -MRuby::Build.new do |conf| - # ... other configuration ... - - # Specify POSIX HAL - automatically brings in mruby-task - conf.gem core: 'hal-posix-task' -end -``` - -### Auto-detection (Development) - -```ruby -MRuby::Build.new do |conf| - # ... other configuration ... - - # Auto-detects and selects hal-posix-task on POSIX platforms - conf.gem core: 'mruby-task' -end -``` - -## Implementation Details - -### Timer Mechanism - -- Uses `setitimer(ITIMER_REAL, ...)` to generate periodic `SIGALRM` signals -- Timer interval configured by `MRB_TICK_UNIT` (default: 4ms) -- Signal handler calls `mrb_tick()` for all registered VM instances - -### Interrupt Protection - -- Critical sections protected using `sigprocmask()` to block `SIGALRM` -- Prevents race conditions during task queue modifications -- Supports nested critical sections through signal masking - -### Multi-VM Support - -- Supports up to `MRB_TASK_MAX_VMS` concurrent mruby VM instances (default: 8) -- Single shared timer ticks all registered VMs -- Per-VM task counters optimize timer usage (timer disabled when idle) - -### Timer Optimization - -The implementation dynamically enables/disables the timer based on task state: - -- **Timer enabled** when: Multiple ready tasks OR any waiting tasks exist -- **Timer disabled** when: Single task or all tasks dormant/suspended -- Reduces CPU usage and power consumption when scheduler is idle - -## Configuration - -Override these macros in your build config if needed: - -```ruby -conf.gem core: 'hal-posix-task' do |spec| - # Custom tick interval (10ms instead of default 4ms) - spec.build.defines << 'MRB_TICK_UNIT=10' - - # Custom timeslice (5 ticks instead of default 3) - spec.build.defines << 'MRB_TIMESLICE_TICK_COUNT=5' - - # More concurrent VMs (16 instead of default 8) - spec.build.defines << 'MRB_TASK_MAX_VMS=16' -end -``` - -## Known Limitations - -- `SIGALRM` conflicts with other code using the same signal -- Timer resolution limited by platform (typically 1-10ms) -- Signal delivery may be delayed under heavy system load -- Not suitable for hard real-time requirements - -## See Also - -- `mruby-task` - Core task scheduler -- `hal-win-task` - Windows HAL implementation -- Task scheduler documentation: `mrbgems/mruby-task/README.md` diff --git a/mrbgems/hal-posix-task/mrbgem.rake b/mrbgems/hal-posix-task/mrbgem.rake deleted file mode 100644 index 570161c77..000000000 --- a/mrbgems/hal-posix-task/mrbgem.rake +++ /dev/null @@ -1,8 +0,0 @@ -MRuby::Gem::Specification.new('hal-posix-task') do |spec| - spec.license = 'MIT' - spec.authors = 'mruby developers' - spec.summary = 'POSIX HAL for mruby-task (Linux, macOS, BSD, Unix)' - - # HAL gem depends on feature gem - brings in mruby-task automatically - spec.add_dependency 'mruby-task', core: 'mruby-task' -end diff --git a/mrbgems/hal-win-task/README.md b/mrbgems/hal-win-task/README.md deleted file mode 100644 index b959afdec..000000000 --- a/mrbgems/hal-win-task/README.md +++ /dev/null @@ -1,109 +0,0 @@ -# hal-win-task - -Windows Hardware Abstraction Layer (HAL) implementation for mruby-task. - -## Description - -Provides timer and interrupt support for the mruby-task cooperative scheduler on Windows platforms. Uses multimedia timer (`timeSetEvent`/`timeKillEvent`) for periodic timer ticks, and `CRITICAL_SECTION` for interrupt protection. - -## Supported Platforms - -- Windows 7 and later -- Windows Server 2008 R2 and later -- All versions with multimedia timer support - -## Requirements - -- Windows operating system -- Multimedia timer API (`winmm.lib`) -- Visual C++, MinGW, or compatible compiler - -## Usage - -### Explicit HAL Selection (Recommended) - -```ruby -MRuby::Build.new do |conf| - # ... other configuration ... - - # Specify Windows HAL - automatically brings in mruby-task - conf.gem core: 'hal-win-task' -end -``` - -### Auto-detection (Development) - -```ruby -MRuby::Build.new do |conf| - # ... other configuration ... - - # Auto-detects and selects hal-win-task on Windows platforms - conf.gem core: 'mruby-task' -end -``` - -## Implementation Details - -### Timer Mechanism - -- Uses Windows multimedia timer (`timeSetEvent`) for periodic callbacks -- Timer interval configured by `MRB_TICK_UNIT` (default: 4ms) -- `TIME_KILL_SYNCHRONOUS` flag ensures clean timer shutdown -- Requests 1ms timer resolution via `timeBeginPeriod(1)` - -### Interrupt Protection - -- Critical sections protected using `CRITICAL_SECTION` objects -- Prevents race conditions during task queue modifications -- `EnterCriticalSection`/`LeaveCriticalSection` used for mutual exclusion -- Supports nested critical sections (automatic lock counting) - -### Multi-VM Support - -- Supports up to `MRB_TASK_MAX_VMS` concurrent mruby VM instances (default: 8) -- Single shared timer ticks all registered VMs -- Per-VM task counters optimize timer usage (timer disabled when idle) -- Interlocked operations (`InterlockedIncrement`/`InterlockedDecrement`) for thread safety - -### Timer Optimization - -The implementation dynamically enables/disables the timer based on task state: - -- **Timer enabled** when: Multiple ready tasks OR any waiting tasks exist -- **Timer disabled** when: Single task or all tasks dormant/suspended -- Reduces CPU usage and power consumption when scheduler is idle - -## Configuration - -Override these macros in your build config if needed: - -```ruby -conf.gem core: 'hal-win-task' do |spec| - # Custom tick interval (10ms instead of default 4ms) - spec.build.defines << 'MRB_TICK_UNIT=10' - - # Custom timeslice (5 ticks instead of default 3) - spec.build.defines << 'MRB_TIMESLICE_TICK_COUNT=5' - - # More concurrent VMs (16 instead of default 8) - spec.build.defines << 'MRB_TASK_MAX_VMS=16' -end -``` - -## Known Limitations - -- Timer resolution typically limited to 1-2ms even with `timeBeginPeriod(1)` -- Multimedia timers consume system resources (kernel timer objects) -- Timer callbacks execute in separate thread context (handled internally) -- Not suitable for hard real-time requirements -- May interfere with other multimedia applications requesting different timer resolutions - -## See Also - -- `mruby-task` - Core task scheduler -- `hal-posix-task` - POSIX/Unix HAL implementation -- Task scheduler documentation: `mrbgems/mruby-task/README.md` - -## Build Notes - -The `winmm` library is automatically linked by the gem specification. No additional linker configuration is needed. diff --git a/mrbgems/hal-win-task/mrbgem.rake b/mrbgems/hal-win-task/mrbgem.rake deleted file mode 100644 index ea40075e8..000000000 --- a/mrbgems/hal-win-task/mrbgem.rake +++ /dev/null @@ -1,11 +0,0 @@ -MRuby::Gem::Specification.new('hal-win-task') do |spec| - spec.license = 'MIT' - spec.authors = 'mruby developers' - spec.summary = 'Windows HAL for mruby-task' - - # HAL gem depends on feature gem - brings in mruby-task automatically - spec.add_dependency 'mruby-task', core: 'mruby-task' - - # Windows multimedia timer library - spec.linker.libraries << 'winmm' -end diff --git a/mrbgems/mruby-task/mrbgem.rake b/mrbgems/mruby-task/mrbgem.rake index 7edc62081..7c7b87a74 100644 --- a/mrbgems/mruby-task/mrbgem.rake +++ b/mrbgems/mruby-task/mrbgem.rake @@ -5,32 +5,4 @@ MRuby::Gem::Specification.new('mruby-task') do |spec| # Enable task scheduler globally (required for vm.c integration) spec.build.defines << 'MRB_USE_TASK_SCHEDULER' - - # Check if HAL gem is loaded - # HAL gems must be explicitly specified in build config (recommended) or via auto-selection below - spec.build.gems.one? { |g| g.name =~ /^hal-.*-task$/ } or begin - # No HAL found - determine appropriate error message or auto-load - suggested_hal = if spec.for_windows? - # Windows (including MinGW) - use Windows HAL - 'hal-win-task' - elsif RUBY_PLATFORM =~ /linux|darwin|bsd/ - 'hal-posix-task' - else - nil - end - - if suggested_hal - # Auto-load HAL gem for convenience (for development) - # This works because HAL gems declare dependency on mruby-task - warn "mruby-task: No HAL specified, loading #{suggested_hal} (explicit selection recommended)" - spec.build.gem core: suggested_hal - else - # Unknown platform - fail with helpful message - fail "mruby-task: No HAL available for platform '#{RUBY_PLATFORM}'.\n" \ - "Please specify HAL gem explicitly in your build config:\n" \ - " conf.gem core: 'hal-posix-task' # For Linux/macOS/BSD\n" \ - " conf.gem core: 'hal-win-task' # For Windows\n" \ - "Or create custom HAL - see mrbgems/mruby-task/README.md" - end - end end diff --git a/mrbgems/hal-posix-task/src/task_hal.c b/mrbgems/mruby-task/ports/posix/task_hal.c similarity index 94% rename from mrbgems/hal-posix-task/src/task_hal.c rename to mrbgems/mruby-task/ports/posix/task_hal.c index 6e293b704..e653da314 100644 --- a/mrbgems/hal-posix-task/src/task_hal.c +++ b/mrbgems/mruby-task/ports/posix/task_hal.c @@ -232,21 +232,3 @@ mrb_hal_task_final(mrb_state *mrb) (void)mrb; #endif } - -/* - * Gem initialization (empty - HAL functions called by mruby-task) - */ - -void -mrb_hal_posix_task_gem_init(mrb_state *mrb) -{ - (void)mrb; - /* HAL interface functions are called by mruby-task gem */ -} - -void -mrb_hal_posix_task_gem_final(mrb_state *mrb) -{ - (void)mrb; - /* Cleanup handled by mrb_hal_task_final called from mruby-task */ -} diff --git a/mrbgems/hal-win-task/src/task_hal.c b/mrbgems/mruby-task/ports/win/task_hal.c similarity index 91% rename from mrbgems/hal-win-task/src/task_hal.c rename to mrbgems/mruby-task/ports/win/task_hal.c index f6e306f3c..83e647441 100644 --- a/mrbgems/hal-win-task/src/task_hal.c +++ b/mrbgems/mruby-task/ports/win/task_hal.c @@ -167,21 +167,3 @@ mrb_hal_task_final(mrb_state *mrb) LeaveCriticalSection(&irq_lock); } } - -/* - * Gem initialization (empty - HAL functions called by mruby-task) - */ - -void -mrb_hal_win_task_gem_init(mrb_state *mrb) -{ - (void)mrb; - /* HAL interface functions are called by mruby-task gem */ -} - -void -mrb_hal_win_task_gem_final(mrb_state *mrb) -{ - (void)mrb; - /* Cleanup handled by mrb_hal_task_final called from mruby-task */ -}