ruby/thread_win32.h
Koichi Sasada be1bbd5b7d M:N thread scheduler for Ractors
This patch introduce M:N thread scheduler for Ractor system.

In general, M:N thread scheduler employs N native threads (OS threads)
to manage M user-level threads (Ruby threads in this case).
On the Ruby interpreter, 1 native thread is provided for 1 Ractor
and all Ruby threads are managed by the native thread.

From Ruby 1.9, the interpreter uses 1:1 thread scheduler which means
1 Ruby thread has 1 native thread. M:N scheduler change this strategy.

Because of compatibility issue (and stableness issue of the implementation)
main Ractor doesn't use M:N scheduler on default. On the other words,
threads on the main Ractor will be managed with 1:1 thread scheduler.

There are additional settings by environment variables:

`RUBY_MN_THREADS=1` enables M:N thread scheduler on the main ractor.
Note that non-main ractors use the M:N scheduler without this
configuration. With this configuration, single ractor applications
run threads on M:1 thread scheduler (green threads, user-level threads).

`RUBY_MAX_CPU=n` specifies maximum number of native threads for
M:N scheduler (default: 8).

This patch will be reverted soon if non-easy issues are found.

[Bug #19842]
2023-10-12 14:47:01 +09:00

59 lines
1.1 KiB
C

#ifndef RUBY_THREAD_WIN32_H
#define RUBY_THREAD_WIN32_H
/**********************************************************************
thread_win32.h -
$Author$
Copyright (C) 2004-2007 Koichi Sasada
**********************************************************************/
/* interface */
# ifdef __CYGWIN__
# undef _WIN32
# endif
#define USE_VM_CLOCK 1
WINBASEAPI BOOL WINAPI
TryEnterCriticalSection(IN OUT LPCRITICAL_SECTION lpCriticalSection);
struct rb_native_thread {
HANDLE thread_id;
HANDLE interrupt_event;
};
struct rb_thread_sched_item {
void *vm_stack;
};
struct rb_thread_sched {
HANDLE lock;
};
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 */