-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[WIP] Generic Select for IO #3595
Conversation
|
||
def self.select(actions : Tuple | Array, has_else = false) | ||
pollfds = Array(LibC::Pollfd).new | ||
fdmap = Hash(Int32, Int32).new # Map of poll file descriptor to action index |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to get rid of these malloc
calls in select
. Unfortunately it seems that allocating variable size arrays on the stack isn't possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe one could recycle the pollfds
and fdmap
variables? Chances are good, that a Fiber is always running the same select
in a loop, with the same arguments, thus the sizes of both would never change and no sub-sequent allocations will be done. That, or we need to introduce VLAs :)
029fdde
to
0527a21
Compare
select | ||
when byte = reader.read_byte | ||
byte.should eq(10_u8) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will not fail if line 10 is never reached
0527a21
to
a57b0cf
Compare
Expanding nodoc to comments which simply end in :nodoc: means that technical documentation for implementors can be attached to lower-level classes while keeping the docs free of their presence.
Resume tokens allows the resuming fiber to send a pointer to the resumed fiber. This is useful in the case when there are multiple possible resume events, as the resumed fiber can work out which event resumed it by checking the received token.
8d059d5
to
c8c5769
Compare
Select.select is a more generic select variant which can select over anything which can resume the fiber once the action is ready. It also provides optimisations for actions which can be tested for readiness cheaply, and actions which can provide a pollfd struct compatible with poll().
c8c5769
to
1f06f5c
Compare
|
||
# :nodoc: | ||
def read_token | ||
self.as(Void*) + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've implemented differentiating read/write tokens by adding a constant to the self pointer. This means now you can select over read/write on the same channel/IO.
Closing because this PR is out of date and the conversation here. Hopefully this will be useful reference code for the future though. |
See extended commit messages for more details.
This is WIP, as some things like selecting read/write from a single IO, and selecting on
sleep
are not yet implemented. I wanted to get this up for discussion early as there may be a better way to implement this, especially resume tokens.