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

Remove static mut in graphics_device.rs #674

Merged
merged 4 commits into from
Jan 27, 2025
Merged

Remove static mut in graphics_device.rs #674

merged 4 commits into from
Jan 27, 2025

Conversation

lionel-
Copy link
Contributor

@lionel- lionel- commented Jan 24, 2025

Progress towards #661.

  • DEVICE_CONTEXT is now a thread-local variable to reflect the fact it should only be accessed from the R thread.

  • It's wrapped in a RefCell that is initialized from RMain on startup. All subsequent accesses are read-only and never panic.

  • The device context requires mutable state. Where possible we use Cell which never panics. In a couple of places we use RefCell with care not to double-borrow on mutation, mainly by only performing short borrows to prevent holding a reference while recusing into a device context method by accident. (I haven't examined whether that would be possible in principle to recurse into a method as I'm not familiar with the graphics device code and its R hooks. I just made sure the borrows were short or at least only invoked pure Rust code.)

@lionel- lionel- force-pushed the task/gd-static-mut branch from 26ce4d6 to e76f980 Compare January 24, 2025 12:37
@lionel- lionel- requested a review from DavisVaughan January 24, 2025 12:58
@lionel- lionel- force-pushed the task/gd-static-mut branch from e76f980 to 5fa82bf Compare January 24, 2025 13:00
@lionel- lionel- changed the base branch from main to task/r-test-lock-static-mut January 24, 2025 13:00
Copy link
Contributor

@DavisVaughan DavisVaughan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See slack for more comments

crates/ark/src/plots/graphics_device.rs Show resolved Hide resolved
crates/ark/src/plots/graphics_device.rs Outdated Show resolved Hide resolved
comm_manager_tx: Sender<CommManagerEvent>,
iopub_tx: Sender<IOPubMessage>,
dynamic_plots: bool,
) {
let id = unwrap!(self._id.clone(), None => {
// Refcell Safety: Short borrows in the file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the comment wrong? More like Cloning to borrow for as short as possible

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's referring to other borrows made by the device context. Not sure how best to phrase this.

@@ -271,10 +279,13 @@ impl DeviceContext {
}

// Save our new socket.
self._channels.insert(id.to_string(), socket.clone());
// Refcell Safety: Short borrows in the file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "in the file" mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DeviceContext is private to the file.

Base automatically changed from task/r-test-lock-static-mut to main January 27, 2025 10:39
@lionel- lionel- force-pushed the task/gd-static-mut branch from 6d2a559 to 49d2cfa Compare January 27, 2025 10:42
@lionel-
Copy link
Contributor Author

lionel- commented Jan 27, 2025

Logging this from Slack for reference: Atomics are for multi-threaded accesses but here it's single-threaded so using Cell is more appropriate.

Since this is all single-threaded, using RwLock instead of RefCell would only be trading panics for deadlocks and we prefer panics because they are actionable and self documenting for users

@lionel- lionel- merged commit c9bcfec into main Jan 27, 2025
5 of 6 checks passed
@lionel- lionel- deleted the task/gd-static-mut branch January 27, 2025 10:47
@github-actions github-actions bot locked and limited conversation to collaborators Jan 27, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants