Skip to content

Interruptable sleep functions#374

Open
Next-Door-Tech wants to merge 3 commits intoWiringPi:nanosleepfrom
Next-Door-Tech:nanosleep-interruptable
Open

Interruptable sleep functions#374
Next-Door-Tech wants to merge 3 commits intoWiringPi:nanosleepfrom
Next-Door-Tech:nanosleep-interruptable

Conversation

@Next-Door-Tech
Copy link
Copy Markdown
Contributor

@Next-Door-Tech Next-Door-Tech commented Jun 8, 2025

The original versions of delayOld() and delayMicrosecondsOld() used nanosleep() not in a loop, meaning that if the process received a system interrupt signal sleep would halt and not resume, unless delayMicrosecondsHard() was used. Current implementations of delay() etc. do resume sleep if interrupted.

The new functions delayInterruptable(), delayMicrosecondsInterruptable(), and delayNanosecondsInterruptable() do not resume after a system interrupt is received, akin to the old non-hard delay functions.

The functions delay(), delayMicroseconds(), and delayNanoseconds() has been changed so that their calls to clock_nanosleep() are not inside a loop, mirroring the original behavior.

New non-interruptable functions delayNI(), delayMicrosecondsNI(), and delayNanosecondsNI() have been added alongside a new helper delayHelperNI() which are simply the loop-resuming versions of their counterparts before this change.

@Next-Door-Tech
Copy link
Copy Markdown
Contributor Author

Up for discussion: should the behavior of the "default" versions of delay(), etc. be interruptable since this was the behavior of the original old versions (if not hard delaying), and the "uninterruptable" versions made into their own separately-named functions instead?

I'm thinking this may be the correct move since there may be some edge case where another project uses delay() to wait for an interrupt signal.

@mstroh76
Copy link
Copy Markdown
Member

mstroh76 commented Jun 10, 2025

I think the new delay function should have the same behavior as the old one.
I will take a look at some WiringPi projects to see how the delay command is used there. Personally, I have never used long delays and have always handled signals myself.

@Next-Door-Tech Next-Door-Tech force-pushed the nanosleep-interruptable branch from 7b297a6 to bd6956c Compare June 11, 2025 22:32
@Next-Door-Tech Next-Door-Tech force-pushed the nanosleep-interruptable branch from bd6956c to 83d5f80 Compare June 12, 2025 00:02
@Next-Door-Tech
Copy link
Copy Markdown
Contributor Author

I think the new delay function should have the same behavior as the old one.

Ok, in that case, I'll remove the loop around clock_nanosleep() from the "standard" versions of delay() etc. so that they behave exactly as the old versions did.

The non-interruptable behavior (loop included) is now in the non-interruptable functions delayNI() etc. These versions are simply copies of their counterparts before this change.

@Next-Door-Tech
Copy link
Copy Markdown
Contributor Author

BTW, this should be the last major change to the delay() family. Ready for testing.

@Next-Door-Tech Next-Door-Tech marked this pull request as ready for review June 12, 2025 00:11
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.

2 participants