Skip to content

kernel: Clarify timeout API regarding negative inputs #20439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

nordic-krch
Copy link
Collaborator

@nordic-krch nordic-krch commented Nov 7, 2019

Timeout API use s32_t as an argument but only positive
values are accepted (or special value like K_FOREVER). It was
not specified in the description which may lead to misinterpretation.

Fixes #20438.

kernel/sched.c Outdated
@@ -975,6 +977,8 @@ s32_t z_impl_k_sleep(int ms)
{
s32_t ticks;

__ASSERT(ms >= 0, "Only positive values are accepted.");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can't do this. Existing code passes K_FOREVER which will fail. That needs to get cleaned up.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

in the tree?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes. Ten examples in-tree, probably more outside. k_sleep(K_FOREVER) is an obvious thing to do, even though it doesn't work as people expect unless it's within a while (true) loop.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

moved it to #20448.

@andrewboie
Copy link
Contributor

not specified in the description which may lead to misinterpretation.

Misinterpretation?
Users might expect Zephyr to be capable of traveling through time?

@pabigot
Copy link
Collaborator

pabigot commented Nov 7, 2019

not specified in the description which may lead to misinterpretation.

Misinterpretation?
Users might expect Zephyr to be capable of traveling through time?

Perhaps. But more likely they might expect code that was working as desired before #19591 would continue to behave the same way in v2.1.

@nordic-krch
Copy link
Collaborator Author

nordic-krch commented Nov 7, 2019

@andrewboie

Users might expect Zephyr to be capable of traveling through time

That's for sure one thing. You can ask author of that piece of code if that was his intention:

k_sleep(__ticks_to_ms(ticks - ticks_elapsed));

Another is that user may expect that he can perform arithmetic operations on timeout or sleep and don't care about negative result which will be interpreted (and currently is, at least until #19591) as the past.

@nordic-krch nordic-krch changed the title kernel: Clarify timeout and sleep API regarding negative inputs kernel: Clarify timeout API regarding negative inputs Nov 8, 2019
@carlescufi
Copy link
Member

@pabigot can you take one more look?

Timeout and use s32_t as an argument but only positive values are
accepted (or special value like K_FOREVER). It was not specified in
the description which may lead to misinterpretation.

Signed-off-by: Krzysztof Chruscinski <[email protected]>
kernel/sched.c Outdated
@@ -368,6 +368,7 @@ static void pend(struct k_thread *thread, _wait_q_t *wait_q, s32_t timeout)
}

if (timeout != K_FOREVER) {
__ASSERT(timeout >= 0, "Only positive values are accepted.");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Non-negative, not positive.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@nordic-krch nordic-krch force-pushed the clarify_timeout_api branch 2 times, most recently from 0f82f83 to 711fbb1 Compare November 8, 2019 11:46
@zephyrbot
Copy link
Collaborator

zephyrbot commented Nov 8, 2019

All checks passed.

Tip: The bot edits this comment instead of posting a new one, so you can check the comment's history to see earlier messages.

Add assert when negative (except K_FOREVER) is passed as timeout.
Add negative timeout correction to 0.

Signed-off-by: Krzysztof Chruscinski <[email protected]>
@pabigot pabigot self-requested a review November 8, 2019 16:15
Copy link
Collaborator

@pabigot pabigot left a comment

Choose a reason for hiding this comment

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

OK within the limits of time and release schedule. It'll have to be updated again if/when we evolve the type and units used to represent timeouts.

@carlescufi
Copy link
Member

@andrewboie feel free to merge or wait for @andyross 's review

@andrewboie
Copy link
Contributor

@carlescufi let me ping @andyross just to be sure

Copy link
Collaborator

@andyross andyross left a comment

Choose a reason for hiding this comment

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

Doc changes look great. Not loving the "assert and clamp" behavior, but it's harmless. This does complicate the rebase of the timeout patches I guess, but that's not awful.


if (timeout < 0) {
timeout = 0;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the value of having both an assert and a runtime clamp? I'd leave the former in and take this out. Note that this code gets changed in the timeout patch anyway, where conversion happens in the initialization of the k_timeout_t before we get here.

Copy link
Member

Choose a reason for hiding this comment

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

agree with andyross here, @andrewboie merged while I was commenting, so we will deal with this later when doing the overall runtime checks

@andrewboie andrewboie merged commit f831929 into zephyrproject-rtos:master Nov 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: API Changes to public APIs area: Kernel
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Kernel timeout API does not document well accepted values
7 participants