Skip to content

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

Merged
byroot merged 1 commit intoruby:masterfrom
Shopify:queue-timeout-0-doc
Oct 17, 2022
Merged

thread_sync.c: Clarify and document the behavior of timeout == 0#6500
byroot merged 1 commit intoruby:masterfrom
Shopify:queue-timeout-0-doc

Conversation

@casperisfine
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

Comment thread thread_sync.c
rb_raise(rb_eThreadError, "queue empty");
}

if (RTEST(rb_equal(INT2FIX(0), timeout))) {
Copy link
Copy Markdown
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
Copy Markdown
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants