From 6682d6ff49877a366fefc5c7cbd020ab178f0826 Mon Sep 17 00:00:00 2001 From: Harry Barber Date: Thu, 7 Dec 2023 00:28:43 +0000 Subject: [PATCH 1/7] add `forget` method to semaphore guards --- src/semaphore.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/semaphore.rs b/src/semaphore.rs index b2c7b1d..bddb585 100644 --- a/src/semaphore.rs +++ b/src/semaphore.rs @@ -337,6 +337,13 @@ impl EventListenerFuture for AcquireArcInner { #[derive(Debug)] pub struct SemaphoreGuard<'a>(&'a Semaphore); +impl SemaphoreGuard<'_> { + /// Drops the guard _without_ releasing the acquired permit. + pub fn forget(self) { + let _ = std::mem::ManuallyDrop::new(self); + } +} + impl Drop for SemaphoreGuard<'_> { fn drop(&mut self) { self.0.count.fetch_add(1, Ordering::AcqRel); @@ -349,6 +356,13 @@ impl Drop for SemaphoreGuard<'_> { #[derive(Debug)] pub struct SemaphoreGuardArc(Arc); +impl SemaphoreGuardArc { + /// Drops the guard _without_ releasing the acquired permit. + pub fn forget(self) { + let _ = std::mem::ManuallyDrop::new(self); + } +} + impl Drop for SemaphoreGuardArc { fn drop(&mut self) { self.0.count.fetch_add(1, Ordering::AcqRel); From 9a45c5570e4692fc301f7ea4c37eafa5563261b8 Mon Sep 17 00:00:00 2001 From: Harry Barber Date: Thu, 7 Dec 2023 00:56:53 +0000 Subject: [PATCH 2/7] use `core` rather than `std` --- src/semaphore.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/semaphore.rs b/src/semaphore.rs index bddb585..8622851 100644 --- a/src/semaphore.rs +++ b/src/semaphore.rs @@ -340,7 +340,7 @@ pub struct SemaphoreGuard<'a>(&'a Semaphore); impl SemaphoreGuard<'_> { /// Drops the guard _without_ releasing the acquired permit. pub fn forget(self) { - let _ = std::mem::ManuallyDrop::new(self); + let _ = core::mem::ManuallyDrop::new(self); } } @@ -359,7 +359,7 @@ pub struct SemaphoreGuardArc(Arc); impl SemaphoreGuardArc { /// Drops the guard _without_ releasing the acquired permit. pub fn forget(self) { - let _ = std::mem::ManuallyDrop::new(self); + let _ = core::mem::ManuallyDrop::new(self); } } From b47cf525fcdc4dca16d108172895778da6e6e08f Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Thu, 7 Dec 2023 13:46:54 -0500 Subject: [PATCH 3/7] Drop the `Arc` in `SemaphoreGuardArc::forget` --- src/semaphore.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/semaphore.rs b/src/semaphore.rs index 8622851..ada61f8 100644 --- a/src/semaphore.rs +++ b/src/semaphore.rs @@ -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; @@ -339,6 +340,7 @@ 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); } @@ -358,8 +360,14 @@ pub struct SemaphoreGuardArc(Arc); impl SemaphoreGuardArc { /// Drops the guard _without_ releasing the acquired permit. + /// (Will still decrement the `Arc` reference count.) + #[inline] pub fn forget(self) { - let _ = core::mem::ManuallyDrop::new(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() }; } } From edf7482697fc4181fe4a48a8179e55e9709c1edd Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Thu, 7 Dec 2023 23:10:00 -0500 Subject: [PATCH 4/7] Use `mem::forget` --- src/semaphore.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/semaphore.rs b/src/semaphore.rs index ada61f8..c5c116c 100644 --- a/src/semaphore.rs +++ b/src/semaphore.rs @@ -342,7 +342,7 @@ impl SemaphoreGuard<'_> { /// Drops the guard _without_ releasing the acquired permit. #[inline] pub fn forget(self) { - let _ = core::mem::ManuallyDrop::new(self); + core::mem::forget(self); } } From a3856b155b519a590c6f8276a7f8e8db4cf9eaea Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Fri, 8 Dec 2023 20:44:25 -0500 Subject: [PATCH 5/7] Replace `unsafe` with `Option::take` --- src/semaphore.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/semaphore.rs b/src/semaphore.rs index c5c116c..ed8bc56 100644 --- a/src/semaphore.rs +++ b/src/semaphore.rs @@ -1,6 +1,6 @@ use core::fmt; +use core::mem; use core::pin::Pin; -use core::ptr::addr_of_mut; use core::sync::atomic::{AtomicUsize, Ordering}; use core::task::Poll; @@ -342,7 +342,7 @@ impl SemaphoreGuard<'_> { /// Drops the guard _without_ releasing the acquired permit. #[inline] pub fn forget(self) { - core::mem::forget(self); + mem::forget(self); } } @@ -356,24 +356,24 @@ impl Drop for SemaphoreGuard<'_> { /// An owned guard that releases the acquired permit. #[clippy::has_significant_drop] #[derive(Debug)] -pub struct SemaphoreGuardArc(Arc); +pub struct SemaphoreGuardArc(Option>); 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); - + pub fn forget(mut 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() }; + // FIXME: get rid of the `Option` once RFC 3466 or equivalent is merged. + drop(self.0.take()); + mem::forget(self); } } impl Drop for SemaphoreGuardArc { fn drop(&mut self) { - self.0.count.fetch_add(1, Ordering::AcqRel); - self.0.event.notify(1); + let opt = self.0.take().unwrap(); + opt.count.fetch_add(1, Ordering::AcqRel); + opt.event.notify(1); } } From 9b7abd123f6a40a9f0192a7c78ab0737dc5313aa Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Sat, 9 Dec 2023 14:11:41 -0500 Subject: [PATCH 6/7] Reword a comment --- src/semaphore.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/semaphore.rs b/src/semaphore.rs index ed8bc56..acb8a3b 100644 --- a/src/semaphore.rs +++ b/src/semaphore.rs @@ -364,7 +364,7 @@ impl SemaphoreGuardArc { #[inline] pub fn forget(mut self) { // Drop the inner `Arc` in order to decrement the reference count. - // FIXME: get rid of the `Option` once RFC 3466 or equivalent is merged. + // FIXME: get rid of the `Option` once RFC 3466 or equivalent becomes available. drop(self.0.take()); mem::forget(self); } From 612bb84708bebe30584008bc2842b068c27d1669 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Mon, 11 Dec 2023 00:12:54 -0500 Subject: [PATCH 7/7] Fix error --- src/semaphore.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/semaphore.rs b/src/semaphore.rs index acb8a3b..4f28055 100644 --- a/src/semaphore.rs +++ b/src/semaphore.rs @@ -152,7 +152,7 @@ impl Semaphore { Ordering::AcqRel, Ordering::Acquire, ) { - Ok(_) => return Some(SemaphoreGuardArc(self.clone())), + Ok(_) => return Some(SemaphoreGuardArc(Some(self.clone()))), Err(c) => count = c, } }