Make thread delays interruptible ##util#25184
Conversation
There was a problem hiding this comment.
Pull request overview
This PR enhances the thread delay mechanism to make it interruptible by checking the breaked flag during sleep operations. Instead of sleeping for the entire delay period in one blocking call, the implementation now sleeps in small 10ms intervals and checks if the thread should be interrupted between each interval.
Key Changes:
- Thread delays can now be interrupted early by setting the
breakedflag - Lock is released during the sleep loop to allow other operations to proceed
- Sleep operations broken into 10ms intervals for responsiveness
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ut32 delay = th->delay; | ||
| th->delay = 0; | ||
| r_th_lock_leave (th->lock); | ||
| while (delay > 0 && !th->breaked) { | ||
| r_sys_usleep (10000); | ||
| delay = (delay > 10) ? delay - 10 : 0; |
There was a problem hiding this comment.
The delay calculation has a unit mismatch. The delay field is documented as "delay the startup of the thread for at least N seconds" (libr/include/r_th.h:170), and the old code used r_sys_sleep(th->delay) which expects seconds. However, the new code sleeps for 10ms intervals and decrements delay by 10 each iteration, effectively treating delay as if it's in units of 10ms rather than seconds. This means a delay of 5 seconds would only sleep for 50ms (5 iterations * 10ms), instead of the intended 5000ms.
To fix this, the delay should be converted to milliseconds or the decrement should be adjusted accordingly. For example, convert delay to milliseconds initially (multiply by 1000), then decrement by 10 each iteration.
| ut32 delay = th->delay; | |
| th->delay = 0; | |
| r_th_lock_leave (th->lock); | |
| while (delay > 0 && !th->breaked) { | |
| r_sys_usleep (10000); | |
| delay = (delay > 10) ? delay - 10 : 0; | |
| /* th->delay is in seconds; convert to milliseconds for 10ms steps */ | |
| ut32 delay_ms = th->delay * 1000; | |
| th->delay = 0; | |
| r_th_lock_leave (th->lock); | |
| while (delay_ms > 0 && !th->breaked) { | |
| r_sys_usleep (10000); | |
| delay_ms = (delay_ms > 10) ? delay_ms - 10 : 0; |
Description