Debian Codegraph @

src/platform/linux_usbfs/events.rs

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 event::epoll::{self, EventData, EventFlags},
17 fd::{AsFd, BorrowedFd, OwnedFd},
18 io::retry_on_intr,
19 };
20 use slab::Slab;
21 use std::{
22 io,
23 sync::{Arc, Mutex, Weak},
24 task::Waker,
25 thread,
26 };
27
28 use crate::Error;
29
30 use super::Device;
31
32 static EPOLL_FD: OnceCell<OwnedFd> = OnceCell::new();
33 static WATCHES: Mutex<Slab<Watch>> = Mutex::new(Slab::new());
34
35 pub(super) enum Watch {
36 Device(Weak<Device>),
37 Fd(Arc<AtomicWaker>),
38 }
39
40 pub(super) fn register(fd: BorrowedFd, watch: Watch, flags: EventFlags) -> Result<usize, Error> {
41 let mut start_thread = false;
42 let epoll_fd = EPOLL_FD.get_or_try_init(|| {
43 start_thread = true;
44 epoll::create(epoll::CreateFlags::CLOEXEC)
45 })?;
46
47 let id = {
48 let mut watches = WATCHES.lock().unwrap();
49 watches.insert(watch)
50 };
51
52 if start_thread {
53 thread::spawn(event_loop);
54 }
55
56 let data = EventData::new_u64(id as u64);
57 epoll::add(epoll_fd, fd, data, flags)?;
58 Ok(id)
59 }
60
61 pub(super) fn unregister_fd(fd: BorrowedFd) {
62 let epoll_fd = EPOLL_FD.get().unwrap();
63 epoll::delete(epoll_fd, fd).ok();
64 }
65
66 pub(super) fn unregister(fd: BorrowedFd, events_id: usize) {
67 let epoll_fd = EPOLL_FD.get().unwrap();
68 epoll::delete(epoll_fd, fd).ok();
69 WATCHES.lock().unwrap().remove(events_id);
70 }
71
72 fn event_loop() {
73 let epoll_fd = EPOLL_FD.get().unwrap();
74 let mut event_list = epoll::EventVec::with_capacity(4);
75 loop {
76 retry_on_intr(|| epoll::wait(epoll_fd, &mut event_list, -1)).unwrap();
77 for event in &event_list {
78 let key = event.data.u64() as usize;
79 let lock = WATCHES.lock().unwrap();
80 let Some(watch) = lock.get(key) else { continue };
81
82 match watch {
83 Watch::Device(w) => {
84 if let Some(device) = w.upgrade() {
85 drop(lock);
86 device.handle_events();
87 // `device` gets dropped here. if it was the last reference, the LinuxDevice will be dropped.
88 // That will unregister its fd, so it's important that WATCHES is unlocked here, or we'd deadlock.
89 }
90 }
91 Watch::Fd(waker) => waker.wake(),
92 }
93 }
94 }
95 }
96
97 pub(crate) struct Async<T> {
98 pub(crate) inner: T,
99 waker: Arc<AtomicWaker>,
100 id: usize,
101 }
102
103 impl<T: AsFd> Async<T> {
104 pub fn new(inner: T) -> Result<Self, io::Error> {
105 let waker = Arc::new(AtomicWaker::new());
106 let id = register(inner.as_fd(), Watch::Fd(waker.clone()), EventFlags::empty())?;
107 Ok(Async { inner, id, waker })
108 }
109
110 pub fn register(&self, waker: &Waker) -> Result<(), io::Error> {
111 self.waker.register(waker);
112 let epoll_fd = EPOLL_FD.get().unwrap();
113 epoll::modify(
114 epoll_fd,
115 self.inner.as_fd(),
116 EventData::new_u64(self.id as u64),
117 EventFlags::ONESHOT | EventFlags::IN,
118 )?;
119 Ok(())
120 }
121 }