This commit is contained in:
Luke Gruber 2025-08-14 19:36:50 +03:00 committed by GitHub
commit 965c664881
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 13 deletions

View file

@ -3166,20 +3166,26 @@ rb_thread_create_timer_thread(void)
timer_thread_setup_mn();
}
pthread_create(&timer_th.pthread_id, NULL, timer_thread_func, GET_VM());
if (pthread_create(&timer_th.pthread_id, NULL, timer_thread_func, GET_VM()) != 0) {
system_working = 0;
// NOTE: safe to call into VM here even if we're in the middle of a call to `rb_fork_ruby()`
rb_warn("Couldn't create timer thread, error: %s", strerror(errno));
}
}
static int
native_stop_timer_thread(void)
{
RUBY_ATOMIC_SET(system_working, 0);
if (RUBY_ATOMIC_LOAD(system_working)) {
RUBY_ATOMIC_SET(system_working, 0);
RUBY_DEBUG_LOG("wakeup send %d", timer_th.comm_fds[1]);
timer_thread_wakeup_force();
RUBY_DEBUG_LOG("wakeup sent");
pthread_join(timer_th.pthread_id, NULL);
RUBY_DEBUG_LOG("wakeup send %d", timer_th.comm_fds[1]);
timer_thread_wakeup_force();
RUBY_DEBUG_LOG("wakeup sent");
pthread_join(timer_th.pthread_id, NULL);
if (TT_DEBUG) fprintf(stderr, "stop timer thread\n");
if (TT_DEBUG) fprintf(stderr, "stop timer thread\n");
}
return 1;
}

View file

@ -797,19 +797,27 @@ rb_thread_create_timer_thread(void)
}
timer_thread.id = w32_create_thread(1024 + (USE_RUBY_DEBUG_LOG ? BUFSIZ : 0),
timer_thread_func, 0);
w32_resume_thread(timer_thread.id);
if (timer_thread.id == 0) {
system_working = 0;
rb_warn("Couldn't create timer thread, error:%s", strerror(thread_errno));
}
else {
w32_resume_thread(timer_thread.id);
}
}
}
static int
native_stop_timer_thread(void)
{
RUBY_ATOMIC_SET(system_working, 0);
if (RUBY_ATOMIC_LOAD(system_working)) {
RUBY_ATOMIC_SET(system_working, 0);
SetEvent(timer_thread.lock);
native_thread_join(timer_thread.id);
CloseHandle(timer_thread.lock);
timer_thread.lock = 0;
SetEvent(timer_thread.lock);
native_thread_join(timer_thread.id);
CloseHandle(timer_thread.lock);
timer_thread.lock = 0;
}
return 1;
}