-
Notifications
You must be signed in to change notification settings - Fork 2
Fix unsound unsafe impl Send + Sync for Display (issue #2) #4
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,6 +15,7 @@ use byteorder::{ByteOrder, LittleEndian}; | |||||||
| use bytes::{BufMut, BytesMut}; | ||||||||
| use hidapi::{HidApi, HidDevice, HidError, HidResult}; | ||||||||
| use pyo3::{exceptions::PyTypeError, prelude::*}; | ||||||||
| use std::sync::Mutex; | ||||||||
|
|
||||||||
| trait ResultExt<T> { | ||||||||
| fn to_py_err(self) -> PyResult<T>; | ||||||||
|
|
@@ -131,15 +132,19 @@ impl Rect { | |||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| /// Wrapper that marks HidDevice as Send. | ||||||||
| /// | ||||||||
| /// Safety: The C hidapi library serializes concurrent access to a device handle | ||||||||
| /// internally, so it is safe to send an HidDevice to another thread. | ||||||||
| struct SendableDevice(HidDevice); | ||||||||
| unsafe impl Send for SendableDevice {} | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like The inner device has its own and it's a restriction on https://github.com/ruabmbua/hidapi-rs/blob/7c8358fd275b394df588cc7ad513693b46d4d908/src/lib.rs#L121 So I think In general, the |
||||||||
|
|
||||||||
| /// Core structure defining the display and possible interactions. | ||||||||
| #[pyclass(frozen)] | ||||||||
| pub struct Display { | ||||||||
| device: HidDevice, | ||||||||
| device: Mutex<SendableDevice>, | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| } | ||||||||
|
|
||||||||
| unsafe impl Send for Display {} | ||||||||
| unsafe impl Sync for Display {} | ||||||||
|
|
||||||||
| #[pymethods] | ||||||||
| impl Display { | ||||||||
| /// Connects to the display and returns a `Display` struct for control. | ||||||||
|
|
@@ -151,7 +156,7 @@ impl Display { | |||||||
| let api = HidApi::new_without_enumerate().to_py_err()?; | ||||||||
| let device = api.open(VENDOR_ID, PRODUCT_ID).to_py_err()?; | ||||||||
|
|
||||||||
| Ok(Self { device }) | ||||||||
| Ok(Self { device: Mutex::new(SendableDevice(device)) }) | ||||||||
| } | ||||||||
|
|
||||||||
| /// Sets the mode for a region of the display. Note that this will always | ||||||||
|
|
@@ -166,10 +171,11 @@ impl Display { | |||||||
| buf.put_i16_le(area.x1); | ||||||||
| buf.put_i16_le(area.y1); | ||||||||
| buf.put_u16(crc16::State::<crc16::XMODEM>::calculate(&buf)); | ||||||||
| self.device.write(&buf).to_py_err()?; | ||||||||
| let device = self.device.lock().unwrap(); | ||||||||
| device.0.write(&buf).to_py_err()?; | ||||||||
|
|
||||||||
| let mut response: [u8; 32] = [0; 32]; | ||||||||
| self.device.read_timeout(&mut response, 200).to_py_err()?; | ||||||||
| device.0.read_timeout(&mut response, 200).to_py_err()?; | ||||||||
| match LittleEndian::read_u16(&response) { | ||||||||
| 0x00 => Err(PyTypeError::new_err("invalid command")), | ||||||||
| 0x01 => Err(PyTypeError::new_err("checksum incorrect")), | ||||||||
|
|
@@ -193,10 +199,11 @@ impl Display { | |||||||
|
|
||||||||
| let chksum = crc16::State::<crc16::XMODEM>::calculate(&buf); | ||||||||
| buf.put_u16(chksum); | ||||||||
| self.device.write(&buf).to_py_err()?; | ||||||||
| let device = self.device.lock().unwrap(); | ||||||||
| device.0.write(&buf).to_py_err()?; | ||||||||
|
|
||||||||
| let mut response: [u8; 16] = [0; 16]; | ||||||||
| self.device.read_timeout(&mut response, 200).to_py_err()?; | ||||||||
| device.0.read_timeout(&mut response, 200).to_py_err()?; | ||||||||
| match LittleEndian::read_u16(&response) { | ||||||||
| 0x00 => Err(PyTypeError::new_err("invalid command")), | ||||||||
| 0x01 => Err(PyTypeError::new_err("checksum incorrect")), | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't see this in the C code or the docs, but I'm less of an expert there; do you have a link to where this gets implemented / how it's accomplished?
If it's internally synchronized, it should implement
Syncas well. But given thehidapicrate doesn't implementSyncforHidDevice, I'm suspicious that it doesn't actually have internal synchronization.(Even if it does implement
Sync, we might want aMutexon our accessor to synchronize the Glider-level transactions.)