Ruby 4.1.0dev (2026-10-28 revision 1b06b950bb460d20a9dcecb8b97d363adbf4a358)
thread_win32.c (1b06b950bb460d20a9dcecb8b97d363adbf4a358)
1/* -*-c-*- */
2/**********************************************************************
3
4 thread_win32.c -
5
6 $Author$
7
8 Copyright (C) 2004-2007 Koichi Sasada
9
10 Windows platform primitives for the common thread/ractor scheduler. The
11 scheduler itself is in thread_sched.c, which includes this file and then
12 builds on the primitives below; see thread_sched.h for the contract.
13
14**********************************************************************/
15
16#ifdef THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
17
18#include "internal/sanitizers.h"
19#include <process.h>
20
21#undef Sleep
22
23#define native_thread_yield() Sleep(0)
24
25// A CRITICAL_SECTION is recursive, so trylock cannot tell "held by me" from
26// "free"; see thread_sched.c.
27#define RB_NATIVE_MUTEX_TRYLOCK_DETECTS_SELF 0
28
29// M:N threads need an event backend to park a coroutine on (epoll/kqueue on
30// the POSIX side; IOCP would be the Windows counterpart). Until there is one
31// every thread here is dedicated, and this file supplies the stubs that
32// thread_sched_mn.c provides elsewhere.
33#define USE_MN_THREADS 0
34
35// Interruption is delivered through a per-native-thread event object rather
36// than a signal, but the bookkeeping is the same as everywhere else.
37#define USE_UBF_LIST 1
38
39#include COROUTINE_H
40
41// Thread event hooks are not implemented on this platform.
42#define RB_INTERNAL_THREAD_HOOK(event, th) ((void)0)
43
44// No fork(), so this never advances; it only keeps TIMER_THREAD_CREATED_P()
45// spelled the same way on both platforms.
46static rb_serial_t current_fork_gen = 1;
47
48// Always: native_cond_timedwait() below takes the rb_hrtime_t deadline and
49// converts it to the relative timeout the Win32 wait wants itself.
50#define RB_NATIVE_COND_HRTIME_DEADLINE_P() 1
51
52static volatile DWORD ruby_native_thread_key = TLS_OUT_OF_INDEXES;
53
54static int w32_wait_events(HANDLE *events, int count, DWORD timeout, rb_thread_t *th);
55static void native_thread_destroy(struct rb_native_thread *nt);
56static void timer_thread_wakeup_force(void);
57static void ubf_select(void *ptr); // thread_sched.c
58
59rb_internal_thread_event_hook_t *
60rb_internal_thread_add_event_hook(rb_internal_thread_event_callback callback, rb_event_flag_t internal_event, void *user_data)
61{
62 // not implemented
63 return NULL;
64}
65
66bool
67rb_internal_thread_remove_event_hook(rb_internal_thread_event_hook_t * hook)
68{
69 // not implemented
70 return false;
71}
72
73bool
74rb_thread_event_hooks_registered_p(void)
75{
76 return false; // hooks are not implemented on this platform
77}
78
80static void
81w32_error(const char *func)
82{
83 LPVOID lpMsgBuf;
84 DWORD err = GetLastError();
85 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
86 FORMAT_MESSAGE_FROM_SYSTEM |
87 FORMAT_MESSAGE_IGNORE_INSERTS,
88 NULL,
89 err,
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,
95 NULL,
96 err,
97 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
98 (LPTSTR) & lpMsgBuf, 0, NULL);
99 rb_bug("%s: %s", func, (char*)lpMsgBuf);
101}
102
103#define W32_EVENT_DEBUG 0
104
105#if W32_EVENT_DEBUG
106#define w32_event_debug printf
107#else
108#define w32_event_debug if (0) printf
109#endif
110
111#ifdef USE_WIN32_MUTEX
112static int
113w32_mutex_lock(HANDLE lock, bool try)
114{
115 DWORD result;
116 while (1) {
117 // RUBY_DEBUG_LOG() is not available because RUBY_DEBUG_LOG() calls it.
118 w32_event_debug("lock:%p\n", lock);
119
120 result = w32_wait_events(&lock, 1, try ? 0 : INFINITE, 0);
121 switch (result) {
122 case WAIT_OBJECT_0:
123 /* get mutex object */
124 w32_event_debug("locked lock:%p\n", lock);
125 return 0;
126
127 case WAIT_OBJECT_0 + 1:
128 /* interrupt */
129 errno = EINTR;
130 w32_event_debug("interrupted lock:%p\n", lock);
131 return 0;
132
133 case WAIT_TIMEOUT:
134 w32_event_debug("timeout locK:%p\n", lock);
135 return EBUSY;
136
137 case WAIT_ABANDONED:
138 rb_bug("win32_mutex_lock: WAIT_ABANDONED");
139 break;
140
141 default:
142 rb_bug("win32_mutex_lock: unknown result (%ld)", result);
143 break;
144 }
145 }
146 return 0;
147}
148
149static HANDLE
150w32_mutex_create(void)
151{
152 HANDLE lock = CreateMutex(NULL, FALSE, NULL);
153 if (lock == NULL) {
154 w32_error("rb_native_mutex_initialize");
155 }
156 return lock;
157}
158#endif
159
160static void
161w32_close_handle(HANDLE handle)
162{
163 if (CloseHandle(handle) == 0) {
164 w32_error("w32_close_handle");
165 }
166}
167
168/* -------------------------------------------------------------------------
169 * native mutex / condition variable
170 * ------------------------------------------------------------------------- */
171
172void
173rb_native_mutex_lock(rb_nativethread_lock_t *lock)
174{
175#ifdef USE_WIN32_MUTEX
176 w32_mutex_lock(lock->mutex, false);
177#else
178 EnterCriticalSection(&lock->crit);
179#endif
180}
181
182int
183rb_native_mutex_trylock(rb_nativethread_lock_t *lock)
184{
185#ifdef USE_WIN32_MUTEX
186 return w32_mutex_lock(lock->mutex, true);
187#else
188 return TryEnterCriticalSection(&lock->crit) == 0 ? EBUSY : 0;
189#endif
190}
191
192void
193rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
194{
195#ifdef USE_WIN32_MUTEX
196 RUBY_DEBUG_LOG("lock:%p", lock->mutex);
197 ReleaseMutex(lock->mutex);
198#else
199 LeaveCriticalSection(&lock->crit);
200#endif
201}
202
203void
204rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
205{
206#ifdef USE_WIN32_MUTEX
207 lock->mutex = w32_mutex_create();
208 /* thread_debug("initialize mutex: %p\n", lock->mutex); */
209#else
210 InitializeCriticalSection(&lock->crit);
211#endif
212}
213
214void
215rb_native_mutex_destroy(rb_nativethread_lock_t *lock)
216{
217#ifdef USE_WIN32_MUTEX
218 w32_close_handle(lock->mutex);
219#else
220 DeleteCriticalSection(&lock->crit);
221#endif
222}
223
224struct cond_event_entry {
225 struct cond_event_entry* next;
226 struct cond_event_entry* prev;
227 HANDLE event;
228};
229
230void
231rb_native_cond_signal(rb_nativethread_cond_t *cond)
232{
233 /* cond is guarded by mutex */
234 struct cond_event_entry *e = cond->next;
235 struct cond_event_entry *head = (struct cond_event_entry*)cond;
236
237 if (e != head) {
238 struct cond_event_entry *next = e->next;
239 struct cond_event_entry *prev = e->prev;
240
241 prev->next = next;
242 next->prev = prev;
243 e->next = e->prev = e;
244
245 SetEvent(e->event);
246 }
247}
248
249void
250rb_native_cond_broadcast(rb_nativethread_cond_t *cond)
251{
252 /* cond is guarded by mutex */
253 struct cond_event_entry *e = cond->next;
254 struct cond_event_entry *head = (struct cond_event_entry*)cond;
255
256 while (e != head) {
257 struct cond_event_entry *next = e->next;
258 struct cond_event_entry *prev = e->prev;
259
260 SetEvent(e->event);
261
262 prev->next = next;
263 next->prev = prev;
264 e->next = e->prev = e;
265
266 e = next;
267 }
268}
269
270// An armed timer, if given, ends the wait the way msec running out does.
271static int
272native_cond_timedwait_ms(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex, HANDLE timer, unsigned long msec)
273{
274 DWORD r;
275 struct cond_event_entry entry;
276 struct cond_event_entry *head = (struct cond_event_entry*)cond;
277
278 entry.event = CreateEvent(0, FALSE, FALSE, 0);
279
280 /* cond is guarded by mutex */
281 entry.next = head;
282 entry.prev = head->prev;
283 head->prev->next = &entry;
284 head->prev = &entry;
285
287 {
288 HANDLE events[2] = {entry.event, timer};
289 r = WaitForMultipleObjects(timer ? 2 : 1, events, FALSE, msec);
290 if (r == WAIT_OBJECT_0 + 1) r = WAIT_TIMEOUT;
291 if ((r != WAIT_OBJECT_0) && (r != WAIT_TIMEOUT)) {
292 rb_bug("rb_native_cond_wait: WaitForMultipleObjects returns %lu", r);
293 }
294 }
296
297 entry.prev->next = entry.next;
298 entry.next->prev = entry.prev;
299
300 w32_close_handle(entry.event);
301 return (r == WAIT_OBJECT_0) ? 0 : ETIMEDOUT;
302}
303
304void
305rb_native_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex)
306{
307 native_cond_timedwait_ms(cond, mutex, NULL, INFINITE);
308}
309
310void
311rb_native_cond_timedwait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex, unsigned long msec)
312{
313 native_cond_timedwait_ms(cond, mutex, NULL, msec);
314}
315
316// The scheduler parks threads with an absolute deadline; on this platform the
317// wait itself is relative, so the conversion happens here.
318static rb_hrtime_t
319native_cond_timeout(rb_nativethread_cond_t *cond, const rb_hrtime_t rel)
320{
321 if (rel > 0) {
322 rb_hrtime_t now = rb_hrtime_now();
323 return (rel > RB_HRTIME_MAX - now) ? RB_HRTIME_MAX : now + rel;
324 }
325 return rb_hrtime_now();
326}
327
328static int
329native_cond_timedwait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex, const rb_hrtime_t *abs)
330{
331 rb_hrtime_t now = rb_hrtime_now();
332
333 if (*abs <= now) return ETIMEDOUT;
334
335 rb_hrtime_t rel = *abs - now;
336 HANDLE timer = ruby_thread_from_native()->nt->wait_timer;
337
338 if (timer) {
339 // a negative due time is relative, in 100ns units
340 LARGE_INTEGER due;
341 due.QuadPart = -(LONGLONG)roomof(rel, 100);
342 if (!SetWaitableTimer(timer, &due, 0, NULL, NULL, FALSE)) {
343 w32_error("native_cond_timedwait");
344 }
345 return native_cond_timedwait_ms(cond, mutex, timer, INFINITE);
346 }
347
348 unsigned long msec = (unsigned long)(rel / RB_HRTIME_PER_MSEC);
349
350 // do not busy loop on a sub-millisecond deadline
351 if (msec == 0) msec = 1;
352
353 return native_cond_timedwait_ms(cond, mutex, NULL, msec);
354}
355
356void
357rb_native_cond_initialize(rb_nativethread_cond_t *cond)
358{
359 cond->next = (struct cond_event_entry *)cond;
360 cond->prev = (struct cond_event_entry *)cond;
361}
362
363void
364rb_native_cond_destroy(rb_nativethread_cond_t *cond)
365{
366 /* */
367}
368
369/* -------------------------------------------------------------------------
370 * thread local storage
371 * ------------------------------------------------------------------------- */
372
374ruby_thread_from_native(void)
375{
376 return TlsGetValue(ruby_native_thread_key);
377}
378
379int
380ruby_thread_set_native(rb_thread_t *th)
381{
382 if (th) {
383 ccan_list_node_init(&th->sched.node.ubf);
384 }
385
386 if (th && th->ec) {
387 rb_ractor_set_current_ec(th->ractor, th->ec);
388 }
389 return TlsSetValue(ruby_native_thread_key, th);
390}
391
392/* -------------------------------------------------------------------------
393 * waiting on Windows objects, with interruption
394 * ------------------------------------------------------------------------- */
395
396static int
397w32_wait_events(HANDLE *events, int count, DWORD timeout, rb_thread_t *th)
398{
399 HANDLE *targets = events;
400 HANDLE intr;
401 const int initcount = count;
402 DWORD ret;
403
404 w32_event_debug("events:%p, count:%d, timeout:%ld, th:%u\n",
405 events, count, timeout, th ? rb_th_serial(th) : UINT_MAX);
406
407 if (th && (intr = th->nt->interrupt_event)) {
408 if (ResetEvent(intr) && (!RUBY_VM_INTERRUPTED(th->ec) || SetEvent(intr))) {
409 targets = ALLOCA_N(HANDLE, count + 1);
410 memcpy(targets, events, sizeof(HANDLE) * count);
411
412 targets[count++] = intr;
413 w32_event_debug("handle:%p (count:%d, intr)\n", intr, count);
414 }
415 else if (intr == th->nt->interrupt_event) {
416 w32_error("w32_wait_events");
417 }
418 }
419
420 w32_event_debug("WaitForMultipleObjects start count:%d\n", count);
421 ret = WaitForMultipleObjects(count, targets, FALSE, timeout);
422 w32_event_debug("WaitForMultipleObjects end ret:%lu\n", ret);
423
424 if (ret == (DWORD)(WAIT_OBJECT_0 + initcount) && th) {
425 errno = EINTR;
426 }
427 if (ret == WAIT_FAILED && W32_EVENT_DEBUG) {
428 int i;
429 DWORD dmy;
430 for (i = 0; i < count; i++) {
431 w32_event_debug("i:%d %s\n", i, GetHandleInformation(targets[i], &dmy) ? "OK" : "NG");
432 }
433 }
434 return ret;
435}
436
437int
438rb_w32_wait_events_blocking(HANDLE *events, int num, DWORD timeout)
439{
440 return w32_wait_events(events, num, timeout, ruby_thread_from_native());
441}
442
443int
444rb_w32_wait_events(HANDLE *events, int num, DWORD timeout)
445{
446 int ret;
447 rb_thread_t *th = GET_THREAD();
448
449 BLOCKING_REGION(th, ret = rb_w32_wait_events_blocking(events, num, timeout),
450 ubf_select, ruby_thread_from_native(), FALSE);
451 return ret;
452}
453
454int
455rb_w32_sleep(unsigned long msec)
456{
457 return w32_wait_events(0, 0, msec, ruby_thread_from_native());
458}
459
460int WINAPI
461rb_w32_Sleep(unsigned long msec)
462{
463 int ret;
464 rb_thread_t *th = GET_THREAD();
465
466 BLOCKING_REGION(th, ret = rb_w32_sleep(msec),
467 ubf_select, ruby_thread_from_native(), FALSE);
468 return ret;
469}
470
471/* @internal */
472int
473rb_w32_check_interrupt(rb_thread_t *th)
474{
475 return w32_wait_events(0, 0, 0, th);
476}
477
478/*
479 * Pull the target thread out of a blocking w32_wait_events(). This is the
480 * counterpart of the SIGVTALRM the POSIX implementation sends.
481 */
482static void
483native_thread_interrupt(rb_thread_t *th)
484{
485 // the caller (ubf_wakeup_thread) logs this
486 if (!SetEvent(th->nt->interrupt_event)) {
487 w32_error("native_thread_interrupt");
488 }
489}
490
491/* -------------------------------------------------------------------------
492 * native thread
493 * ------------------------------------------------------------------------- */
494
495static void
496w32_resume_thread(HANDLE handle)
497{
498 if (ResumeThread(handle) == (DWORD)-1) {
499 w32_error("w32_resume_thread");
500 }
501}
502
503#ifdef _MSC_VER
504#define HAVE__BEGINTHREADEX 1
505#else
506#undef HAVE__BEGINTHREADEX
507#endif
508
509#ifdef HAVE__BEGINTHREADEX
510#define start_thread (HANDLE)_beginthreadex
511#define thread_errno errno
512typedef unsigned w32_thread_start_ret;
513typedef w32_thread_start_ret (__stdcall *w32_thread_start_func)(void*);
514#else
515#define start_thread CreateThread
516#define thread_errno rb_w32_map_errno(GetLastError())
517typedef DWORD w32_thread_start_ret;
518typedef LPTHREAD_START_ROUTINE w32_thread_start_func;
519#endif
520
521static HANDLE
522w32_create_thread(DWORD stack_size, w32_thread_start_func func, void *val)
523{
524 return start_thread(0, stack_size, func, val, CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION, 0);
525}
526
527static void
528native_thread_join(HANDLE th)
529{
530 w32_wait_events(&th, 1, INFINITE, 0);
531}
532
533#if !defined(_WIN32_WINNT_WIN8) || _WIN32_WINNT < 0x602
534/* declared in processthreadsapi.h only when _WIN32_WINNT >= 0x0602,
535 * but exported from kernel32.dll since Windows 8 */
536WINBASEAPI VOID WINAPI GetCurrentThreadStackLimits(PULONG_PTR, PULONG_PTR);
537#endif
538
539static void
540native_thread_init_stack(rb_thread_t *th, void *local_in_parent_frame)
541{
542 ULONG_PTR low, high;
543 SIZE_T size, space;
544
545 /* VirtualQuery against the current stack pointer may return a region
546 * that does not span the whole stack when the interpreter is
547 * initialized deep in the stack, which makes stack_check() misfire.
548 * [Bug #11438] */
549 GetCurrentThreadStackLimits(&low, &high);
550 size = high - low;
551 space = size / 5;
552 if (space > 1024*1024) space = 1024*1024;
553 th->ec->machine.stack_start = (VALUE *)high - 1;
554 th->ec->machine.stack_maxsize = size - space;
555}
556
557static void
558native_thread_setup(struct rb_native_thread *nt)
559{
560 rb_native_cond_initialize(&nt->readyq);
561 rb_native_mutex_initialize(&nt->running_th_lock);
562
563 // Created here rather than on the new thread itself: ubf can fire before
564 // that thread gets a chance to run.
565 nt->interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
566 if (nt->interrupt_event == NULL) {
567 w32_error("native_thread_setup");
568 }
569
570#if _WIN32_WINNT >= 0x0A00 && defined(CREATE_WAITABLE_TIMER_HIGH_RESOLUTION)
571 // The headers define the flag for any target version, but only Windows
572 // 10 1803 and later accept it. Failing to create the timer only leaves
573 // timed waits on the millisecond timeout.
574 nt->wait_timer = CreateWaitableTimerExW(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION,
575 TIMER_MODIFY_STATE | SYNCHRONIZE);
576#endif
577}
578
579static void
580native_thread_setup_on_thread(struct rb_native_thread *nt)
581{
582 // nothing to do: there is no altstack and no thread id to cache
583}
584
585static struct rb_native_thread *
586native_thread_alloc(void)
587{
588 struct rb_native_thread *nt = ZALLOC(struct rb_native_thread);
589 native_thread_setup(nt);
590
591#if USE_RUBY_DEBUG_LOG
592 static rb_atomic_t nt_serial = 2;
593 nt->serial = RUBY_ATOMIC_FETCH_ADD(nt_serial, 1);
594#endif
595 return nt;
596}
597
598static void
599native_thread_destroy_atfork(struct rb_native_thread *nt)
600{
601 /* no fork() on this platform */
602}
603
604#ifndef InterlockedExchangePointer
605#define InterlockedExchangePointer(t, v) \
606 (void *)InterlockedExchange((long *)(t), (long)(v))
607#endif
608
609static void
610native_thread_destroy(struct rb_native_thread *nt)
611{
612 if (nt) {
613 HANDLE intr = InterlockedExchangePointer(&nt->interrupt_event, 0);
614 RUBY_DEBUG_LOG("close handle intr:%p, thid:%p\n", intr, nt->thread_id);
615 if (intr) w32_close_handle(intr);
616 if (nt->wait_timer) w32_close_handle(nt->wait_timer);
617
618 rb_native_cond_destroy(&nt->readyq);
619 rb_native_mutex_destroy(&nt->running_th_lock);
620
621 ruby_xfree(nt);
622 }
623}
624
625static void
626native_thread_destroy_self(struct rb_native_thread *nt)
627{
628 native_thread_destroy(nt);
629}
630
631static w32_thread_start_ret __stdcall
632nt_start_trampoline(void *nt_ptr)
633{
634 struct rb_native_thread *nt = (struct rb_native_thread *)nt_ptr;
635 HANDLE thread_id = nt->thread_id;
636
637 nt_start(nt);
638
639 w32_close_handle(thread_id);
640 return 0;
641}
642
643static int
644native_thread_create0(struct rb_native_thread *nt)
645{
646 const size_t stack_size = nt->vm->default_params.thread_machine_stack_size;
647
648 nt->thread_id = w32_create_thread(stack_size, nt_start_trampoline, nt);
649 if (nt->thread_id == 0) {
650 return thread_errno;
651 }
652
653 w32_resume_thread(nt->thread_id);
654
655 RUBY_DEBUG_LOG("nt:%u thid:%p stack size:%"PRIuSIZE"",
656 nt->serial, nt->thread_id, stack_size);
657 return 0;
658}
659
660static int
661native_thread_default_max_cpu(void)
662{
663 SYSTEM_INFO si;
664 GetSystemInfo(&si);
665 return si.dwNumberOfProcessors > 0 ? (int)si.dwNumberOfProcessors : 8;
666}
667
668#if USE_NATIVE_THREAD_PRIORITY
669
670static void
671native_thread_apply_priority(rb_thread_t *th)
672{
673 int priority = th->priority;
674 if (th->priority > 0) {
675 priority = THREAD_PRIORITY_ABOVE_NORMAL;
676 }
677 else if (th->priority < 0) {
678 priority = THREAD_PRIORITY_BELOW_NORMAL;
679 }
680 else {
681 priority = THREAD_PRIORITY_NORMAL;
682 }
683
684 SetThreadPriority(th->nt->thread_id, priority);
685}
686
687#endif /* USE_NATIVE_THREAD_PRIORITY */
688
689int rb_w32_select_with_thread(int, fd_set *, fd_set *, fd_set *, struct timeval *, void *); /* @internal */
690
691static int
692native_fd_select(int n, rb_fdset_t *readfds, rb_fdset_t *writefds, rb_fdset_t *exceptfds, struct timeval *timeout, rb_thread_t *th)
693{
694 fd_set *r = NULL, *w = NULL, *e = NULL;
695 if (readfds) {
696 rb_fd_resize(n - 1, readfds);
697 r = rb_fd_ptr(readfds);
698 }
699 if (writefds) {
700 rb_fd_resize(n - 1, writefds);
701 w = rb_fd_ptr(writefds);
702 }
703 if (exceptfds) {
704 rb_fd_resize(n - 1, exceptfds);
705 e = rb_fd_ptr(exceptfds);
706 }
707 return rb_w32_select_with_thread(n, r, w, e, timeout, th);
708}
709
710int rb_w32_set_thread_description(HANDLE th, const WCHAR *name);
711int rb_w32_set_thread_description_str(HANDLE th, VALUE name);
712#define native_set_another_thread_name rb_w32_set_thread_description_str
713
714static void
715native_set_thread_name(rb_thread_t *th)
716{
717}
718
719static VALUE
720native_thread_native_thread_id(rb_thread_t *th)
721{
722 DWORD tid = GetThreadId(th->nt->thread_id);
723 if (tid == 0) rb_sys_fail("GetThreadId");
724 return ULONG2NUM(tid);
725}
726#define USE_NATIVE_THREAD_NATIVE_THREAD_ID 1
727
728void
729Init_native_thread(rb_thread_t *main_th)
730{
731 if ((ruby_current_ec_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) {
732 rb_bug("TlsAlloc() for ruby_current_ec_key fails");
733 }
734 if ((ruby_native_thread_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) {
735 rb_bug("TlsAlloc() for ruby_native_thread_key fails");
736 }
737
738 // setup vm
739 rb_vm_t *vm = main_th->vm;
740 thread_sched_init_vm(vm);
741
742 // setup main thread
743 native_thread_setup(main_th->nt);
744 DuplicateHandle(GetCurrentProcess(),
745 GetCurrentThread(),
746 GetCurrentProcess(),
747 &main_th->nt->thread_id, 0, FALSE, DUPLICATE_SAME_ACCESS);
748 main_th->nt->serial = 1;
749 ruby_thread_set_native(main_th);
750
751 TH_SCHED(main_th)->running = main_th;
752 main_th->has_dedicated_nt = 1;
753
754 // setup main NT (before the record below: its kind decides where it goes)
755 main_th->nt->dedicated = 1;
756 main_th->nt->running_thread = main_th;
757 main_th->nt->vm = vm;
758
759 thread_sched_setup_running_threads(TH_SCHED(main_th), main_th->ractor, vm, main_th, NULL);
760
761#if USE_RUBY_DEBUG_LOG
762 vm->ractor.sched.dnt_cnt = 1;
763#endif
764
765 RUBY_DEBUG_LOG("initial thread th:%u thid:%p, event: %p",
766 rb_th_serial(main_th),
767 main_th->nt->thread_id,
768 main_th->nt->interrupt_event);
769}
770
771/* -------------------------------------------------------------------------
772 * timer thread
773 * ------------------------------------------------------------------------- */
774
775static struct {
776 rb_serial_t created_fork_gen;
777 HANDLE thread_id;
778 HANDLE wakeup_event; // manual reset; the "comm pipe" of this platform
779} timer_th = {
780 .created_fork_gen = 0,
781};
782
783#define TIMER_THREAD_CREATED_P() (timer_th.created_fork_gen == current_fork_gen)
784
785static void
786timer_thread_wakeup_force(void)
787{
788 if (timer_th.wakeup_event) {
789 SetEvent(timer_th.wakeup_event);
790 }
791}
792
793void
794rb_thread_wakeup_timer_thread(int sig)
795{
796 timer_thread_wakeup_force();
797
798 if (RUBY_ATOMIC_LOAD(system_working)) {
799 rb_vm_t *vm = GET_VM();
800 rb_thread_t *main_th = vm->ractor.main_thread;
801
802 if (main_th) {
803 volatile rb_execution_context_t *main_th_ec = ACCESS_ONCE(rb_execution_context_t *, main_th->ec);
804
805 if (main_th_ec) {
806 RUBY_VM_SET_TRAP_INTERRUPT(main_th_ec);
807
808 if (vm->ubf_async_safe && main_th->unblock.func) {
809 (main_th->unblock.func)(main_th->unblock.arg);
810 }
811 }
812 }
813 }
814}
815
816// The blocking part of the timer thread loop: this is what
817// timer_thread_wakeup_force() interrupts.
818static void
819timer_thread_polling(rb_vm_t *vm)
820{
821 int timeout = timer_thread_set_timeout(vm);
822 DWORD msec = (timeout < 0) ? INFINITE : (DWORD)timeout;
823
824 DWORD ret = WaitForSingleObject(timer_th.wakeup_event, msec);
825
826 switch (ret) {
827 case WAIT_TIMEOUT:
828 ractor_sched_lock(vm, NULL);
829 {
830 timer_thread_check_timeslice(vm);
831 }
832 ractor_sched_unlock(vm, NULL);
833 break;
834
835 case WAIT_OBJECT_0:
836 ResetEvent(timer_th.wakeup_event);
837 break;
838
839 default:
840 w32_error("timer_thread_polling");
841 }
842}
843
844static w32_thread_start_ret __stdcall
845timer_thread_trampoline(void *vm_ptr)
846{
847 rb_w32_set_thread_description(GetCurrentThread(), L"ruby-timer-thread");
848 timer_thread_func(vm_ptr);
849 return 0;
850}
851
852static void
853rb_thread_create_timer_thread(void)
854{
855 timer_th.created_fork_gen = current_fork_gen;
856
857 if (timer_th.wakeup_event == NULL) {
858 timer_th.wakeup_event = CreateEvent(0, TRUE, FALSE, 0);
859 if (timer_th.wakeup_event == NULL) {
860 w32_error("rb_thread_create_timer_thread");
861 }
862 }
863
864 timer_th.thread_id = w32_create_thread(1024 + (USE_RUBY_DEBUG_LOG ? BUFSIZ : 0),
865 timer_thread_trampoline, GET_VM());
866 if (timer_th.thread_id == 0) {
867 rb_bug("rb_thread_create_timer_thread: failed to create the timer thread");
868 }
869 w32_resume_thread(timer_th.thread_id);
870}
871
872static int
873native_stop_timer_thread(void)
874{
875 RUBY_ATOMIC_SET(system_working, 0);
876
877 timer_thread_wakeup_force();
878 native_thread_join(timer_th.thread_id);
879
880 w32_close_handle(timer_th.wakeup_event);
881 timer_th.wakeup_event = NULL;
882
883 return 1;
884}
885
886static void
887native_reset_timer_thread(void)
888{
889 if (timer_th.thread_id) {
890 CloseHandle(timer_th.thread_id);
891 timer_th.thread_id = 0;
892 }
893}
894
895/* -------------------------------------------------------------------------
896 * M:N scheduler stubs
897 *
898 * These are what thread_sched_mn.c provides on platforms that have an event
899 * backend. Every thread here is dedicated, so the scheduler never reaches
900 * the ones that rb_bug().
901 * ------------------------------------------------------------------------- */
902
903static bool
904native_thread_self_can_retire_p(void)
905{
906 return true;
907}
908
909static int
910native_thread_create_shared(rb_thread_t *th)
911{
912 rb_bug("unreachable");
913}
914
915static enum thread_sched_wait_result
916thread_sched_wait_events(struct rb_thread_sched *sched, rb_thread_t *th, int fd,
917 enum thread_sched_waiting_flag events, rb_hrtime_t *rel)
918{
919 return thread_sched_wait_unavailable;
920}
921
922static bool
923ractor_sched_timeout_arm(rb_thread_t *th, const rb_hrtime_t *rel)
924{
925 rb_bug("unreachable");
926}
927
928static bool
929ractor_sched_timeout_disarm(rb_thread_t *th)
930{
931 rb_bug("unreachable");
932}
933
934static int
935timer_wheel_timeout(int timeout)
936{
937 return timeout; // no M:N threads, no timed waiters
938}
939
940static void
941timer_thread_wake_fence(rb_thread_t *th)
942{
943 // no timer wheel, no wake batches
944}
945
946static void
947timer_thread_check_timeout(rb_vm_t *vm)
948{
949 // no M:N threads, no timed waiters
950}
951
952/* -------------------------------------------------------------------------
953 * misc
954 * ------------------------------------------------------------------------- */
955
956int
957ruby_stack_overflowed_p(const rb_thread_t *th, const void *addr)
958{
959 return rb_ec_raised_p(th->ec, RAISED_STACKOVERFLOW);
960}
961
962#if defined(__MINGW32__)
963LONG WINAPI
964rb_w32_stack_overflow_handler(struct _EXCEPTION_POINTERS *exception)
965{
966 if (exception->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) {
967 rb_ec_raised_set(GET_EC(), RAISED_STACKOVERFLOW);
968 raise(SIGSEGV);
969 }
970 return EXCEPTION_CONTINUE_SEARCH;
971}
972#endif
973
974#ifdef RUBY_ALLOCA_CHKSTK
975void
976ruby_alloca_chkstk(size_t len, void *sp)
977{
978 if (ruby_stack_length(NULL) * sizeof(VALUE) >= len) {
979 rb_execution_context_t *ec = GET_EC();
980 if (!rb_ec_raised_p(ec, RAISED_STACKOVERFLOW)) {
981 rb_ec_raised_set(ec, RAISED_STACKOVERFLOW);
982 rb_exc_raise(sysstack_error);
983 }
984 }
985}
986#endif
987
988int
989rb_reserved_fd_p(int fd)
990{
991 return 0;
992}
993
994rb_nativethread_id_t
996{
997 return GetCurrentThread();
998}
999
1000void *
1001rb_thread_prevent_fork(void *(*func)(void *), void *data)
1002{
1003 return func(data);
1004}
1005
1006void
1007rb_thread_register_exiting(void)
1008{
1009 // nothing to do: there is no fork
1010}
1011
1012#endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
#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...
Definition atomic.h:118
#define RUBY_ATOMIC_LOAD(var)
Atomic load.
Definition atomic.h:175
#define RUBY_ATOMIC_SET(var, val)
Identical to RUBY_ATOMIC_EXCHANGE, except for the return type.
Definition atomic.h:185
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:108
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
Definition assume.h:28
#define ULONG2NUM
Old name of RB_ULONG2NUM.
Definition long.h:60
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:402
size_t ruby_stack_length(VALUE **p)
Queries what Ruby thinks is the machine stack.
Definition gc.c:2868
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:678
int rb_reserved_fd_p(int fd)
Queries if the given FD is reserved or not.
int len
Length of the buffer.
Definition io.h:8
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.
Definition largesize.h:195
#define ALLOCA_N(type, n)
Definition memory.h:292
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
Definition noreturn.h:38
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
#define rb_fd_resize(n, f)
Does nothing (defined for compatibility).
Definition select.h:43
The data structure which wraps the fd_set bitmap used by select(2).
Definition largesize.h:71
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.
Definition value.h:40