Skip to content

Make thread delays interruptible ##util#25184

Merged
trufae merged 2 commits intomasterfrom
thread-launcher-race
Jan 7, 2026
Merged

Make thread delays interruptible ##util#25184
trufae merged 2 commits intomasterfrom
thread-launcher-race

Conversation

@trufae
Copy link
Copy Markdown
Collaborator

@trufae trufae commented Jan 7, 2026

  • Mark this if you consider it ready to merge
  • I've added tests (optional)
  • I wrote some lines in the book (optional)

Description

Copilot AI review requested due to automatic review settings January 7, 2026 09:57
@trufae trufae merged commit 6088972 into master Jan 7, 2026
23 checks passed
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 breaked flag
  • 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.

Comment thread libr/util/thread.c
Comment on lines +50 to +55
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;
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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;

Copilot uses AI. Check for mistakes.
@trufae trufae deleted the thread-launcher-race branch January 9, 2026 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants