Skip to content

Commit

Permalink
Merge pull request #101 from elmarco/interrupt-event
Browse files Browse the repository at this point in the history
Add Context::interrupt_handle_events()
  • Loading branch information
a1ien authored Aug 18, 2021
2 parents 790d8d2 + 11bca16 commit e43f266
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
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

0 comments on commit e43f266

Please sign in to comment.