Skip to content
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

thread_sync.c: Clarify and document the behavior of timeout == 0 #6500

Merged
merged 1 commit into from Oct 17, 2022

Conversation

casperisfine
Copy link
Contributor

[Feature #18982]

Instead of introducing an exception: false argument to have non_block return nil rather than raise, we can clearly document that a timeout of 0 immediately returns.

The code is refactored a bit to avoid doing a time calculation in such case.

cc @ko1 @eregon

@eregon
Copy link
Member

eregon commented Oct 6, 2022

Seems OK to me.
Maybe a bit weird that 0.0 doesn't exactly behave as 0 (doesn't have the optimization).

@casperisfine
Copy link
Contributor Author

Maybe a bit weird that 0.0 doesn't exactly behave as 0 (doesn't have the optimization).

Indeed, I guess it wouldn't cost much to test for it as well. I can add it.

@eregon
Copy link
Member

eregon commented Oct 6, 2022

I think the correct thing to do here would be to coerce first (Fixnum or use to_f) and then test for both of these being 0.
If not 0, then convert that to hrtime

rb_raise(rb_eThreadError, "queue empty");
}

if (RTEST(rb_equal(INT2FIX(0), timeout))) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eregon this should do ^

Copy link
Member

Choose a reason for hiding this comment

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

That should work, except if it's a custom object with to_f. Probably good enough.

[Feature #18982]

Instead of introducing an `exception: false` argument to have `non_block`
return nil rather than raise, we can clearly document that a timeout of 0
immediately returns.

The code is refactored a bit to avoid doing a time calculation in
such case.
@byroot byroot merged commit 60defe0 into ruby:master Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants