mirror of
https://github.com/ruby/ruby.git
synced 2026-10-28 16:36:20 +08:00
Windows carried a second, much weaker scheduler: a single Win32 mutex as the GVL, vm->clock for preemption, its own VM barrier over vm->ractor.sync.barrier_*, its own Ractor wait/wakeup on a per-Ractor condvar, no ready queue and no time slice. Keeping two made the rest of the interpreter branch on which one was compiled in, and left Ractors on Windows without real scheduling. Move the scheduler out of thread_pthread.c into thread_sched.c and let both platforms use it. thread.c now includes thread_sched.c, which declares the scheduler <-> platform contract, includes THREAD_IMPL_SRC, and then builds the scheduler on the primitives that file provides. thread_pthread.c keeps only those primitives: synchronization, thread local storage, native thread creation and stacks, the communication pipe, the timer thread backend, the fork lock and the event hooks. The data structures the scheduler works on move to a new thread_sched.h, included from both platform headers, so struct rb_thread_sched, struct rb_native_thread and struct rb_thread_sched_item are now shared. thread_pthread_mn.c becomes thread_sched_mn.c: it is the M:N scheduler, not a pthread detail. Most of it -- the coroutine threads, the native thread stack pool, the timer wheel and the fd -> waiters map -- is platform independent; only the readiness backend is not, and that section is now marked as the seam where an epoll/kqueue split (or an IOCP backend, which is what Windows would need for M:N) would go. What Windows had to add is small: native_thread_interrupt(), native_thread_default_max_cpu(), timer_thread_polling(), timer_thread_wakeup_force(), RB_NATIVE_COND_MONOTONIC_P() and RB_NATIVE_MUTEX_TRYLOCK_DETECTS_SELF() -- the last because a CRITICAL_SECTION is recursive, so trylock cannot back a "somebody holds this" assertion. It sets USE_MN_THREADS to 0 and supplies the stubs thread_sched_mn.c provides elsewhere, so every thread there is dedicated, as it already was. With one scheduler the platform branches go away: all 16 uses of RUBY_THREAD_PTHREAD_H / RUBY_THREAD_WIN32_H are removed from vm_core.h, ractor.c, ractor_core.h, ractor_sync.c, vm_sync.c, vm.c and thread.c. ractor_check_blocking() went with them -- it only ever fed the win32 scheduler's vm->ractor.blocking_cnt -- and so did USE_VM_CLOCK, now that the common time slice drives preemption on Windows too. Verified on both platforms. Linux: 11 rounds of make test-all (36236 tests) and btest (2066 tests) with no failures, plus 474 runs of a scheduler and Ractor stress soak. Windows: no test fails that does not also fail on the same tree without this change, btest passes, and two 40-minute soaks of 438 and 460 runs are clean. Ractors now run in parallel there -- 3.6x on four Ractors, where before they could not. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
26 lines
997 B
C
26 lines
997 B
C
#ifndef RUBY_THREAD_NONE_H
|
|
#define RUBY_THREAD_NONE_H
|
|
|
|
#define RB_NATIVETHREAD_LOCK_INIT (void)(0)
|
|
#define RB_NATIVETHREAD_COND_INIT (void)(0)
|
|
|
|
// no-thread impl doesn't use TLS but define this to avoid using tls key
|
|
// based implementation in vm.c
|
|
#define RB_THREAD_LOCAL_SPECIFIER
|
|
|
|
// This model brings its own scheduler stubs (thread_none.c) instead of the
|
|
// common one; thread.c keys off this.
|
|
#define RB_THREAD_SCHED_NONE 1
|
|
|
|
// The scheduler's types are shared with the threaded platforms: the code that
|
|
// reads them (vm_sync.c, ractor.c, thread.c) is compiled for every thread
|
|
// model, so it needs the real fields even here, where nothing ever runs
|
|
// concurrently and they all stay zero. thread_none.c only ever passes the
|
|
// structs around, never looks inside them.
|
|
#include "thread_sched.h"
|
|
|
|
RUBY_EXTERN struct rb_execution_context_struct *ruby_current_ec;
|
|
NOINLINE(struct rb_execution_context_struct *rb_current_ec_noinline(void)); // for assertions
|
|
|
|
#endif /* RUBY_THREAD_NONE_H */
|