From 658a9f66b3ffffe1c4cfd90482e1953508ea68ce Mon Sep 17 00:00:00 2001 From: Gus Wynn Date: Mon, 25 Apr 2022 14:21:56 -0700 Subject: [PATCH] add SyncActivateOnDrop --- timely/src/scheduling/activate.rs | 45 +++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/timely/src/scheduling/activate.rs b/timely/src/scheduling/activate.rs index 79b69d24d..7e4327341 100644 --- a/timely/src/scheduling/activate.rs +++ b/timely/src/scheduling/activate.rs @@ -294,6 +294,9 @@ impl std::fmt::Display for SyncActivationError { impl std::error::Error for SyncActivationError {} /// A wrapper that unparks on drop. +/// +/// The wrapped value can be `()` if you only desire an +/// `Activator` that activates on drop. #[derive(Debug)] pub struct ActivateOnDrop { wrapped: T, @@ -328,3 +331,45 @@ impl Drop for ActivateOnDrop { self.activator.borrow_mut().activate(&self.address[..]); } } + +/// A _thread-safe_ wrapper that unparks on drop. +/// +/// The wrapped value can be `()` if you only desire an +/// `SyncActivator` that activates on drop. Note this has a +/// slightly different `new` api than `ActivateOnDrop` as it +/// involves a `SyncActivator` which requires ownership of +/// the address. +/// +/// Note this is best effort, and `SyncActivationError`'s will +/// be ignored on drop +#[derive(Debug)] +pub struct SyncActivateOnDrop { + wrapped: T, + activator: SyncActivator, +} + +impl SyncActivateOnDrop { + /// Wraps an element so that it is unparked on drop. + pub fn new(wrapped: T, activator: SyncActivator) -> Self { + Self { wrapped, activator} + } +} + +impl Deref for SyncActivateOnDrop { + type Target = T; + fn deref(&self) -> &Self::Target { + &self.wrapped + } +} + +impl DerefMut for SyncActivateOnDrop { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.wrapped + } +} + +impl Drop for SyncActivateOnDrop { + fn drop(&mut self) { + let _ = self.activator.activate(); + } +}