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
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/semaphore.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::fmt;
use core::pin::Pin;
use core::ptr::addr_of_mut;
use core::sync::atomic::{AtomicUsize, Ordering};
use core::task::Poll;

Expand Down Expand Up @@ -337,6 +338,14 @@ impl EventListenerFuture for AcquireArcInner {
#[derive(Debug)]
pub struct SemaphoreGuard<'a>(&'a Semaphore);

impl SemaphoreGuard<'_> {
/// Drops the guard _without_ releasing the acquired permit.
#[inline]
pub fn forget(self) {
let _ = core::mem::ManuallyDrop::new(self);
Jules-Bertholet marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl Drop for SemaphoreGuard<'_> {
fn drop(&mut self) {
self.0.count.fetch_add(1, Ordering::AcqRel);
Expand All @@ -349,6 +358,19 @@ impl Drop for SemaphoreGuard<'_> {
#[derive(Debug)]
pub struct SemaphoreGuardArc(Arc<Semaphore>);

impl SemaphoreGuardArc {
/// Drops the guard _without_ releasing the acquired permit.
/// (Will still decrement the `Arc` reference count.)
#[inline]
pub fn forget(self) {
let mut manual = core::mem::ManuallyDrop::new(self);

// 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

}
}

impl Drop for SemaphoreGuardArc {
fn drop(&mut self) {
self.0.count.fetch_add(1, Ordering::AcqRel);
Expand Down