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

feat: add forget method to semaphore guards #73

Merged
merged 7 commits into from
Dec 11, 2023

Conversation

hlbarber
Copy link
Contributor

@hlbarber hlbarber commented Dec 7, 2023

No description provided.

Copy link
Member

@notgull notgull left a comment

Choose a reason for hiding this comment

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

What advantage does this have over just calling mem::forget on the locks?

@hlbarber
Copy link
Contributor Author

hlbarber commented Dec 7, 2023

If we don't provide an explicit forget method and want to provide this functionality through documentation, "prevent this permit from returning to the semaphore by calling mem::forget", we are less able to evolve the internals. It would forbid changes which would break this behaviour.

@hlbarber
Copy link
Contributor Author

hlbarber commented Dec 7, 2023

In fact, I think the forget on the owned permit is incorrect here - we want the Arc destructor to run.

Copy link
Member

@notgull notgull left a comment

Choose a reason for hiding this comment

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

I get the idea, although it seems somewhat useless without a manual unlock method. Although we can save that for a future PR.

src/semaphore.rs Outdated Show resolved Hide resolved
src/semaphore.rs Outdated

// Drop the inner `Arc` in order to decrement the reference count.
// SAFETY: `manual` not used after this
let _arc = unsafe { addr_of_mut!(manual.0).read() };
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather avoid the unsafe here. I would prefer to make the type a wrapper around Option<Arc<...>>, and then make this just take the Arc out without any of the other dropping.

Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. Arc has only one niche currently, it would be a shame to use it up.
  2. There is already another unsafe block in this library that performs the same operation. (Hopefully, my RFC 3466 will be accepted someday and then neither of these will be necessary…)

Copy link
Member

Choose a reason for hiding this comment

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

For both cases, I value safety over precedent here. smol-rs doesn't have the resources to conduct thorough unsafe audits (outside of off-the-shelf tools like Miri), so the less unsafe code we have the better.

Copy link
Collaborator

Choose a reason for hiding this comment

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

For both cases

What do you mean by "both cases" here? Are you saying I should remove the unsafe in the RwLock impl also? (The performance cost would be much greater over there compared to here.)

Copy link
Member

Choose a reason for hiding this comment

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

Not in this PR, but I am skeptical of premature optimizations like this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I've removed the unsafe.

Copy link
Collaborator

@Jules-Bertholet Jules-Bertholet Dec 9, 2023

Choose a reason for hiding this comment

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

(The performance cost would be much greater over there compared to here.)

Specifically, it would mean a branch on every access to the value behind the lock guard.

Copy link
Member

Choose a reason for hiding this comment

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

We can benchmark this in the future

Copy link
Member

@notgull notgull left a comment

Choose a reason for hiding this comment

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

Thanks!

@Jules-Bertholet Jules-Bertholet merged commit 3e855f5 into smol-rs:master Dec 11, 2023
9 checks passed
@notgull notgull mentioned this pull request Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants