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

Add Context::interrupt_handle_events() #101

Merged
merged 2 commits into from
Aug 18, 2021
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 libusb1-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ extern "system" {
completed: *mut c_int,
) -> c_int;
pub fn libusb_handle_events_locked(context: *mut libusb_context, tv: *const timeval) -> c_int;
pub fn libusb_interrupt_event_handler(context: *mut libusb_context);

pub fn libusb_try_lock_events(context: *mut libusb_context) -> c_int;
pub fn libusb_lock_events(context: *mut libusb_context);
Expand Down
19 changes: 14 additions & 5 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ pub trait UsbContext: Clone + Sized + Send + Sync {
/// [Hotplug::device_arrived] method is called when a new device is added to
/// the bus, and [Hotplug::device_left] is called when it is removed.
///
/// Devices can optionally be filtered by vendor ([vendor_id]) and device id
/// ([device_id]). If [enumerate] is `true`, then devices that are already
/// connected will cause your callback's `device_arrived()` method to be
/// called for them.
/// Devices can optionally be filtered by `vendor_id` and `device_id`. If
/// `enumerate` is `true`, then devices that are already connected will
/// cause your callback's `device_arrived()` method to be called for them.
///
/// The callback will remain registered until the returned [Registration] is
/// dropped, which can be done explicitly with [hotplug_unregister_callback].
/// dropped, which can be done explicitly with
/// [hotplug_unregister_callback][Self::hotplug_unregister_callback()].
#[must_use = "USB hotplug callbacks will be deregistered if the registration is dropped"]
fn hotplug_register_callback(
&self,
Expand Down Expand Up @@ -190,6 +190,15 @@ pub trait UsbContext: Clone + Sized + Send + Sync {
}
}

/// Interrupt any active thread that is handling events (for example with
/// [handle_events][`Self::handle_events()`]).
#[doc(alias = "libusb_interrupt_event_handler")]
fn interrupt_handle_events(&self) {
unsafe {
libusb_interrupt_event_handler(self.as_raw())
}
}

fn next_timeout(&self) -> crate::Result<Option<Duration>> {
let mut tv = timeval { tv_sec: 0, tv_usec: 0 };
let n = unsafe {
Expand Down