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

Make Event a wrapper around a platform event #983

Merged
merged 18 commits into from
Jun 18, 2019

Conversation

Thomasdezeeuw
Copy link
Collaborator

@Thomasdezeeuw Thomasdezeeuw commented Jun 15, 2019

Now Event is just a wrapper around a platform specific event to provide
a nice API, with methods such as is_readable.

This removes the Ready type and moves all its methods to the Event type.

TODO:

  • Make this work on all platforms:
    • kqueue,
    • epoll,
    • Windows.
  • Fix tests:
    • Fix test_write_shutdown test, the following fails assert!(!event.is_hup()).
    • Update all tests in test_waker module (uses now removed Event::new).
  • Fix examples:
    • in Waker.
  • Fix docs everywhere.

Now Event is just a wrapper around a platform specific event to provide
a nice API, with methods such as is_readable.

This removes the Ready type and moves all its methods to the Event type.
@Thomasdezeeuw
Copy link
Collaborator Author

Windows is left, the other platforms work.

This basically moves the old Event and Ready into the windows module.

Someone with more knowledge about IOCP and Windows should really look at
this, the currently situation isn't great.
@Thomasdezeeuw
Copy link
Collaborator Author

I've made an attempt at fixing Windows, basically moving Event and Ready into the windows module and pretend nothing changed. We already wanted a major rewrite of the Window's side of things, I just added to the mess here and hopefully that's acceptable for now.

@Thomasdezeeuw
Copy link
Collaborator Author

waker_multiple_wakeups_same_thread fails on Windows because it explicitly check for a single event, but Windows creates three. @carllerche do we just want to make the test less strict?

Now it accept mutltiple waker events, rather then just one.
@Thomasdezeeuw
Copy link
Collaborator Author

With 31e271a Windows also works.

// TODO: add readiness.
f.debug_struct("Event")
.field("token", &self.token())
//.field("readiness", &self.readiness())
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can we do with readiness? I could create a function epoll_event/kevent -> Ready, but is worth the effort?

Copy link
Member

Choose a reason for hiding this comment

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

I don't understand the question / problem. Instead of readiness, we could also include the details like "readable", "writable", "lio", ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've moved the Debug impl to event_imp.rs.

src/sys/mod.rs Outdated
@@ -1,14 +1,16 @@
#[cfg(unix)]
pub use self::unix::{
pipe, set_nonblock, EventedFd, Events, Io, Selector, TcpListener, TcpStream, UdpSocket, Waker,
pipe, set_nonblock, Event, EventedFd, Events, Io, RawEvent, Selector, TcpListener, TcpStream,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure if I like the name RawEvent, would SysEvent be better?

Copy link
Member

Choose a reason for hiding this comment

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

SysEvent is probably best as we are going for "sys" terminology.

Copy link
Member

Choose a reason for hiding this comment

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

I mentioned it in an in-progress review, but what about sys::Event? It is a crate private type anyway.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sys::Event and SysEvent are two different types. sys::Event is a wrapper around SysEvent to implement token, is_readable, etc. and SysEvent is just an alias for epoll_event/kevent.


println!("SHUTTING DOWN");
// Now, shutdown the write half of the socket.
socket.shutdown(Shutdown::Write).unwrap();

wait!(poll, is_readable);
wait!(poll, is_readable, true);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a function change.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Before this wouldn't expect hup here.


expect_no_events(&mut poll, &mut events);

handle1.join().unwrap();
handle2.join().unwrap();
}

fn expect_waker_event(poll: &mut Poll, events: &mut Events, token: Token) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This because less strict, allow multiple waker events rather then only one.

@Thomasdezeeuw Thomasdezeeuw marked this pull request as ready for review June 17, 2019 11:37
Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

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

Looks pretty good 👍 thanks for putting this together. I had a few minor questions inline.

src/event_imp.rs Outdated
}
/// Get access to the platform specific event, the returned value differs
/// per platform.
pub fn raw_event(&self) -> &sys::RawEvent {
Copy link
Member

Choose a reason for hiding this comment

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

Is this function intentionally part of the public API?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, this allows users to access the underlying system event for platform specific extensions. You previously mentioned accessing kevent for optimising Tokio.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, my intent was not to expose RawEvent but have kqueue only fns directly on Event (conditionally defined). WDYT? Either way, we can punt exposing the raw event until we get to exposing kqueue specific data.

src/event_imp.rs Outdated
pub fn ready_from_usize(events: usize) -> Ready {
Ready::from_usize(events)
/// Create an `Event` from a platform specific event.
pub fn from_raw_event(raw_event: sys::RawEvent) -> Event {
Copy link
Member

Choose a reason for hiding this comment

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

Is this function intentionally part of the public API?

Also, if we are bikeshedding names, what about:

  • sys::Event
  • fn as_sys(&self) -> &sys::Event
  • fn from_sys(sys::Event)

src/sys/mod.rs Outdated
@@ -1,14 +1,16 @@
#[cfg(unix)]
pub use self::unix::{
pipe, set_nonblock, EventedFd, Events, Io, Selector, TcpListener, TcpStream, UdpSocket, Waker,
pipe, set_nonblock, Event, EventedFd, Events, Io, RawEvent, Selector, TcpListener, TcpStream,
Copy link
Member

Choose a reason for hiding this comment

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

I mentioned it in an in-progress review, but what about sys::Event? It is a crate private type anyway.

// TODO: add readiness.
f.debug_struct("Event")
.field("token", &self.token())
//.field("readiness", &self.readiness())
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand the question / problem. Instead of readiness, we could also include the details like "readable", "writable", "lio", ...

Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

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

Approving, but I recommend removing fn sys_event and fn from_sys_event for now and consider adding it back when we approach exposing kqueue specific data.

@carllerche
Copy link
Member

Very nice job 👍

@Thomasdezeeuw
Copy link
Collaborator Author

I've removed the system event from the public API and opened #990.

@carllerche carllerche merged commit 1eb579c into tokio-rs:master Jun 18, 2019
@Thomasdezeeuw Thomasdezeeuw deleted the platform-event branch June 24, 2019 14:43
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.

2 participants