add mingw pattern to RUBY_PLATFORM check. native mingw builds were
falling through to windows hal because previous detection only worked
for cross-compilation. now checks RUBY_PLATFORM for mingw along with
linux/darwin/bsd.
Co-authored-by: Claude <noreply@anthropic.com>
add explicit cast when assigning mrb_int to mp_limb. the value is
already validated to fit within mp_limb range by checking against
DIG_BASE, but explicit cast silences msvc warning c4244.
Co-authored-by: Claude <noreply@anthropic.com>
add explicit cast to DWORD when passing usec to Sleep(). Sleep() takes
32-bit DWORD but usec is mrb_int which can be 64-bit, causing warning
c4244.
Co-authored-by: Claude <noreply@anthropic.com>
only define mrb_lstat when symbolic link macros are available. on
windows/mingw, symlinks are not supported and the function is unused,
causing -Wunused-function warning.
Co-authored-by: Claude <noreply@anthropic.com>
remove example containing /* sequence from comment. this triggers
-Wcomment warning on mingw about nested comments.
Co-authored-by: Claude <noreply@anthropic.com>
only define _WIN32_WINNT if not already defined. mingw headers may
predefine this macro, causing redefinition warning.
Co-authored-by: Claude <noreply@anthropic.com>
change sleep_us_impl and sleep_ms_impl parameters from mrb_int to uint32_t.
this makes the type requirement explicit and resolves msvc warning c4244.
all type conversions happen at ruby boundary functions after validation.
Co-authored-by: Claude <noreply@anthropic.com>
replace (-rot) with (32 - rot) to avoid msvc warning c4146. both
expressions are equivalent when masked with & 31, but the latter
is clearer and doesn't trigger warnings about negating unsigned values.
Co-authored-by: Claude <noreply@anthropic.com>
remove const qualifier from variables passed to free functions.
msvc is stricter about const correctness than gcc. variables from
mrb_utf8_from_locale and mrb_locale_from_utf8 are dynamically allocated
and need to be freed, so they should not be const.
Co-authored-by: Claude <noreply@anthropic.com>
mingw provides posix-compatible functions (readlink, symlink, opendir, etc.)
so it should use hal-posix-io/dir instead of hal-win-io/dir. detect mingw by
checking if host_target or compiler command contains "mingw". check posix
platforms first so mingw is caught before for_windows check.
this fixes test failures on mingw where readlink returned absolute paths
instead of relative paths, and symlink/socket tests failed due to api
differences between windows native apis and posix apis.
Co-authored-by: Claude <noreply@anthropic.com>
increase sandbox path buffer from 1024 to 2048 bytes to accommodate
full path with suffix without truncation.
Co-authored-by: Claude <noreply@anthropic.com>
replace posix directory functions with hal interface functions in dirtest.c
to fix windows linking errors. test code now uses mrb_hal_dir_open/read/close
instead of opendir/readdir/closedir, and mrb_hal_dir_* for filesystem
operations.
Co-authored-by: Claude <noreply@anthropic.com>
reduced TASK_STACK_INIT_SIZE from 64 to 16 and TASK_CI_INIT_SIZE from 8 to 4,
matching mruby-fiber's conservative allocations. this saves 56 bytes per task
(320 bytes down to 160 bytes for initial allocations). stacks grow dynamically
via mrb_stack_extend when needed.
Co-authored-by: Claude <noreply@anthropic.com>
use t->c.ci->proc directly with explicit null check instead of falling
back to t->proc (which was removed). with c function boundary checks
preventing suspension in c functions, proc should always be valid on resume.
Co-authored-by: Claude <noreply@anthropic.com>
removes duplicate proc field and adds state-based union for result/timeslice,
achieving 16 bytes total savings per task (12.5% reduction):
optimizations:
- removed proc field (stored in c.ci->proc, already marked by gc): 8 bytes
- unified result/timeslice into state union (mutually exclusive): ~4 bytes
- combined with previous commit savings (priority_preemption, started, etc)
total reduction: 128 -> 112 bytes per task
impact:
- 10 tasks: 160 bytes saved
- 50 tasks: 800 bytes saved
- 100 tasks: 1.6 KB saved
all 1770 tests pass with zero functionality changes.
Co-authored-by: Claude <noreply@anthropic.com>
reduces per-task memory usage by 8 bytes (6.2%) through:
- removing priority_preemption field (always equals priority)
- removing started flag (inferred from context status)
- unifying wakeup_tick/join/mutex into single union
old size: 128 bytes
new size: 120 bytes
all tests pass with no functionality changes.
Co-authored-by: Claude <noreply@anthropic.com>
updates algorithm section to document the change from xoshiro128++
to PCG-XSH-RR. highlights key benefits including 50% memory reduction,
platform-adaptive optimization, and excellent statistical quality.
Co-authored-by: Claude <noreply@anthropic.com>
replaces xoshiro128++/xorshift96 with PCG-XSH-RR algorithm. PCG uses
64-bit state compared to xoshiro's 128-bit state, reducing memory
footprint by 50% while maintaining excellent statistical quality.
on 32-bit platforms, uses optimized 32-bit multiplier (0xf13283ad)
requiring only 2 multiplies instead of 3. on 64-bit platforms, uses
standard 64-bit multiplier for maximum quality.
all existing tests pass. api compatibility maintained.
Co-authored-by: Claude <noreply@anthropic.com>
add missing headers (direct.h for _getcwd, stdint.h for intptr_t) and
fix handle/int pointer truncation warnings by casting through intptr_t.
handles are 64-bit pointers on x64 windows but the hal interface uses
int for pid, requiring intermediate cast to suppress warnings.
Co-authored-by: Claude <noreply@anthropic.com>
when task.pass is called from within a C function (such as Module.new's
block evaluation), attempting to yield would cause a segfault because C
functions lack valid bytecode program counters (see #6642).
this commit adds C function boundary detection to task.pass, raising a
runtime error when cci > 0 (indicating execution is inside a C function).
this matches fiber's behavior and provides a clear error message instead of
a cryptic segfault.
unlike the previous commit which allowed sleep to fall back to blocking
sleep, task.pass raises an exception because its sole purpose is cooperative
yielding - there is no sensible blocking fallback behavior.
Co-authored-by: Claude <noreply@anthropic.com>
when sleep() was called from within a C function (such as module.new's block
evaluation), the task scheduler would segfault while attempting to resume the
task. this occurred because C functions don't execute bytecode and thus their
callinfo has no valid program counter (pc). when the task tried to resume
execution, mrb_vm_exec() received a null pc, causing a segmentation fault.
the fix adds two safeguards in task.c:
1. C function boundary detection: before suspending a task for sleep, check
if we're inside a C function by examining the cci (c call info) field.
if cci > 0, fall back to blocking sleep via HAL instead of attempting
cooperative context switch. this preserves sleep functionality without
raising exceptions, though it blocks other tasks during the sleep period.
2. proc fallback in execute_task(): use the task's stored proc if the
current callinfo's proc is null, ensuring mrb_vm_exec() always receives
a valid proc pointer.
this approach prioritizes functionality over strict cooperative multitasking
semantics - tasks can still sleep inside C functions, but the sleep becomes
blocking. the alternative would be raising an exception like fiber does, but
that would break existing code unexpectedly.
Co-authored-by: Claude <noreply@anthropic.com>
rename all HAL functions from mrb_<feature>_hal_<name>() to
mrb_hal_<feature>_<name>() for better grouping and clarity. this makes all
HAL functions immediately identifiable with the mrb_hal_* prefix.
affected gems:
- mruby-task: mrb_task_hal_* -> mrb_hal_task_*
- mruby-io: mrb_io_hal_* -> mrb_hal_io_*
- mruby-socket: mrb_socket_hal_* -> mrb_hal_socket_*
- mruby-dir: mrb_dir_hal_* -> mrb_hal_dir_*
Co-authored-by: Claude <noreply@anthropic.com>
platform-specific directory operations separated into hal-posix-dir and
hal-win-dir gems. this allows mruby-dir to support embedded platforms and
simplifies platform-specific implementations.
Co-authored-by: Claude <noreply@anthropic.com>
changed from angle brackets to quotes for gem-local HAL headers
(task.h, io_hal.h, socket_hal.h), and removed relative path prefix
from task.h include. this follows the mrbgem build system convention
where gem/include/ is automatically added to the include path.
Co-authored-by: Claude <noreply@anthropic.com>
separate platform-specific socket operations into HAL implementations
for POSIX (Linux/macOS/BSD/Unix) and Windows platforms to improve
portability and maintainability
Co-authored-by: Claude <noreply@anthropic.com>
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>
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>
task.c used clock_gettime() directly, breaking portability. added
mrb_task_hal_sleep_us() to hal interface.
Co-authored-by: Claude <noreply@anthropic.com>
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>
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>
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>
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>
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>
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>
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>
__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>
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>
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>
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>
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>
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>
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>
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>