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>
49 lines
1.0 KiB
C
49 lines
1.0 KiB
C
#ifndef RUBY_THREAD_WIN32_H
|
|
#define RUBY_THREAD_WIN32_H
|
|
/**********************************************************************
|
|
|
|
thread_win32.h -
|
|
|
|
$Author$
|
|
|
|
Copyright (C) 2004-2007 Koichi Sasada
|
|
|
|
This platform runs the common scheduler; see thread_sched.h for its data
|
|
structures and thread_sched.c for the implementation.
|
|
|
|
**********************************************************************/
|
|
|
|
/* interface */
|
|
|
|
# ifdef __CYGWIN__
|
|
# undef _WIN32
|
|
# endif
|
|
|
|
WINBASEAPI BOOL WINAPI
|
|
TryEnterCriticalSection(IN OUT LPCRITICAL_SECTION lpCriticalSection);
|
|
|
|
#include "thread_sched.h"
|
|
|
|
typedef DWORD native_tls_key_t; // TLS index
|
|
|
|
static inline void *
|
|
native_tls_get(native_tls_key_t key)
|
|
{
|
|
// return value should be checked by caller.
|
|
return TlsGetValue(key);
|
|
}
|
|
|
|
static inline void
|
|
native_tls_set(native_tls_key_t key, void *ptr)
|
|
{
|
|
if (UNLIKELY(TlsSetValue(key, ptr) == 0)) {
|
|
rb_bug("TlsSetValue() error");
|
|
}
|
|
}
|
|
|
|
RUBY_SYMBOL_EXPORT_BEGIN
|
|
RUBY_EXTERN native_tls_key_t ruby_current_ec_key;
|
|
RUBY_SYMBOL_EXPORT_END
|
|
|
|
#endif /* RUBY_THREAD_WIN32_H */
|