Skip to content

add SyncActivateOnDrop #464

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
45 changes: 45 additions & 0 deletions timely/src/scheduling/activate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
wrapped: T,
Expand Down Expand Up @@ -328,3 +331,45 @@ impl<T> Drop for ActivateOnDrop<T> {
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<T> {
wrapped: T,
activator: SyncActivator,
}

impl<T> SyncActivateOnDrop<T> {
/// Wraps an element so that it is unparked on drop.
pub fn new(wrapped: T, activator: SyncActivator) -> Self {
Self { wrapped, activator}
}
}

impl<T> Deref for SyncActivateOnDrop<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.wrapped
}
}

impl<T> DerefMut for SyncActivateOnDrop<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.wrapped
}
}

impl<T> Drop for SyncActivateOnDrop<T> {
fn drop(&mut self) {
let _ = self.activator.activate();
}
}