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>
64 lines
1.7 KiB
C
64 lines
1.7 KiB
C
#ifndef RUBY_THREAD_PTHREAD_H
|
|
#define RUBY_THREAD_PTHREAD_H
|
|
/**********************************************************************
|
|
|
|
thread_pthread.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.
|
|
|
|
**********************************************************************/
|
|
|
|
#ifdef HAVE_PTHREAD_NP_H
|
|
#include <pthread_np.h>
|
|
#endif
|
|
|
|
#define RB_NATIVETHREAD_LOCK_INIT PTHREAD_MUTEX_INITIALIZER
|
|
#define RB_NATIVETHREAD_COND_INIT PTHREAD_COND_INITIALIZER
|
|
|
|
// TLS can not be accessed across .so on arm64 and perhaps ppc64le too.
|
|
#if defined(__arm64__) || defined(__aarch64__) || defined(__powerpc64__)
|
|
# define RB_THREAD_CURRENT_EC_NOINLINE
|
|
#endif
|
|
|
|
#include "thread_sched.h"
|
|
|
|
#ifdef RB_THREAD_LOCAL_SPECIFIER
|
|
NOINLINE(void rb_current_ec_set(struct rb_execution_context_struct *));
|
|
|
|
# ifdef RB_THREAD_CURRENT_EC_NOINLINE
|
|
NOINLINE(struct rb_execution_context_struct *rb_current_ec(void));
|
|
# else
|
|
RUBY_EXTERN RB_THREAD_LOCAL_SPECIFIER struct rb_execution_context_struct *ruby_current_ec;
|
|
|
|
// for RUBY_DEBUG_LOG()
|
|
RUBY_EXTERN RB_THREAD_LOCAL_SPECIFIER rb_atomic_t ruby_nt_serial;
|
|
#define RUBY_NT_SERIAL 1
|
|
# endif
|
|
#else
|
|
typedef pthread_key_t native_tls_key_t;
|
|
|
|
static inline void *
|
|
native_tls_get(native_tls_key_t key)
|
|
{
|
|
// return value should be checked by caller
|
|
return pthread_getspecific(key);
|
|
}
|
|
|
|
static inline void
|
|
native_tls_set(native_tls_key_t key, void *ptr)
|
|
{
|
|
if (UNLIKELY(pthread_setspecific(key, ptr) != 0)) {
|
|
rb_bug("pthread_setspecific error");
|
|
}
|
|
}
|
|
|
|
RUBY_EXTERN native_tls_key_t ruby_current_ec_key;
|
|
#endif
|
|
|
|
#endif /* RUBY_THREAD_PTHREAD_H */
|