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

[WIP] Generic Select for IO #3595

Closed
wants to merge 4 commits into from
Closed

Conversation

RX14
Copy link
Contributor

@RX14 RX14 commented Nov 27, 2016

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.


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
Copy link
Contributor Author

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.

Copy link
Contributor

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 :)

select
when byte = reader.read_byte
byte.should eq(10_u8)
end
Copy link
Contributor

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

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.
@RX14 RX14 force-pushed the feature/io-select branch 4 times, most recently from 8d059d5 to c8c5769 Compare December 9, 2016 18:58
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().

# :nodoc:
def read_token
self.as(Void*) + 1
Copy link
Contributor Author

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.

@RX14
Copy link
Contributor Author

RX14 commented May 10, 2017

Closing because this PR is out of date and the conversation here. Hopefully this will be useful reference code for the future though.

@RX14 RX14 closed this May 10, 2017
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.

None yet

2 participants