16#ifdef THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
18#include "internal/sanitizers.h"
23#define native_thread_yield() Sleep(0)
27#define RB_NATIVE_MUTEX_TRYLOCK_DETECTS_SELF 0
33#define USE_MN_THREADS 0
42#define RB_INTERNAL_THREAD_HOOK(event, th) ((void)0)
46static rb_serial_t current_fork_gen = 1;
50#define RB_NATIVE_COND_HRTIME_DEADLINE_P() 1
52static volatile DWORD ruby_native_thread_key = TLS_OUT_OF_INDEXES;
54static int w32_wait_events(HANDLE *events,
int count, DWORD timeout,
rb_thread_t *th);
56static void timer_thread_wakeup_force(
void);
57static void ubf_select(
void *ptr);
59rb_internal_thread_event_hook_t *
74rb_thread_event_hooks_registered_p(
void)
81w32_error(const
char *func)
84 DWORD err = GetLastError();
85 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
86 FORMAT_MESSAGE_FROM_SYSTEM |
87 FORMAT_MESSAGE_IGNORE_INSERTS,
90 MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
91 (LPTSTR) & lpMsgBuf, 0, NULL) == 0)
92 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
93 FORMAT_MESSAGE_FROM_SYSTEM |
94 FORMAT_MESSAGE_IGNORE_INSERTS,
97 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
98 (LPTSTR) & lpMsgBuf, 0, NULL);
99 rb_bug(
"%s: %s", func, (
char*)lpMsgBuf);
103#define W32_EVENT_DEBUG 0
106#define w32_event_debug printf
108#define w32_event_debug if (0) printf
111#ifdef USE_WIN32_MUTEX
113w32_mutex_lock(HANDLE lock,
bool try)
118 w32_event_debug(
"lock:%p\n", lock);
120 result = w32_wait_events(&lock, 1,
try ? 0 : INFINITE, 0);
124 w32_event_debug(
"locked lock:%p\n", lock);
127 case WAIT_OBJECT_0 + 1:
130 w32_event_debug(
"interrupted lock:%p\n", lock);
134 w32_event_debug(
"timeout locK:%p\n", lock);
138 rb_bug(
"win32_mutex_lock: WAIT_ABANDONED");
142 rb_bug(
"win32_mutex_lock: unknown result (%ld)", result);
150w32_mutex_create(
void)
152 HANDLE lock = CreateMutex(NULL, FALSE, NULL);
154 w32_error(
"rb_native_mutex_initialize");
161w32_close_handle(HANDLE handle)
163 if (CloseHandle(handle) == 0) {
164 w32_error(
"w32_close_handle");
175#ifdef USE_WIN32_MUTEX
176 w32_mutex_lock(lock->mutex,
false);
178 EnterCriticalSection(&lock->crit);
185#ifdef USE_WIN32_MUTEX
186 return w32_mutex_lock(lock->mutex,
true);
188 return TryEnterCriticalSection(&lock->crit) == 0 ? EBUSY : 0;
195#ifdef USE_WIN32_MUTEX
196 RUBY_DEBUG_LOG(
"lock:%p", lock->mutex);
197 ReleaseMutex(lock->mutex);
199 LeaveCriticalSection(&lock->crit);
206#ifdef USE_WIN32_MUTEX
207 lock->mutex = w32_mutex_create();
210 InitializeCriticalSection(&lock->crit);
217#ifdef USE_WIN32_MUTEX
218 w32_close_handle(lock->mutex);
220 DeleteCriticalSection(&lock->crit);
224struct cond_event_entry {
225 struct cond_event_entry* next;
226 struct cond_event_entry* prev;
234 struct cond_event_entry *e = cond->next;
235 struct cond_event_entry *head = (
struct cond_event_entry*)cond;
238 struct cond_event_entry *next = e->next;
239 struct cond_event_entry *prev = e->prev;
243 e->next = e->prev = e;
253 struct cond_event_entry *e = cond->next;
254 struct cond_event_entry *head = (
struct cond_event_entry*)cond;
257 struct cond_event_entry *next = e->next;
258 struct cond_event_entry *prev = e->prev;
264 e->next = e->prev = e;
271native_cond_timedwait_ms(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex,
unsigned long msec)
274 struct cond_event_entry entry;
275 struct cond_event_entry *head = (
struct cond_event_entry*)cond;
277 entry.event = CreateEvent(0, FALSE, FALSE, 0);
281 entry.prev = head->prev;
282 head->prev->next = &entry;
287 r = WaitForSingleObject(entry.event, msec);
288 if ((r != WAIT_OBJECT_0) && (r != WAIT_TIMEOUT)) {
289 rb_bug(
"rb_native_cond_wait: WaitForSingleObject returns %lu", r);
294 entry.prev->next = entry.next;
295 entry.next->prev = entry.prev;
297 w32_close_handle(entry.event);
298 return (r == WAIT_OBJECT_0) ? 0 : ETIMEDOUT;
304 native_cond_timedwait_ms(cond, mutex, INFINITE);
310 native_cond_timedwait_ms(cond, mutex, msec);
316native_cond_timeout(rb_nativethread_cond_t *cond,
const rb_hrtime_t rel)
319 rb_hrtime_t now = rb_hrtime_now();
320 return (rel > RB_HRTIME_MAX - now) ? RB_HRTIME_MAX : now + rel;
322 return rb_hrtime_now();
326native_cond_timedwait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex,
const rb_hrtime_t *abs)
328 rb_hrtime_t now = rb_hrtime_now();
330 if (*abs <= now)
return ETIMEDOUT;
332 rb_hrtime_t rel = *abs - now;
333 unsigned long msec = (
unsigned long)(rel / RB_HRTIME_PER_MSEC);
336 if (msec == 0) msec = 1;
338 return native_cond_timedwait_ms(cond, mutex, msec);
344 cond->next = (
struct cond_event_entry *)cond;
345 cond->prev = (
struct cond_event_entry *)cond;
359ruby_thread_from_native(
void)
361 return TlsGetValue(ruby_native_thread_key);
368 ccan_list_node_init(&th->sched.node.ubf);
372 rb_ractor_set_current_ec(th->ractor, th->ec);
374 return TlsSetValue(ruby_native_thread_key, th);
382w32_wait_events(HANDLE *events,
int count, DWORD timeout,
rb_thread_t *th)
384 HANDLE *targets = events;
386 const int initcount = count;
389 w32_event_debug(
"events:%p, count:%d, timeout:%ld, th:%u\n",
390 events, count, timeout, th ? rb_th_serial(th) : UINT_MAX);
392 if (th && (intr = th->nt->interrupt_event)) {
393 if (ResetEvent(intr) && (!RUBY_VM_INTERRUPTED(th->ec) || SetEvent(intr))) {
394 targets =
ALLOCA_N(HANDLE, count + 1);
395 memcpy(targets, events,
sizeof(HANDLE) * count);
397 targets[count++] = intr;
398 w32_event_debug(
"handle:%p (count:%d, intr)\n", intr, count);
400 else if (intr == th->nt->interrupt_event) {
401 w32_error(
"w32_wait_events");
405 w32_event_debug(
"WaitForMultipleObjects start count:%d\n", count);
406 ret = WaitForMultipleObjects(count, targets, FALSE, timeout);
407 w32_event_debug(
"WaitForMultipleObjects end ret:%lu\n", ret);
409 if (ret == (DWORD)(WAIT_OBJECT_0 + initcount) && th) {
412 if (ret == WAIT_FAILED && W32_EVENT_DEBUG) {
415 for (i = 0; i < count; i++) {
416 w32_event_debug(
"i:%d %s\n", i, GetHandleInformation(targets[i], &dmy) ?
"OK" :
"NG");
423rb_w32_wait_events_blocking(HANDLE *events,
int num, DWORD timeout)
425 return w32_wait_events(events, num, timeout, ruby_thread_from_native());
429rb_w32_wait_events(HANDLE *events,
int num, DWORD timeout)
434 BLOCKING_REGION(th, ret = rb_w32_wait_events_blocking(events, num, timeout),
435 ubf_select, ruby_thread_from_native(), FALSE);
440rb_w32_sleep(
unsigned long msec)
442 return w32_wait_events(0, 0, msec, ruby_thread_from_native());
446rb_w32_Sleep(
unsigned long msec)
451 BLOCKING_REGION(th, ret = rb_w32_sleep(msec),
452 ubf_select, ruby_thread_from_native(), FALSE);
460 return w32_wait_events(0, 0, 0, th);
471 if (!SetEvent(th->nt->interrupt_event)) {
472 w32_error(
"native_thread_interrupt");
481w32_resume_thread(HANDLE handle)
483 if (ResumeThread(handle) == (DWORD)-1) {
484 w32_error(
"w32_resume_thread");
489#define HAVE__BEGINTHREADEX 1
491#undef HAVE__BEGINTHREADEX
494#ifdef HAVE__BEGINTHREADEX
495#define start_thread (HANDLE)_beginthreadex
496#define thread_errno errno
497typedef unsigned long (__stdcall *w32_thread_start_func)(
void*);
499#define start_thread CreateThread
500#define thread_errno rb_w32_map_errno(GetLastError())
501typedef LPTHREAD_START_ROUTINE w32_thread_start_func;
505w32_create_thread(DWORD stack_size, w32_thread_start_func func,
void *val)
507 return start_thread(0, stack_size, func, val, CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION, 0);
511native_thread_join(HANDLE th)
513 w32_wait_events(&th, 1, INFINITE, 0);
516#if !defined(_WIN32_WINNT_WIN8) || _WIN32_WINNT < 0x602
519WINBASEAPI VOID WINAPI GetCurrentThreadStackLimits(PULONG_PTR, PULONG_PTR);
523native_thread_init_stack(
rb_thread_t *th,
void *local_in_parent_frame)
532 GetCurrentThreadStackLimits(&low, &high);
535 if (space > 1024*1024) space = 1024*1024;
536 th->ec->machine.stack_start = (
VALUE *)high - 1;
537 th->ec->machine.stack_maxsize = size - space;
548 nt->interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
549 if (nt->interrupt_event == NULL) {
550 w32_error(
"native_thread_setup");
561native_thread_alloc(
void)
564 native_thread_setup(nt);
566#if USE_RUBY_DEBUG_LOG
579#ifndef InterlockedExchangePointer
580#define InterlockedExchangePointer(t, v) \
581 (void *)InterlockedExchange((long *)(t), (long)(v))
588 HANDLE intr = InterlockedExchangePointer(&nt->interrupt_event, 0);
589 RUBY_DEBUG_LOG(
"close handle intr:%p, thid:%p\n", intr, nt->thread_id);
590 if (intr) w32_close_handle(intr);
602 native_thread_destroy(nt);
605static unsigned long __stdcall
606nt_start_trampoline(
void *nt_ptr)
609 HANDLE thread_id = nt->thread_id;
613 w32_close_handle(thread_id);
620 const size_t stack_size = nt->vm->default_params.thread_machine_stack_size;
622 nt->thread_id = w32_create_thread(stack_size, nt_start_trampoline, nt);
623 if (nt->thread_id == 0) {
627 w32_resume_thread(nt->thread_id);
629 RUBY_DEBUG_LOG(
"nt:%u thid:%p stack size:%"PRIuSIZE
"",
630 nt->serial, nt->thread_id, stack_size);
635native_thread_default_max_cpu(
void)
639 return si.dwNumberOfProcessors > 0 ? (int)si.dwNumberOfProcessors : 8;
642#if USE_NATIVE_THREAD_PRIORITY
647 int priority = th->priority;
648 if (th->priority > 0) {
649 priority = THREAD_PRIORITY_ABOVE_NORMAL;
651 else if (th->priority < 0) {
652 priority = THREAD_PRIORITY_BELOW_NORMAL;
655 priority = THREAD_PRIORITY_NORMAL;
658 SetThreadPriority(th->nt->thread_id, priority);
663int rb_w32_select_with_thread(
int, fd_set *, fd_set *, fd_set *,
struct timeval *,
void *);
668 fd_set *r = NULL, *w = NULL, *e = NULL;
681 return rb_w32_select_with_thread(n, r, w, e, timeout, th);
684int rb_w32_set_thread_description(HANDLE th,
const WCHAR *name);
685int rb_w32_set_thread_description_str(HANDLE th,
VALUE name);
686#define native_set_another_thread_name rb_w32_set_thread_description_str
696 DWORD tid = GetThreadId(th->nt->thread_id);
697 if (tid == 0) rb_sys_fail(
"GetThreadId");
700#define USE_NATIVE_THREAD_NATIVE_THREAD_ID 1
705 if ((ruby_current_ec_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) {
706 rb_bug(
"TlsAlloc() for ruby_current_ec_key fails");
708 if ((ruby_native_thread_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) {
709 rb_bug(
"TlsAlloc() for ruby_native_thread_key fails");
714 thread_sched_init_vm(vm);
717 native_thread_setup(main_th->nt);
718 DuplicateHandle(GetCurrentProcess(),
721 &main_th->nt->thread_id, 0, FALSE, DUPLICATE_SAME_ACCESS);
722 main_th->nt->serial = 1;
723 ruby_thread_set_native(main_th);
725 TH_SCHED(main_th)->running = main_th;
726 main_th->has_dedicated_nt = 1;
729 main_th->nt->dedicated = 1;
730 main_th->nt->running_thread = main_th;
731 main_th->nt->vm = vm;
733 thread_sched_setup_running_threads(TH_SCHED(main_th), main_th->ractor, vm, main_th, NULL);
735#if USE_RUBY_DEBUG_LOG
736 vm->ractor.sched.dnt_cnt = 1;
739 RUBY_DEBUG_LOG(
"initial thread th:%u thid:%p, event: %p",
740 rb_th_serial(main_th),
741 main_th->nt->thread_id,
742 main_th->nt->interrupt_event);
750 rb_serial_t created_fork_gen;
754 .created_fork_gen = 0,
757#define TIMER_THREAD_CREATED_P() (timer_th.created_fork_gen == current_fork_gen)
760timer_thread_wakeup_force(
void)
762 if (timer_th.wakeup_event) {
763 SetEvent(timer_th.wakeup_event);
768rb_thread_wakeup_timer_thread(
int sig)
770 timer_thread_wakeup_force();
780 RUBY_VM_SET_TRAP_INTERRUPT(main_th_ec);
782 if (vm->ubf_async_safe && main_th->unblock.func) {
783 (main_th->unblock.func)(main_th->unblock.arg);
793timer_thread_polling(
rb_vm_t *vm)
795 int timeout = timer_thread_set_timeout(vm);
796 DWORD msec = (timeout < 0) ? INFINITE : (DWORD)timeout;
798 DWORD ret = WaitForSingleObject(timer_th.wakeup_event, msec);
802 ractor_sched_lock(vm, NULL);
804 timer_thread_check_timeslice(vm);
806 ractor_sched_unlock(vm, NULL);
810 ResetEvent(timer_th.wakeup_event);
814 w32_error(
"timer_thread_polling");
818static unsigned long __stdcall
819timer_thread_trampoline(
void *vm_ptr)
821 rb_w32_set_thread_description(GetCurrentThread(), L
"ruby-timer-thread");
822 timer_thread_func(vm_ptr);
827rb_thread_create_timer_thread(
void)
829 timer_th.created_fork_gen = current_fork_gen;
831 if (timer_th.wakeup_event == NULL) {
832 timer_th.wakeup_event = CreateEvent(0, TRUE, FALSE, 0);
833 if (timer_th.wakeup_event == NULL) {
834 w32_error(
"rb_thread_create_timer_thread");
838 timer_th.thread_id = w32_create_thread(1024 + (USE_RUBY_DEBUG_LOG ? BUFSIZ : 0),
839 timer_thread_trampoline, GET_VM());
840 if (timer_th.thread_id == 0) {
841 rb_bug(
"rb_thread_create_timer_thread: failed to create the timer thread");
843 w32_resume_thread(timer_th.thread_id);
847native_stop_timer_thread(
void)
851 timer_thread_wakeup_force();
852 native_thread_join(timer_th.thread_id);
854 w32_close_handle(timer_th.wakeup_event);
855 timer_th.wakeup_event = NULL;
861native_reset_timer_thread(
void)
863 if (timer_th.thread_id) {
864 CloseHandle(timer_th.thread_id);
865 timer_th.thread_id = 0;
878native_thread_self_can_retire_p(
void)
886 rb_bug(
"unreachable");
889static enum thread_sched_wait_result
891 enum thread_sched_waiting_flag events, rb_hrtime_t *rel)
893 return thread_sched_wait_unavailable;
897ractor_sched_timeout_arm(
rb_thread_t *th,
const rb_hrtime_t *rel)
899 rb_bug(
"unreachable");
905 rb_bug(
"unreachable");
909timer_wheel_timeout(
int timeout)
921timer_thread_check_timeout(
rb_vm_t *vm)
931ruby_stack_overflowed_p(
const rb_thread_t *th,
const void *addr)
933 return rb_ec_raised_p(th->ec, RAISED_STACKOVERFLOW);
936#if defined(__MINGW32__)
938rb_w32_stack_overflow_handler(
struct _EXCEPTION_POINTERS *exception)
940 if (exception->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) {
941 rb_ec_raised_set(GET_EC(), RAISED_STACKOVERFLOW);
944 return EXCEPTION_CONTINUE_SEARCH;
948#ifdef RUBY_ALLOCA_CHKSTK
950ruby_alloca_chkstk(
size_t len,
void *sp)
954 if (!rb_ec_raised_p(ec, RAISED_STACKOVERFLOW)) {
955 rb_ec_raised_set(ec, RAISED_STACKOVERFLOW);
971 return GetCurrentThread();
975rb_thread_prevent_fork(
void *(*func)(
void *),
void *data)
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define RUBY_ATOMIC_FETCH_ADD(var, val)
Atomically replaces the value pointed by var with the result of addition of val to the old value of v...
#define RUBY_ATOMIC_LOAD(var)
Atomic load.
#define RUBY_ATOMIC_SET(var, val)
Identical to RUBY_ATOMIC_EXCHANGE, except for the return type.
uint32_t rb_event_flag_t
Represents event(s).
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define ZALLOC
Old name of RB_ZALLOC.
size_t ruby_stack_length(VALUE **p)
Queries what Ruby thinks is the machine stack.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
int rb_reserved_fd_p(int fd)
Queries if the given FD is reserved or not.
int len
Length of the buffer.
rb_internal_thread_event_hook_t * rb_internal_thread_add_event_hook(rb_internal_thread_event_callback func, rb_event_flag_t events, void *data)
Registers a thread event hook function.
bool rb_internal_thread_remove_event_hook(rb_internal_thread_event_hook_t *hook)
Unregister the passed hook.
static fd_set * rb_fd_ptr(const rb_fdset_t *f)
Raw pointer to fd_set.
#define ALLOCA_N(type, n)
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
#define errno
Ractor-aware version of errno.
#define rb_fd_resize(n, f)
Does nothing (defined for compatibility).
The data structure which wraps the fd_set bitmap used by select(2).
rb_nativethread_id_t rb_nativethread_self(void)
Queries the ID of the native thread that is calling this function.
void rb_native_mutex_lock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_lock.
void rb_native_cond_initialize(rb_nativethread_cond_t *cond)
Fills the passed condition variable with an initial value.
int rb_native_mutex_trylock(rb_nativethread_lock_t *lock)
Identical to rb_native_mutex_lock(), except it doesn't block in case rb_native_mutex_lock() would.
void rb_native_cond_broadcast(rb_nativethread_cond_t *cond)
Signals a condition variable.
void rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_initialize.
void rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_unlock.
void rb_native_mutex_destroy(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_destroy.
void rb_native_cond_destroy(rb_nativethread_cond_t *cond)
Destroys the passed condition variable.
void rb_native_cond_signal(rb_nativethread_cond_t *cond)
Signals a condition variable.
void rb_native_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex)
Waits for the passed condition variable to be signalled.
void rb_native_cond_timedwait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex, unsigned long msec)
Identical to rb_native_cond_wait(), except it additionally takes timeout in msec resolution.
uintptr_t VALUE
Type that represents a Ruby object.