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

Added a method that allows for conditional mutable access #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

john-sharratt
Copy link

Adds a new method try_mut which if there is only a single reference to the ArcSwap grants mutable access to the insides

Copy link
Owner

@vorner vorner left a comment

Choose a reason for hiding this comment

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

As noted in the comments, this approach might actually work, but needs some more care:

  • Some argument/proof why this is OK to do.
  • Deciding how to deal with the Weak type.
  • semver compatibility.

return None;
}
Some(unsafe { ptr.as_mut().unwrap() })
}
Copy link
Owner

Choose a reason for hiding this comment

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

🤔 This might actually work.

However, some more argument / (non-formal) proof that this is actually OK and safe would be in place here.

Also, instead of having 3 unsafe blocks just one after another, maybe we can just put all three things into a block.

drop(guard);
assert_eq!(1, Arc::strong_count(&ptr));
}

Copy link
Owner

Choose a reason for hiding this comment

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

I think a check for the try_mut access, calls for a dedicated test (I know there's one „hidden“ in this long method, but we probably should have a test where it can be found easily). Especially, making sure it is not accessible in all the cases listed above.

@@ -67,6 +68,9 @@ pub unsafe trait RefCnt: Clone {
/// This must not be called by code outside of this crate.
unsafe fn from_ptr(ptr: *const Self::Base) -> Self;

/// Returns the reference count of the pointer.
unsafe fn ref_cnt(ptr: *const Self::Base) -> usize;
Copy link
Owner

Choose a reason for hiding this comment

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

Now, adding a new method into the trait would be a semver-breaking change.

Worse, the crate also offers (under a feature) RefCnt for Weak… and I don't think this method is valid (eg. makes sense) for that type.

if ptr.is_null() {
0
} else {
1
Copy link
Owner

Choose a reason for hiding this comment

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

This doesn't look correct. This implementation is for things like ArcSwap<Option<Arc<X>>. And the inner Arc can have multiple references.

@vorner
Copy link
Owner

vorner commented Jul 13, 2024

Also, I'd suggest you do some measurements. This method will likely be expensive (the wait_for_readers thing).

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