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
112w32_mutex_lock(HANDLE lock,
bool try)
117 w32_event_debug(
"lock:%p\n", lock);
119 result = w32_wait_events(&lock, 1,
try ? 0 : INFINITE, 0);
123 w32_event_debug(
"locked lock:%p\n", lock);
126 case WAIT_OBJECT_0 + 1:
129 w32_event_debug(
"interrupted lock:%p\n", lock);
133 w32_event_debug(
"timeout locK:%p\n", lock);
137 rb_bug(
"win32_mutex_lock: WAIT_ABANDONED");
141 rb_bug(
"win32_mutex_lock: unknown result (%ld)", result);
149w32_mutex_create(
void)
151 HANDLE lock = CreateMutex(NULL, FALSE, NULL);
153 w32_error(
"rb_native_mutex_initialize");
159w32_close_handle(HANDLE handle)
161 if (CloseHandle(handle) == 0) {
162 w32_error(
"w32_close_handle");
173#ifdef USE_WIN32_MUTEX
174 w32_mutex_lock(lock->mutex,
false);
176 EnterCriticalSection(&lock->crit);
183#ifdef USE_WIN32_MUTEX
184 return w32_mutex_lock(lock->mutex,
true);
186 return TryEnterCriticalSection(&lock->crit) == 0 ? EBUSY : 0;
193#ifdef USE_WIN32_MUTEX
194 RUBY_DEBUG_LOG(
"lock:%p", lock->mutex);
195 ReleaseMutex(lock->mutex);
197 LeaveCriticalSection(&lock->crit);
204#ifdef USE_WIN32_MUTEX
205 lock->mutex = w32_mutex_create();
208 InitializeCriticalSection(&lock->crit);
215#ifdef USE_WIN32_MUTEX
216 w32_close_handle(lock->mutex);
218 DeleteCriticalSection(&lock->crit);
222struct cond_event_entry {
223 struct cond_event_entry* next;
224 struct cond_event_entry* prev;
232 struct cond_event_entry *e = cond->next;
233 struct cond_event_entry *head = (
struct cond_event_entry*)cond;
236 struct cond_event_entry *next = e->next;
237 struct cond_event_entry *prev = e->prev;
241 e->next = e->prev = e;
251 struct cond_event_entry *e = cond->next;
252 struct cond_event_entry *head = (
struct cond_event_entry*)cond;
255 struct cond_event_entry *next = e->next;
256 struct cond_event_entry *prev = e->prev;
262 e->next = e->prev = e;
269native_cond_timedwait_ms(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex,
unsigned long msec)
272 struct cond_event_entry entry;
273 struct cond_event_entry *head = (
struct cond_event_entry*)cond;
275 entry.event = CreateEvent(0, FALSE, FALSE, 0);
279 entry.prev = head->prev;
280 head->prev->next = &entry;
285 r = WaitForSingleObject(entry.event, msec);
286 if ((r != WAIT_OBJECT_0) && (r != WAIT_TIMEOUT)) {
287 rb_bug(
"rb_native_cond_wait: WaitForSingleObject returns %lu", r);
292 entry.prev->next = entry.next;
293 entry.next->prev = entry.prev;
295 w32_close_handle(entry.event);
296 return (r == WAIT_OBJECT_0) ? 0 : ETIMEDOUT;
302 native_cond_timedwait_ms(cond, mutex, INFINITE);
308 native_cond_timedwait_ms(cond, mutex, msec);
314native_cond_timeout(rb_nativethread_cond_t *cond,
const rb_hrtime_t rel)
317 rb_hrtime_t now = rb_hrtime_now();
318 return (rel > RB_HRTIME_MAX - now) ? RB_HRTIME_MAX : now + rel;
320 return rb_hrtime_now();
324native_cond_timedwait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex,
const rb_hrtime_t *abs)
326 rb_hrtime_t now = rb_hrtime_now();
328 if (*abs <= now)
return ETIMEDOUT;
330 rb_hrtime_t rel = *abs - now;
331 unsigned long msec = (
unsigned long)(rel / RB_HRTIME_PER_MSEC);
334 if (msec == 0) msec = 1;
336 return native_cond_timedwait_ms(cond, mutex, msec);
342 cond->next = (
struct cond_event_entry *)cond;
343 cond->prev = (
struct cond_event_entry *)cond;
357ruby_thread_from_native(
void)
359 return TlsGetValue(ruby_native_thread_key);
366 ccan_list_node_init(&th->sched.node.ubf);
370 rb_ractor_set_current_ec(th->ractor, th->ec);
372 return TlsSetValue(ruby_native_thread_key, th);
380w32_wait_events(HANDLE *events,
int count, DWORD timeout,
rb_thread_t *th)
382 HANDLE *targets = events;
384 const int initcount = count;
387 w32_event_debug(
"events:%p, count:%d, timeout:%ld, th:%u\n",
388 events, count, timeout, th ? rb_th_serial(th) : UINT_MAX);
390 if (th && (intr = th->nt->interrupt_event)) {
391 if (ResetEvent(intr) && (!RUBY_VM_INTERRUPTED(th->ec) || SetEvent(intr))) {
392 targets =
ALLOCA_N(HANDLE, count + 1);
393 memcpy(targets, events,
sizeof(HANDLE) * count);
395 targets[count++] = intr;
396 w32_event_debug(
"handle:%p (count:%d, intr)\n", intr, count);
398 else if (intr == th->nt->interrupt_event) {
399 w32_error(
"w32_wait_events");
403 w32_event_debug(
"WaitForMultipleObjects start count:%d\n", count);
404 ret = WaitForMultipleObjects(count, targets, FALSE, timeout);
405 w32_event_debug(
"WaitForMultipleObjects end ret:%lu\n", ret);
407 if (ret == (DWORD)(WAIT_OBJECT_0 + initcount) && th) {
410 if (ret == WAIT_FAILED && W32_EVENT_DEBUG) {
413 for (i = 0; i < count; i++) {
414 w32_event_debug(
"i:%d %s\n", i, GetHandleInformation(targets[i], &dmy) ?
"OK" :
"NG");
421rb_w32_wait_events_blocking(HANDLE *events,
int num, DWORD timeout)
423 return w32_wait_events(events, num, timeout, ruby_thread_from_native());
427rb_w32_wait_events(HANDLE *events,
int num, DWORD timeout)
432 BLOCKING_REGION(th, ret = rb_w32_wait_events_blocking(events, num, timeout),
433 ubf_select, ruby_thread_from_native(), FALSE);
438rb_w32_sleep(
unsigned long msec)
440 return w32_wait_events(0, 0, msec, ruby_thread_from_native());
444rb_w32_Sleep(
unsigned long msec)
449 BLOCKING_REGION(th, ret = rb_w32_sleep(msec),
450 ubf_select, ruby_thread_from_native(), FALSE);
458 return w32_wait_events(0, 0, 0, th);
469 if (!SetEvent(th->nt->interrupt_event)) {
470 w32_error(
"native_thread_interrupt");
479w32_resume_thread(HANDLE handle)
481 if (ResumeThread(handle) == (DWORD)-1) {
482 w32_error(
"w32_resume_thread");
487#define HAVE__BEGINTHREADEX 1
489#undef HAVE__BEGINTHREADEX
492#ifdef HAVE__BEGINTHREADEX
493#define start_thread (HANDLE)_beginthreadex
494#define thread_errno errno
495typedef unsigned long (__stdcall *w32_thread_start_func)(
void*);
497#define start_thread CreateThread
498#define thread_errno rb_w32_map_errno(GetLastError())
499typedef LPTHREAD_START_ROUTINE w32_thread_start_func;
503w32_create_thread(DWORD stack_size, w32_thread_start_func func,
void *val)
505 return start_thread(0, stack_size, func, val, CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION, 0);
509native_thread_join(HANDLE th)
511 w32_wait_events(&th, 1, INFINITE, 0);
514#if !defined(_WIN32_WINNT_WIN8) || _WIN32_WINNT < 0x602
517WINBASEAPI VOID WINAPI GetCurrentThreadStackLimits(PULONG_PTR, PULONG_PTR);
521native_thread_init_stack(
rb_thread_t *th,
void *local_in_parent_frame)
530 GetCurrentThreadStackLimits(&low, &high);
533 if (space > 1024*1024) space = 1024*1024;
534 th->ec->machine.stack_start = (
VALUE *)high - 1;
535 th->ec->machine.stack_maxsize = size - space;
546 nt->interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
547 if (nt->interrupt_event == NULL) {
548 w32_error(
"native_thread_setup");
559native_thread_alloc(
void)
562 native_thread_setup(nt);
564#if USE_RUBY_DEBUG_LOG
577#ifndef InterlockedExchangePointer
578#define InterlockedExchangePointer(t, v) \
579 (void *)InterlockedExchange((long *)(t), (long)(v))
586 HANDLE intr = InterlockedExchangePointer(&nt->interrupt_event, 0);
587 RUBY_DEBUG_LOG(
"close handle intr:%p, thid:%p\n", intr, nt->thread_id);
588 if (intr) w32_close_handle(intr);
600 native_thread_destroy(nt);
603static unsigned long __stdcall
604nt_start_trampoline(
void *nt_ptr)
607 HANDLE thread_id = nt->thread_id;
611 w32_close_handle(thread_id);
618 const size_t stack_size = nt->vm->default_params.thread_machine_stack_size;
620 nt->thread_id = w32_create_thread(stack_size, nt_start_trampoline, nt);
621 if (nt->thread_id == 0) {
625 w32_resume_thread(nt->thread_id);
627 RUBY_DEBUG_LOG(
"nt:%u thid:%p stack size:%"PRIuSIZE
"",
628 nt->serial, nt->thread_id, stack_size);
633native_thread_default_max_cpu(
void)
637 return si.dwNumberOfProcessors > 0 ? (int)si.dwNumberOfProcessors : 8;
640#if USE_NATIVE_THREAD_PRIORITY
645 int priority = th->priority;
646 if (th->priority > 0) {
647 priority = THREAD_PRIORITY_ABOVE_NORMAL;
649 else if (th->priority < 0) {
650 priority = THREAD_PRIORITY_BELOW_NORMAL;
653 priority = THREAD_PRIORITY_NORMAL;
656 SetThreadPriority(th->nt->thread_id, priority);
661int rb_w32_select_with_thread(
int, fd_set *, fd_set *, fd_set *,
struct timeval *,
void *);
666 fd_set *r = NULL, *w = NULL, *e = NULL;
679 return rb_w32_select_with_thread(n, r, w, e, timeout, th);
682int rb_w32_set_thread_description(HANDLE th,
const WCHAR *name);
683int rb_w32_set_thread_description_str(HANDLE th,
VALUE name);
684#define native_set_another_thread_name rb_w32_set_thread_description_str
694 DWORD tid = GetThreadId(th->nt->thread_id);
695 if (tid == 0) rb_sys_fail(
"GetThreadId");
698#define USE_NATIVE_THREAD_NATIVE_THREAD_ID 1
703 if ((ruby_current_ec_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) {
704 rb_bug(
"TlsAlloc() for ruby_current_ec_key fails");
706 if ((ruby_native_thread_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) {
707 rb_bug(
"TlsAlloc() for ruby_native_thread_key fails");
712 thread_sched_init_vm(vm);
715 native_thread_setup(main_th->nt);
716 DuplicateHandle(GetCurrentProcess(),
719 &main_th->nt->thread_id, 0, FALSE, DUPLICATE_SAME_ACCESS);
720 main_th->nt->serial = 1;
721 ruby_thread_set_native(main_th);
723 TH_SCHED(main_th)->running = main_th;
724 main_th->has_dedicated_nt = 1;
727 main_th->nt->dedicated = 1;
728 main_th->nt->running_thread = main_th;
729 main_th->nt->vm = vm;
731 thread_sched_setup_running_threads(TH_SCHED(main_th), main_th->ractor, vm, main_th, NULL);
733#if USE_RUBY_DEBUG_LOG
734 vm->ractor.sched.dnt_cnt = 1;
737 RUBY_DEBUG_LOG(
"initial thread th:%u thid:%p, event: %p",
738 rb_th_serial(main_th),
739 main_th->nt->thread_id,
740 main_th->nt->interrupt_event);
748 rb_serial_t created_fork_gen;
752 .created_fork_gen = 0,
755#define TIMER_THREAD_CREATED_P() (timer_th.created_fork_gen == current_fork_gen)
758timer_thread_wakeup_force(
void)
760 if (timer_th.wakeup_event) {
761 SetEvent(timer_th.wakeup_event);
766rb_thread_wakeup_timer_thread(
int sig)
768 timer_thread_wakeup_force();
778 RUBY_VM_SET_TRAP_INTERRUPT(main_th_ec);
780 if (vm->ubf_async_safe && main_th->unblock.func) {
781 (main_th->unblock.func)(main_th->unblock.arg);
791timer_thread_polling(
rb_vm_t *vm)
793 int timeout = timer_thread_set_timeout(vm);
794 DWORD msec = (timeout < 0) ? INFINITE : (DWORD)timeout;
796 DWORD ret = WaitForSingleObject(timer_th.wakeup_event, msec);
800 ractor_sched_lock(vm, NULL);
802 timer_thread_check_timeslice(vm);
804 ractor_sched_unlock(vm, NULL);
808 ResetEvent(timer_th.wakeup_event);
812 w32_error(
"timer_thread_polling");
816static unsigned long __stdcall
817timer_thread_trampoline(
void *vm_ptr)
819 rb_w32_set_thread_description(GetCurrentThread(), L
"ruby-timer-thread");
820 timer_thread_func(vm_ptr);
825rb_thread_create_timer_thread(
void)
827 timer_th.created_fork_gen = current_fork_gen;
829 if (timer_th.wakeup_event == NULL) {
830 timer_th.wakeup_event = CreateEvent(0, TRUE, FALSE, 0);
831 if (timer_th.wakeup_event == NULL) {
832 w32_error(
"rb_thread_create_timer_thread");
836 timer_th.thread_id = w32_create_thread(1024 + (USE_RUBY_DEBUG_LOG ? BUFSIZ : 0),
837 timer_thread_trampoline, GET_VM());
838 if (timer_th.thread_id == 0) {
839 rb_bug(
"rb_thread_create_timer_thread: failed to create the timer thread");
841 w32_resume_thread(timer_th.thread_id);
845native_stop_timer_thread(
void)
849 timer_thread_wakeup_force();
850 native_thread_join(timer_th.thread_id);
852 w32_close_handle(timer_th.wakeup_event);
853 timer_th.wakeup_event = NULL;
859native_reset_timer_thread(
void)
861 if (timer_th.thread_id) {
862 CloseHandle(timer_th.thread_id);
863 timer_th.thread_id = 0;
878 rb_bug(
"unreachable");
881static enum thread_sched_wait_result
883 enum thread_sched_waiting_flag events, rb_hrtime_t *rel)
885 return thread_sched_wait_unavailable;
889ractor_sched_timeout_arm(
rb_thread_t *th,
const rb_hrtime_t *rel)
891 rb_bug(
"unreachable");
897 rb_bug(
"unreachable");
901timer_wheel_timeout(
int timeout)
913timer_thread_check_timeout(
rb_vm_t *vm)
923ruby_stack_overflowed_p(
const rb_thread_t *th,
const void *addr)
925 return rb_ec_raised_p(th->ec, RAISED_STACKOVERFLOW);
928#if defined(__MINGW32__)
930rb_w32_stack_overflow_handler(
struct _EXCEPTION_POINTERS *exception)
932 if (exception->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) {
933 rb_ec_raised_set(GET_EC(), RAISED_STACKOVERFLOW);
936 return EXCEPTION_CONTINUE_SEARCH;
940#ifdef RUBY_ALLOCA_CHKSTK
942ruby_alloca_chkstk(
size_t len,
void *sp)
946 if (!rb_ec_raised_p(ec, RAISED_STACKOVERFLOW)) {
947 rb_ec_raised_set(ec, RAISED_STACKOVERFLOW);
963 return GetCurrentThread();
967rb_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.