Skip to content

Commit

Permalink
Merge pull request #105 from kevinmehall/windows-hotplug-send-sync
Browse files Browse the repository at this point in the history
impl Send + Sync for HotplugWatch
  • Loading branch information
kevinmehall authored Jan 6, 2025
2 parents b167c7e + c59e0fe commit 0dc8aa5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/hotplug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ pub enum HotplugEvent {
/// A device has been disconnected.
Disconnected(DeviceId),
}

#[test]
fn assert_send_sync() {
fn require_send_sync<T: Send + Sync>() {}
require_send_sync::<HotplugWatch>();
}
4 changes: 4 additions & 0 deletions src/platform/macos_iokit/hotplug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ impl MacHotplugWatch {
}
}

// Safety: Structurally Send and only method is &mut self, so Sync
// doesn't have any additional requirements.
unsafe impl Sync for MacHotplugWatch {}

fn register_notification(
port: &NotificationPort,
dictionary: &CFDictionary,
Expand Down
6 changes: 6 additions & 0 deletions src/platform/windows_winusb/hotplug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ impl WindowsHotplugWatch {
}
}

// Safety: Effectively a Box<HotplugInner>, which is Send+Sync;
// `registration` is accessed only in `Drop` and `CM_Unregister_Notification`
// docs mention using a threadpool to call the function.
unsafe impl Send for WindowsHotplugWatch {}
unsafe impl Sync for WindowsHotplugWatch {}

impl Drop for WindowsHotplugWatch {
fn drop(&mut self) {
unsafe {
Expand Down

0 comments on commit 0dc8aa5

Please sign in to comment.