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

Open
wants to merge 3 commits into
base: task/r-test-lock-static-mut
Choose a base branch
from

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
Comment on lines +121 to +122
// Refcell safety: Only called on the R thread and we make sure not to
// recurse into `DeviceContext` methods.
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no refcell in this function IIUC

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

@@ -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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants