| 1 |
use atomic_waker::AtomicWaker; |
| 2 |
/// Epoll based event loop for Linux. |
| 3 |
/// |
| 4 |
/// Launches a thread when opening the first device that polls |
| 5 |
/// for events on usbfs devices and arbitrary file descriptors |
| 6 |
/// (used for udev hotplug). |
| 7 |
/// |
| 8 |
/// ### Why not share an event loop with `tokio` or `async-io`? |
| 9 |
/// |
| 10 |
/// This event loop will call USBFS_REAP_URB on the event thread and |
| 11 |
/// dispatch to the transfer's waker directly. Since all USB transfers |
| 12 |
/// on a device use the same file descriptor, putting USB-specific |
| 13 |
/// dispatch in the event loop avoids additonal synchronization. |
| 14 |
use once_cell::sync::OnceCell; |
| 15 |
use rustix::{ |
| •16 |
buffer::spare_capacity, |
| 17 |
event::epoll::{self, EventData, EventFlags}, |
| 18 |
fd::{AsFd, BorrowedFd, OwnedFd}, |
| 19 |
io::retry_on_intr, |
| 20 |
}; |
| 21 |
use slab::Slab; |
| 22 |
use std::{ |
| 23 |
io, |
| 24 |
sync::{Arc, Mutex, Weak}, |
| 25 |
task::Waker, |
| 26 |
thread, |
| 27 |
}; |
| 28 |
|
| 29 |
use crate::Error; |
| 30 |
|
| 31 |
use super::Device; |
| 32 |
|
| 33 |
static EPOLL_FD: OnceCell<OwnedFd> = OnceCell::new(); |
| 34 |
static WATCHES: Mutex<Slab<Watch>> = Mutex::new(Slab::new()); |
| 35 |
|
| 36 |
pub(super) enum Watch { |
| 37 |
Device(Weak<Device>), |
| 38 |
Fd(Arc<AtomicWaker>), |
| 39 |
} |
| 40 |
|
| 41 |
pub(super) fn register(fd: BorrowedFd, watch: Watch, flags: EventFlags) -> Result<usize, Error> { |
| 42 |
let mut start_thread = false; |
| 43 |
let epoll_fd = EPOLL_FD.get_or_try_init(|| { |
| 44 |
start_thread = true; |
| 45 |
epoll::create(epoll::CreateFlags::CLOEXEC) |
| 46 |
})?; |
| 47 |
|
| 48 |
let id = { |
| 49 |
let mut watches = WATCHES.lock().unwrap(); |
| 50 |
watches.insert(watch) |
| 51 |
}; |
| 52 |
|
| 53 |
if start_thread { |
| 54 |
thread::spawn(event_loop); |
| 55 |
} |
| 56 |
|
| 57 |
let data = EventData::new_u64(id as u64); |
| 58 |
epoll::add(epoll_fd, fd, data, flags)?; |
| 59 |
Ok(id) |
| 60 |
} |
| 61 |
|
| 62 |
pub(super) fn unregister_fd(fd: BorrowedFd) { |
| 63 |
let epoll_fd = EPOLL_FD.get().unwrap(); |
| 64 |
epoll::delete(epoll_fd, fd).ok(); |
| 65 |
} |
| 66 |
|
| 67 |
pub(super) fn unregister(fd: BorrowedFd, events_id: usize) { |
| 68 |
let epoll_fd = EPOLL_FD.get().unwrap(); |
| 69 |
epoll::delete(epoll_fd, fd).ok(); |
| 70 |
WATCHES.lock().unwrap().remove(events_id); |
| 71 |
} |
| 72 |
|
| 73 |
fn event_loop() { |
| 74 |
let epoll_fd = EPOLL_FD.get().unwrap(); |
| •75 |
let mut event_list = Vec::with_capacity(4); |
| 76 |
loop { |
| •77 |
retry_on_intr(|| epoll::wait(epoll_fd, spare_capacity(&mut event_list), None)).unwrap(); |
| 78 |
for event in &event_list { |
| 79 |
let key = event.data.u64() as usize; |
| 80 |
let lock = WATCHES.lock().unwrap(); |
| 81 |
let Some(watch) = lock.get(key) else { continue }; |
| 82 |
|
| 83 |
match watch { |
| 84 |
Watch::Device(w) => { |
| 85 |
if let Some(device) = w.upgrade() { |
| 86 |
drop(lock); |
| 87 |
device.handle_events(); |
| 88 |
// `device` gets dropped here. if it was the last reference, the LinuxDevice will be dropped. |
| 89 |
// That will unregister its fd, so it's important that WATCHES is unlocked here, or we'd deadlock. |
| 90 |
} |
| 91 |
} |
| 92 |
Watch::Fd(waker) => waker.wake(), |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
pub(crate) struct Async<T> { |
| 99 |
pub(crate) inner: T, |
| 100 |
waker: Arc<AtomicWaker>, |
| 101 |
id: usize, |
| 102 |
} |
| 103 |
|
| 104 |
impl<T: AsFd> Async<T> { |
| 105 |
pub fn new(inner: T) -> Result<Self, io::Error> { |
| 106 |
let waker = Arc::new(AtomicWaker::new()); |
| 107 |
let id = register(inner.as_fd(), Watch::Fd(waker.clone()), EventFlags::empty())?; |
| 108 |
Ok(Async { inner, id, waker }) |
| 109 |
} |
| 110 |
|
| 111 |
pub fn register(&self, waker: &Waker) -> Result<(), io::Error> { |
| 112 |
self.waker.register(waker); |
| 113 |
let epoll_fd = EPOLL_FD.get().unwrap(); |
| 114 |
epoll::modify( |
| 115 |
epoll_fd, |
| 116 |
self.inner.as_fd(), |
| 117 |
EventData::new_u64(self.id as u64), |
| 118 |
EventFlags::ONESHOT | EventFlags::IN, |
| 119 |
)?; |
| 120 |
Ok(()) |
| 121 |
} |
| 122 |
} |