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

Refactor and add comments to IOCP #run_once #14380

Merged

Conversation

straight-shoota
Copy link
Member

While reading through this method 2 years after writing it, I found it hard to understand what its doing.
So this patch adds some comments to annotate the code.

And I also realized the control flow could be restructed to be more linear and easier to follow.
Thes code changes introduce no semantic difference, the just bring it to a slightly better readable form. The resulting LLVM IR is almost identical, just with a few branch lables swapped (as expected).

@beta-ziliani beta-ziliani added this to the 1.12.0 milestone Mar 21, 2024
Comment on lines +47 to +50
unless next_event
Crystal::System.print_error "Warning: No runnables in scheduler. Exiting program.\n"
::exit
end
Copy link
Contributor

Choose a reason for hiding this comment

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

For the record, this cannot actually happen because min_by instead of min_by? is used

Copy link
Member

Choose a reason for hiding this comment

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

nice catch!

Copy link
Contributor

Choose a reason for hiding this comment

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

thought (non blocking): is that the job of the eventloop to decide to exit? As stated in the error message, this is a matter for the scheduler.

libevent merely returns an error (no events), and the case is expected in my implementation for RFC #2: the schedulers use it to decide to start spinning then park themselves for example.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, this is definitely not a final solution. But it's just a stop gap for some odd edge case. So far it has never happend because the branch couldn't be reached and you'd get a different error if the queue was empty.
I think this was introduced before we even had an actual event loop on Windows and things were much more brittle.

Comment on lines +47 to +50
unless next_event
Crystal::System.print_error "Warning: No runnables in scheduler. Exiting program.\n"
::exit
end
Copy link
Member

Choose a reason for hiding this comment

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

nice catch!

src/crystal/system/win32/event_loop_iocp.cr Show resolved Hide resolved
@beta-ziliani beta-ziliani removed this from the 1.12.0 milestone Mar 21, 2024
@beta-ziliani beta-ziliani added this to the 1.12.0 milestone Mar 22, 2024
@straight-shoota straight-shoota merged commit 10e354f into crystal-lang:master Mar 25, 2024
57 of 58 checks passed
@straight-shoota straight-shoota deleted the refactor/iocp-run_once branch March 25, 2024 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants