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

Implement AsRawFd for EventLoop<'l, Data> #159

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#### Additions

- `Token` and `RegistrationToken` are now invalidated when the event source they represent is removed from the event loop.
- Implement `AsRawFd` and `AsFd` for `EventLoop<'l, Data>`

#### Bugfixes

Expand Down
31 changes: 30 additions & 1 deletion src/loop_logic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cell::{Cell, RefCell};
use std::fmt::Debug;
use std::os::unix::io::AsFd;
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
Expand All @@ -11,6 +11,7 @@
use std::future::Future;

use log::trace;
use polling::Poller;

use crate::list::{SourceEntry, SourceList};
use crate::sources::{Dispatcher, EventSource, Idle, IdleDispatcher};
Expand Down Expand Up @@ -286,6 +287,7 @@
///
/// This loop can host several event sources, that can be dynamically added or removed.
pub struct EventLoop<'l, Data> {
poller: Arc<Poller>,
handle: LoopHandle<'l, Data>,
signals: Arc<Signals>,
// A caching vector for synthetic poll events
Expand Down Expand Up @@ -315,6 +317,7 @@
/// Fails if the initialization of the polling system failed.
pub fn try_new() -> crate::Result<Self> {
let poll = Poll::new()?;
let poller = poll.poller.clone();
let handle = LoopHandle {
inner: Rc::new(LoopInner {
poll: RefCell::new(poll),
Expand All @@ -332,6 +335,7 @@
#[cfg(feature = "block_on")]
future_ready: AtomicBool::new(false),
}),
poller,
synthetic_events: vec![],
})
}
Expand Down Expand Up @@ -660,6 +664,31 @@
}
}

impl<'l, Data> AsRawFd for EventLoop<'l, Data> {
/// Get the underlying raw-fd of the poller.
///
/// This could be used to create [`Generic`] source out of the current loop
/// and inserting into some other [`EventLoop`]. It's recommended to clone `fd`
/// before doing so.
///
/// [`Generic`]: crate::generic::Generic
fn as_raw_fd(&self) -> RawFd {
self.poller.as_raw_fd()
}

Check warning on line 677 in src/loop_logic.rs

View check run for this annotation

Codecov / codecov/patch

src/loop_logic.rs#L675-L677

Added lines #L675 - L677 were not covered by tests
}

impl<'l, Data> AsFd for EventLoop<'l, Data> {
/// Get the underlying fd of the poller.
///
/// This could be used to create [`Generic`] source out of the current loop
/// and inserting into some other [`EventLoop`].
///
/// [`Generic`]: crate::generic::Generic
fn as_fd(&self) -> BorrowedFd<'_> {
self.poller.as_fd()
}

Check warning on line 689 in src/loop_logic.rs

View check run for this annotation

Codecov / codecov/patch

src/loop_logic.rs#L687-L689

Added lines #L687 - L689 were not covered by tests
}

#[derive(Clone, Debug)]
/// The EventIterator is an `Iterator` over the events relevant to a particular source
/// This type is used in the [`EventSource::before_handle_events`] methods for
Expand Down
2 changes: 1 addition & 1 deletion src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
/// source and delegate the implementations to it.
pub struct Poll {
/// The handle to wepoll/epoll/kqueue/... used to poll for events.
poller: Arc<Poller>,
pub(crate) poller: Arc<Poller>,

/// The buffer of events returned by the poller.
events: RefCell<Events>,
Expand Down Expand Up @@ -319,7 +319,7 @@
let ev = cvt_interest(interest, token);

// SAFETY: See invariant on function.
unsafe {

Check warning on line 322 in src/sys.rs

View workflow job for this annotation

GitHub Actions / CI (1.63.0)

unnecessary `unsafe` block

Check warning on line 322 in src/sys.rs

View workflow job for this annotation

GitHub Actions / CI (1.63.0)

unnecessary `unsafe` block
self.poller
.add_with_mode(raw, ev, cvt_mode(mode, self.poller.supports_level()))?;
}
Expand Down
Loading