From 2f7336ed2d4a54a467d650c74505f4e716572436 Mon Sep 17 00:00:00 2001 From: Kevin Mehall Date: Sun, 5 Jan 2025 22:49:14 -0700 Subject: [PATCH 1/2] windows: impl Send + Sync for HotplugWatch --- src/hotplug.rs | 6 ++++++ src/platform/windows_winusb/hotplug.rs | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/hotplug.rs b/src/hotplug.rs index 7bdc681..8a8d314 100644 --- a/src/hotplug.rs +++ b/src/hotplug.rs @@ -33,3 +33,9 @@ pub enum HotplugEvent { /// A device has been disconnected. Disconnected(DeviceId), } + +#[test] +fn assert_send_sync() { + fn require_send_sync() {} + require_send_sync::(); +} diff --git a/src/platform/windows_winusb/hotplug.rs b/src/platform/windows_winusb/hotplug.rs index 67cf96b..a151c13 100644 --- a/src/platform/windows_winusb/hotplug.rs +++ b/src/platform/windows_winusb/hotplug.rs @@ -115,6 +115,12 @@ impl WindowsHotplugWatch { } } +// Safety: Effectively a Box, 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 { From c59e0fe7ecceacda11382076035ad290b5cc5ffd Mon Sep 17 00:00:00 2001 From: Kevin Mehall Date: Sun, 5 Jan 2025 23:13:19 -0700 Subject: [PATCH 2/2] macos: impl Sync for HotplugWatch --- src/platform/macos_iokit/hotplug.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/platform/macos_iokit/hotplug.rs b/src/platform/macos_iokit/hotplug.rs index 1b1c88a..175b9be 100644 --- a/src/platform/macos_iokit/hotplug.rs +++ b/src/platform/macos_iokit/hotplug.rs @@ -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,