From d845da05e83a2c2929ef8d4fd829804d44f292d3 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Thu, 14 Nov 2024 15:21:38 -0800 Subject: [PATCH] Force reset running time in timer interrupt Co-authored-by: Ivo Anjo Co-authored-by: Luke Gruber --- ractor_core.h | 10 +++++----- thread.c | 6 +++--- thread_pthread.c | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ractor_core.h b/ractor_core.h index 256ecc38e6..a4d0a087d0 100644 --- a/ractor_core.h +++ b/ractor_core.h @@ -277,12 +277,16 @@ rb_ractor_sleeper_thread_num(rb_ractor_t *r) } static inline void -rb_ractor_thread_switch(rb_ractor_t *cr, rb_thread_t *th) +rb_ractor_thread_switch(rb_ractor_t *cr, rb_thread_t *th, bool always_reset) { RUBY_DEBUG_LOG("th:%d->%u%s", cr->threads.running_ec ? (int)rb_th_serial(cr->threads.running_ec->thread_ptr) : -1, rb_th_serial(th), cr->threads.running_ec == th->ec ? " (same)" : ""); + if (cr->threads.running_ec != th->ec || always_reset) { + th->running_time_us = 0; + } + if (cr->threads.running_ec != th->ec) { if (0) { ruby_debug_printf("rb_ractor_thread_switch ec:%p->%p\n", @@ -293,10 +297,6 @@ rb_ractor_thread_switch(rb_ractor_t *cr, rb_thread_t *th) return; } - if (cr->threads.running_ec != th->ec) { - th->running_time_us = 0; - } - cr->threads.running_ec = th->ec; VM_ASSERT(cr == GET_RACTOR()); diff --git a/thread.c b/thread.c index 8cf9030ee6..ab28b0debc 100644 --- a/thread.c +++ b/thread.c @@ -173,7 +173,7 @@ static inline void blocking_region_end(rb_thread_t *th, struct rb_blocking_regio #define THREAD_BLOCKING_END(th) \ thread_sched_to_running((sched), (th)); \ - rb_ractor_thread_switch(th->ractor, th); \ + rb_ractor_thread_switch(th->ractor, th, false); \ } while(0) #ifdef __GNUC__ @@ -1470,7 +1470,7 @@ rb_thread_schedule_limits(uint32_t limits_us) RB_VM_SAVE_MACHINE_CONTEXT(th); thread_sched_yield(TH_SCHED(th), th); - rb_ractor_thread_switch(th->ractor, th); + rb_ractor_thread_switch(th->ractor, th, true); RUBY_DEBUG_LOG("switch %s", "done"); } @@ -1518,7 +1518,7 @@ blocking_region_end(rb_thread_t *th, struct rb_blocking_region_buffer *region) unregister_ubf_list(th); thread_sched_to_running(TH_SCHED(th), th); - rb_ractor_thread_switch(th->ractor, th); + rb_ractor_thread_switch(th->ractor, th, false); th->blocking_region_buffer = 0; rb_ractor_blocking_threads_dec(th->ractor, __FILE__, __LINE__); diff --git a/thread_pthread.c b/thread_pthread.c index 7811d5afbf..fd67eaf735 100644 --- a/thread_pthread.c +++ b/thread_pthread.c @@ -859,7 +859,7 @@ thread_sched_wait_running_turn(struct rb_thread_sched *sched, rb_thread_t *th, b RUBY_DEBUG_LOG("(nt) wakeup %s", sched->running == th ? "success" : "failed"); if (th == sched->running) { - rb_ractor_thread_switch(th->ractor, th); + rb_ractor_thread_switch(th->ractor, th, false); } } else {