| 1 |
use atomic_wakerrust-analyzer cargo atomic-waker 1.1.2 crate/::AtomicWakerrust-analyzer cargo atomic-waker 1.1.2 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_cellrust-analyzer cargo once_cell 1.20.3 crate/::syncrust-analyzer cargo once_cell 1.20.3 sync/::OnceCellrust-analyzer cargo once_cell 1.20.3 sync/OnceCell#; |
| 15 |
use rustixrust-analyzer cargo rustix 1.1.4 crate/::{ |
| •16 |
bufferrust-analyzer cargo rustix 1.1.4 buffer/::spare_capacityrust-analyzer cargo rustix 1.1.4 buffer/spare_capacity()., |
| 17 |
eventrust-analyzer cargo rustix 1.1.4 event/::epollrust-analyzer cargo rustix 1.1.4 event/epoll/::{selfrust-analyzer cargo rustix 1.1.4 event/epoll/, EventDatarust-analyzer cargo rustix 1.1.4 event/epoll/EventData#, EventFlagsrust-analyzer cargo rustix 1.1.4 backend/event/epoll/EventFlags#}, |
| 18 |
fdrust-analyzer cargo rustix 1.1.4 fd/::{AsFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/AsFd#, BorrowedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/BorrowedFd#, OwnedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/OwnedFd#}, |
| 19 |
iorust-analyzer cargo rustix 1.1.4 io/::retry_on_intrrust-analyzer cargo rustix 1.1.4 io/errno/retry_on_intr()., |
| 20 |
}; |
| 21 |
use slabrust-analyzer cargo slab 0.4.9 crate/::Slabrust-analyzer cargo slab 0.4.9 Slab#; |
| 22 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 23 |
iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, |
| 24 |
syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::{Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#, Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#, Weakrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Weak#}, |
| 25 |
taskrust-analyzer cargo std https://github.com/rust-lang/rust/library/std task/::Wakerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/wake/Waker#, |
| 26 |
threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/, |
| 27 |
}; |
| 28 |
|
| 29 |
use craterust-analyzer cargo nusb 0.1.13 crate/::Errorrust-analyzer cargo nusb 0.1.13 Error#; |
| 30 |
|
| 31 |
use superrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/::Devicerust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/device/LinuxDevice#; |
| 32 |
|
| 33 |
static EPOLL_FDrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/EPOLL_FD.: OnceCellrust-analyzer cargo once_cell 1.20.3 sync/OnceCell#<OwnedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/OwnedFd#> = OnceCellrust-analyzer cargo once_cell 1.20.3 sync/OnceCell#::newrust-analyzer cargo once_cell 1.20.3 sync/impl#[`OnceCell<T>`]new().(); |
| 34 |
static WATCHESrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/WATCHES.: Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#<Slabrust-analyzer cargo slab 0.4.9 Slab#<Watchrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Watch#>> = Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]new().(Slabrust-analyzer cargo slab 0.4.9 Slab#::newrust-analyzer cargo slab 0.4.9 impl#[`Slab<T>`]new().()); |
| 35 |
|
| 36 |
pub(superrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/) enum Watchrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Watch# { |
| 37 |
Devicerust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Watch#Device#(Weakrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Weak#<Devicerust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/device/LinuxDevice#>), |
| 38 |
Fdrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Watch#Fd#(Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<AtomicWakerrust-analyzer cargo atomic-waker 1.1.2 AtomicWaker#>), |
| 39 |
} |
| 40 |
|
| 41 |
pub(superrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/) fn registerrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/register().(fd: BorrowedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/BorrowedFd#, watch: Watchrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Watch#, flags: EventFlagsrust-analyzer cargo rustix 1.1.4 backend/event/epoll/EventFlags#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, Errorrust-analyzer cargo nusb 0.1.13 Error#> { |
| 42 |
let mut start_thread = false; |
| 43 |
let epoll_fd = EPOLL_FDrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/EPOLL_FD..get_or_try_initrust-analyzer cargo once_cell 1.20.3 sync/impl#[`OnceCell<T>`]get_or_try_init().(|| { |
| 44 |
start_thread = true; |
| 45 |
epollrust-analyzer cargo rustix 1.1.4 event/epoll/::createrust-analyzer cargo rustix 1.1.4 event/epoll/create().(epollrust-analyzer cargo rustix 1.1.4 event/epoll/::CreateFlagsrust-analyzer cargo rustix 1.1.4 backend/event/epoll/CreateFlags#::CLOEXECrust-analyzer cargo rustix 1.1.4 backend/event/epoll/impl#[CreateFlags]CLOEXEC.) |
| 46 |
})?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 47 |
|
| 48 |
let id = { |
| 49 |
let mut watches = WATCHESrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/WATCHES..lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 50 |
watches.insertrust-analyzer cargo slab 0.4.9 impl#[`Slab<T>`]insert().(watch) |
| 51 |
}; |
| 52 |
|
| 53 |
if start_thread { |
| 54 |
threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/spawn().(event_looprust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/event_loop().); |
| 55 |
} |
| 56 |
|
| 57 |
let data = EventDatarust-analyzer cargo rustix 1.1.4 event/epoll/EventData#::new_u64rust-analyzer cargo rustix 1.1.4 event/epoll/impl#[EventData]new_u64().(id as u64); |
| 58 |
epollrust-analyzer cargo rustix 1.1.4 event/epoll/::addrust-analyzer cargo rustix 1.1.4 event/epoll/add().(epoll_fd, fd, data, flags)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 59 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(id) |
| 60 |
} |
| 61 |
|
| 62 |
pub(superrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/) fn unregister_fdrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/unregister_fd().(fd: BorrowedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/BorrowedFd#) { |
| 63 |
let epoll_fd = EPOLL_FDrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/EPOLL_FD..getrust-analyzer cargo once_cell 1.20.3 sync/impl#[`OnceCell<T>`]get().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 64 |
epollrust-analyzer cargo rustix 1.1.4 event/epoll/::deleterust-analyzer cargo rustix 1.1.4 event/epoll/delete().(epoll_fd, fd).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().(); |
| 65 |
} |
| 66 |
|
| 67 |
pub(superrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/) fn unregisterrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/unregister().(fd: BorrowedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/BorrowedFd#, events_id: usize) { |
| 68 |
let epoll_fd = EPOLL_FDrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/EPOLL_FD..getrust-analyzer cargo once_cell 1.20.3 sync/impl#[`OnceCell<T>`]get().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 69 |
epollrust-analyzer cargo rustix 1.1.4 event/epoll/::deleterust-analyzer cargo rustix 1.1.4 event/epoll/delete().(epoll_fd, fd).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().(); |
| 70 |
WATCHESrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/WATCHES..lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().removerust-analyzer cargo slab 0.4.9 impl#[`Slab<T>`]remove().(events_id); |
| 71 |
} |
| 72 |
|
| 73 |
fn event_looprust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/event_loop().() { |
| 74 |
let epoll_fd = EPOLL_FDrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/EPOLL_FD..getrust-analyzer cargo once_cell 1.20.3 sync/impl#[`OnceCell<T>`]get().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| •75 |
let mut event_list = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(4); |
| 76 |
loop { |
| •77 |
retry_on_intrrust-analyzer cargo rustix 1.1.4 io/errno/retry_on_intr().(|| epollrust-analyzer cargo rustix 1.1.4 event/epoll/::waitrust-analyzer cargo rustix 1.1.4 event/epoll/wait().(epoll_fd, spare_capacityrust-analyzer cargo rustix 1.1.4 buffer/spare_capacity().(&mut event_list), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 78 |
for event in &event_list { |
| 79 |
let key = event.datarust-analyzer cargo rustix 1.1.4 event/epoll/Event#data..u64rust-analyzer cargo rustix 1.1.4 event/epoll/impl#[EventData]u64().() as usize; |
| 80 |
let lock = WATCHESrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/WATCHES..lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 81 |
let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(watch) = lock.getrust-analyzer cargo slab 0.4.9 impl#[`Slab<T>`]get().(key) else { continue }; |
| 82 |
|
| 83 |
match watch { |
| 84 |
Watchrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Watch#::Devicerust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Watch#Device#(w) => { |
| 85 |
if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(device) = w.upgraderust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Weak<T, A>`]upgrade().() { |
| 86 |
droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(lock); |
| 87 |
device.handle_eventsrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/device/impl#[LinuxDevice]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 |
Watchrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Watch#::Fdrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Watch#Fd#(waker) => waker.wakerust-analyzer cargo atomic-waker 1.1.2 impl#[AtomicWaker]wake().(), |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
pub(craterust-analyzer cargo nusb 0.1.13 crate/) struct Asyncrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Async#<T> { |
| 99 |
pub(craterust-analyzer cargo nusb 0.1.13 crate/) innerrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Async#inner.: T, |
| 100 |
wakerrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Async#waker.: Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<AtomicWakerrust-analyzer cargo atomic-waker 1.1.2 AtomicWaker#>, |
| 101 |
idrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Async#id.: usize, |
| 102 |
} |
| 103 |
|
| 104 |
impl<T: AsFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/AsFd#> Asyncrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Async#<T> { |
| 105 |
pub fn newrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/impl#[`Async<T>`]new().(inner: T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/impl#[`Async<T>`], iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> { |
| 106 |
let waker = Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T>`]new().(AtomicWakerrust-analyzer cargo atomic-waker 1.1.2 AtomicWaker#::newrust-analyzer cargo atomic-waker 1.1.2 impl#[AtomicWaker]new().()); |
| 107 |
let id = registerrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/register().(inner.as_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/AsFd#as_fd().(), Watchrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Watch#::Fdrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Watch#Fd#(waker.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().()), EventFlagsrust-analyzer cargo rustix 1.1.4 backend/event/epoll/EventFlags#::emptyrust-analyzer cargo rustix 1.1.4 backend/event/epoll/impl#[EventFlags]empty().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 108 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Asyncrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Async# { innerrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Async#inner., idrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Async#id., wakerrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Async#waker. }) |
| 109 |
} |
| 110 |
|
| 111 |
pub fn registerrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/impl#[`Async<T>`]register().(&self, waker: &Wakerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/wake/Waker#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> { |
| 112 |
self.wakerrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Async#waker..registerrust-analyzer cargo atomic-waker 1.1.2 impl#[AtomicWaker]register().(waker); |
| 113 |
let epoll_fd = EPOLL_FDrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/EPOLL_FD..getrust-analyzer cargo once_cell 1.20.3 sync/impl#[`OnceCell<T>`]get().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 114 |
epollrust-analyzer cargo rustix 1.1.4 event/epoll/::modifyrust-analyzer cargo rustix 1.1.4 event/epoll/modify().( |
| 115 |
epoll_fd, |
| 116 |
self.innerrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Async#inner..as_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/AsFd#as_fd().(), |
| 117 |
EventDatarust-analyzer cargo rustix 1.1.4 event/epoll/EventData#::new_u64rust-analyzer cargo rustix 1.1.4 event/epoll/impl#[EventData]new_u64().(self.idrust-analyzer cargo nusb 0.1.13 platform/linux_usbfs/events/Async#id. as u64), |
| 118 |
EventFlagsrust-analyzer cargo rustix 1.1.4 backend/event/epoll/EventFlags#::ONESHOTrust-analyzer cargo rustix 1.1.4 backend/event/epoll/impl#[EventFlags]ONESHOT. rust-analyzer cargo rustix 1.1.4 backend/event/epoll/impl#[EventFlags][`BitOr<Self>`]bitor().|rust-analyzer cargo rustix 1.1.4 backend/event/epoll/impl#[EventFlags][`BitOr<Self>`]bitor(). rust-analyzer cargo rustix 1.1.4 backend/event/epoll/impl#[EventFlags][`BitOr<Self>`]bitor().EventFlagsrust-analyzer cargo rustix 1.1.4 backend/event/epoll/EventFlags#::INrust-analyzer cargo rustix 1.1.4 backend/event/epoll/impl#[EventFlags]IN., |
| 119 |
)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 120 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 121 |
} |
| 122 |
} |