Ruby 4.1.0dev (2026-09-30 revision 696ebaad23e46382f04235e9663767ab644795b4)
thread_win32.c (696ebaad23e46382f04235e9663767ab644795b4)
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 long (__stdcall *w32_thread_start_func)(void*);
513#else
514#define start_thread CreateThread
515#define thread_errno rb_w32_map_errno(GetLastError())
516typedef LPTHREAD_START_ROUTINE w32_thread_start_func;
517#endif
518
519static HANDLE
520w32_create_thread(DWORD stack_size, w32_thread_start_func func, void *val)
521{
522 return start_thread(0, stack_size, func, val, CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION, 0);
523}
524
525static void
526native_thread_join(HANDLE th)
527{
528 w32_wait_events(&th, 1, INFINITE, 0);
529}
530
531#if !defined(_WIN32_WINNT_WIN8) || _WIN32_WINNT < 0x602
532/* declared in processthreadsapi.h only when _WIN32_WINNT >= 0x0602,
533 * but exported from kernel32.dll since Windows 8 */
534WINBASEAPI VOID WINAPI GetCurrentThreadStackLimits(PULONG_PTR, PULONG_PTR);
535#endif
536
537static void
538native_thread_init_stack(rb_thread_t *th, void *local_in_parent_frame)
539{
540 ULONG_PTR low, high;
541 SIZE_T size, space;
542
543 /* VirtualQuery against the current stack pointer may return a region
544 * that does not span the whole stack when the interpreter is
545 * initialized deep in the stack, which makes stack_check() misfire.
546 * [Bug #11438] */
547 GetCurrentThreadStackLimits(&low, &high);
548 size = high - low;
549 space = size / 5;
550 if (space > 1024*1024) space = 1024*1024;
551 th->ec->machine.stack_start = (VALUE *)high - 1;
552 th->ec->machine.stack_maxsize = size - space;
553}
554
555static void
556native_thread_setup(struct rb_native_thread *nt)
557{
558 rb_native_cond_initialize(&nt->readyq);
559 rb_native_mutex_initialize(&nt->running_th_lock);
560
561 // Created here rather than on the new thread itself: ubf can fire before
562 // that thread gets a chance to run.
563 nt->interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
564 if (nt->interrupt_event == NULL) {
565 w32_error("native_thread_setup");
566 }
567
568#if _WIN32_WINNT >= 0x0A00 && defined(CREATE_WAITABLE_TIMER_HIGH_RESOLUTION)
569 // The headers define the flag for any target version, but only Windows
570 // 10 1803 and later accept it. Failing to create the timer only leaves
571 // timed waits on the millisecond timeout.
572 nt->wait_timer = CreateWaitableTimerExW(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION,
573 TIMER_MODIFY_STATE | SYNCHRONIZE);
574#endif
575}
576
577static void
578native_thread_setup_on_thread(struct rb_native_thread *nt)
579{
580 // nothing to do: there is no altstack and no thread id to cache
581}
582
583static struct rb_native_thread *
584native_thread_alloc(void)
585{
586 struct rb_native_thread *nt = ZALLOC(struct rb_native_thread);
587 native_thread_setup(nt);
588
589#if USE_RUBY_DEBUG_LOG
590 static rb_atomic_t nt_serial = 2;
591 nt->serial = RUBY_ATOMIC_FETCH_ADD(nt_serial, 1);
592#endif
593 return nt;
594}
595
596static void
597native_thread_destroy_atfork(struct rb_native_thread *nt)
598{
599 /* no fork() on this platform */
600}
601
602#ifndef InterlockedExchangePointer
603#define InterlockedExchangePointer(t, v) \
604 (void *)InterlockedExchange((long *)(t), (long)(v))
605#endif
606
607static void
608native_thread_destroy(struct rb_native_thread *nt)
609{
610 if (nt) {
611 HANDLE intr = InterlockedExchangePointer(&nt->interrupt_event, 0);
612 RUBY_DEBUG_LOG("close handle intr:%p, thid:%p\n", intr, nt->thread_id);
613 if (intr) w32_close_handle(intr);
614 if (nt->wait_timer) w32_close_handle(nt->wait_timer);
615
616 rb_native_cond_destroy(&nt->readyq);
617 rb_native_mutex_destroy(&nt->running_th_lock);
618
619 ruby_xfree(nt);
620 }
621}
622
623static void
624native_thread_destroy_self(struct rb_native_thread *nt)
625{
626 native_thread_destroy(nt);
627}
628
629static unsigned long __stdcall
630nt_start_trampoline(void *nt_ptr)
631{
632 struct rb_native_thread *nt = (struct rb_native_thread *)nt_ptr;
633 HANDLE thread_id = nt->thread_id;
634
635 nt_start(nt);
636
637 w32_close_handle(thread_id);
638 return 0;
639}
640
641static int
642native_thread_create0(struct rb_native_thread *nt)
643{
644 const size_t stack_size = nt->vm->default_params.thread_machine_stack_size;
645
646 nt->thread_id = w32_create_thread(stack_size, nt_start_trampoline, nt);
647 if (nt->thread_id == 0) {
648 return thread_errno;
649 }
650
651 w32_resume_thread(nt->thread_id);
652
653 RUBY_DEBUG_LOG("nt:%u thid:%p stack size:%"PRIuSIZE"",
654 nt->serial, nt->thread_id, stack_size);
655 return 0;
656}
657
658static int
659native_thread_default_max_cpu(void)
660{
661 SYSTEM_INFO si;
662 GetSystemInfo(&si);
663 return si.dwNumberOfProcessors > 0 ? (int)si.dwNumberOfProcessors : 8;
664}
665
666#if USE_NATIVE_THREAD_PRIORITY
667
668static void
669native_thread_apply_priority(rb_thread_t *th)
670{
671 int priority = th->priority;
672 if (th->priority > 0) {
673 priority = THREAD_PRIORITY_ABOVE_NORMAL;
674 }
675 else if (th->priority < 0) {
676 priority = THREAD_PRIORITY_BELOW_NORMAL;
677 }
678 else {
679 priority = THREAD_PRIORITY_NORMAL;
680 }
681
682 SetThreadPriority(th->nt->thread_id, priority);
683}
684
685#endif /* USE_NATIVE_THREAD_PRIORITY */
686
687int rb_w32_select_with_thread(int, fd_set *, fd_set *, fd_set *, struct timeval *, void *); /* @internal */
688
689static int
690native_fd_select(int n, rb_fdset_t *readfds, rb_fdset_t *writefds, rb_fdset_t *exceptfds, struct timeval *timeout, rb_thread_t *th)
691{
692 fd_set *r = NULL, *w = NULL, *e = NULL;
693 if (readfds) {
694 rb_fd_resize(n - 1, readfds);
695 r = rb_fd_ptr(readfds);
696 }
697 if (writefds) {
698 rb_fd_resize(n - 1, writefds);
699 w = rb_fd_ptr(writefds);
700 }
701 if (exceptfds) {
702 rb_fd_resize(n - 1, exceptfds);
703 e = rb_fd_ptr(exceptfds);
704 }
705 return rb_w32_select_with_thread(n, r, w, e, timeout, th);
706}
707
708int rb_w32_set_thread_description(HANDLE th, const WCHAR *name);
709int rb_w32_set_thread_description_str(HANDLE th, VALUE name);
710#define native_set_another_thread_name rb_w32_set_thread_description_str
711
712static void
713native_set_thread_name(rb_thread_t *th)
714{
715}
716
717static VALUE
718native_thread_native_thread_id(rb_thread_t *th)
719{
720 DWORD tid = GetThreadId(th->nt->thread_id);
721 if (tid == 0) rb_sys_fail("GetThreadId");
722 return ULONG2NUM(tid);
723}
724#define USE_NATIVE_THREAD_NATIVE_THREAD_ID 1
725
726void
727Init_native_thread(rb_thread_t *main_th)
728{
729 if ((ruby_current_ec_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) {
730 rb_bug("TlsAlloc() for ruby_current_ec_key fails");
731 }
732 if ((ruby_native_thread_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) {
733 rb_bug("TlsAlloc() for ruby_native_thread_key fails");
734 }
735
736 // setup vm
737 rb_vm_t *vm = main_th->vm;
738 thread_sched_init_vm(vm);
739
740 // setup main thread
741 native_thread_setup(main_th->nt);
742 DuplicateHandle(GetCurrentProcess(),
743 GetCurrentThread(),
744 GetCurrentProcess(),
745 &main_th->nt->thread_id, 0, FALSE, DUPLICATE_SAME_ACCESS);
746 main_th->nt->serial = 1;
747 ruby_thread_set_native(main_th);
748
749 TH_SCHED(main_th)->running = main_th;
750 main_th->has_dedicated_nt = 1;
751
752 // setup main NT (before the record below: its kind decides where it goes)
753 main_th->nt->dedicated = 1;
754 main_th->nt->running_thread = main_th;
755 main_th->nt->vm = vm;
756
757 thread_sched_setup_running_threads(TH_SCHED(main_th), main_th->ractor, vm, main_th, NULL);
758
759#if USE_RUBY_DEBUG_LOG
760 vm->ractor.sched.dnt_cnt = 1;
761#endif
762
763 RUBY_DEBUG_LOG("initial thread th:%u thid:%p, event: %p",
764 rb_th_serial(main_th),
765 main_th->nt->thread_id,
766 main_th->nt->interrupt_event);
767}
768
769/* -------------------------------------------------------------------------
770 * timer thread
771 * ------------------------------------------------------------------------- */
772
773static struct {
774 rb_serial_t created_fork_gen;
775 HANDLE thread_id;
776 HANDLE wakeup_event; // manual reset; the "comm pipe" of this platform
777} timer_th = {
778 .created_fork_gen = 0,
779};
780
781#define TIMER_THREAD_CREATED_P() (timer_th.created_fork_gen == current_fork_gen)
782
783static void
784timer_thread_wakeup_force(void)
785{
786 if (timer_th.wakeup_event) {
787 SetEvent(timer_th.wakeup_event);
788 }
789}
790
791void
792rb_thread_wakeup_timer_thread(int sig)
793{
794 timer_thread_wakeup_force();
795
796 if (RUBY_ATOMIC_LOAD(system_working)) {
797 rb_vm_t *vm = GET_VM();
798 rb_thread_t *main_th = vm->ractor.main_thread;
799
800 if (main_th) {
801 volatile rb_execution_context_t *main_th_ec = ACCESS_ONCE(rb_execution_context_t *, main_th->ec);
802
803 if (main_th_ec) {
804 RUBY_VM_SET_TRAP_INTERRUPT(main_th_ec);
805
806 if (vm->ubf_async_safe && main_th->unblock.func) {
807 (main_th->unblock.func)(main_th->unblock.arg);
808 }
809 }
810 }
811 }
812}
813
814// The blocking part of the timer thread loop: this is what
815// timer_thread_wakeup_force() interrupts.
816static void
817timer_thread_polling(rb_vm_t *vm)
818{
819 int timeout = timer_thread_set_timeout(vm);
820 DWORD msec = (timeout < 0) ? INFINITE : (DWORD)timeout;
821
822 DWORD ret = WaitForSingleObject(timer_th.wakeup_event, msec);
823
824 switch (ret) {
825 case WAIT_TIMEOUT:
826 ractor_sched_lock(vm, NULL);
827 {
828 timer_thread_check_timeslice(vm);
829 }
830 ractor_sched_unlock(vm, NULL);
831 break;
832
833 case WAIT_OBJECT_0:
834 ResetEvent(timer_th.wakeup_event);
835 break;
836
837 default:
838 w32_error("timer_thread_polling");
839 }
840}
841
842static unsigned long __stdcall
843timer_thread_trampoline(void *vm_ptr)
844{
845 rb_w32_set_thread_description(GetCurrentThread(), L"ruby-timer-thread");
846 timer_thread_func(vm_ptr);
847 return 0;
848}
849
850static void
851rb_thread_create_timer_thread(void)
852{
853 timer_th.created_fork_gen = current_fork_gen;
854
855 if (timer_th.wakeup_event == NULL) {
856 timer_th.wakeup_event = CreateEvent(0, TRUE, FALSE, 0);
857 if (timer_th.wakeup_event == NULL) {
858 w32_error("rb_thread_create_timer_thread");
859 }
860 }
861
862 timer_th.thread_id = w32_create_thread(1024 + (USE_RUBY_DEBUG_LOG ? BUFSIZ : 0),
863 timer_thread_trampoline, GET_VM());
864 if (timer_th.thread_id == 0) {
865 rb_bug("rb_thread_create_timer_thread: failed to create the timer thread");
866 }
867 w32_resume_thread(timer_th.thread_id);
868}
869
870static int
871native_stop_timer_thread(void)
872{
873 RUBY_ATOMIC_SET(system_working, 0);
874
875 timer_thread_wakeup_force();
876 native_thread_join(timer_th.thread_id);
877
878 w32_close_handle(timer_th.wakeup_event);
879 timer_th.wakeup_event = NULL;
880
881 return 1;
882}
883
884static void
885native_reset_timer_thread(void)
886{
887 if (timer_th.thread_id) {
888 CloseHandle(timer_th.thread_id);
889 timer_th.thread_id = 0;
890 }
891}
892
893/* -------------------------------------------------------------------------
894 * M:N scheduler stubs
895 *
896 * These are what thread_sched_mn.c provides on platforms that have an event
897 * backend. Every thread here is dedicated, so the scheduler never reaches
898 * the ones that rb_bug().
899 * ------------------------------------------------------------------------- */
900
901static bool
902native_thread_self_can_retire_p(void)
903{
904 return true;
905}
906
907static int
908native_thread_create_shared(rb_thread_t *th)
909{
910 rb_bug("unreachable");
911}
912
913static enum thread_sched_wait_result
914thread_sched_wait_events(struct rb_thread_sched *sched, rb_thread_t *th, int fd,
915 enum thread_sched_waiting_flag events, rb_hrtime_t *rel)
916{
917 return thread_sched_wait_unavailable;
918}
919
920static bool
921ractor_sched_timeout_arm(rb_thread_t *th, const rb_hrtime_t *rel)
922{
923 rb_bug("unreachable");
924}
925
926static bool
927ractor_sched_timeout_disarm(rb_thread_t *th)
928{
929 rb_bug("unreachable");
930}
931
932static int
933timer_wheel_timeout(int timeout)
934{
935 return timeout; // no M:N threads, no timed waiters
936}
937
938static void
939timer_thread_wake_fence(rb_thread_t *th)
940{
941 // no timer wheel, no wake batches
942}
943
944static void
945timer_thread_check_timeout(rb_vm_t *vm)
946{
947 // no M:N threads, no timed waiters
948}
949
950/* -------------------------------------------------------------------------
951 * misc
952 * ------------------------------------------------------------------------- */
953
954int
955ruby_stack_overflowed_p(const rb_thread_t *th, const void *addr)
956{
957 return rb_ec_raised_p(th->ec, RAISED_STACKOVERFLOW);
958}
959
960#if defined(__MINGW32__)
961LONG WINAPI
962rb_w32_stack_overflow_handler(struct _EXCEPTION_POINTERS *exception)
963{
964 if (exception->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) {
965 rb_ec_raised_set(GET_EC(), RAISED_STACKOVERFLOW);
966 raise(SIGSEGV);
967 }
968 return EXCEPTION_CONTINUE_SEARCH;
969}
970#endif
971
972#ifdef RUBY_ALLOCA_CHKSTK
973void
974ruby_alloca_chkstk(size_t len, void *sp)
975{
976 if (ruby_stack_length(NULL) * sizeof(VALUE) >= len) {
977 rb_execution_context_t *ec = GET_EC();
978 if (!rb_ec_raised_p(ec, RAISED_STACKOVERFLOW)) {
979 rb_ec_raised_set(ec, RAISED_STACKOVERFLOW);
980 rb_exc_raise(sysstack_error);
981 }
982 }
983}
984#endif
985
986int
987rb_reserved_fd_p(int fd)
988{
989 return 0;
990}
991
992rb_nativethread_id_t
994{
995 return GetCurrentThread();
996}
997
998void *
999rb_thread_prevent_fork(void *(*func)(void *), void *data)
1000{
1001 return func(data);
1002}
1003
1004#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:2860
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