Skip to content

Commit

Permalink
Sticky drops from the registry eagerly now
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Oct 18, 2022
1 parent 1b1304d commit a7e6ace
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,8 @@ All notable changes to similar are documented here.
* `Fragile` no longer boxes internally.
* `Sticky` and `SemiSticky` now require the use of stack tokens.
For more information see [#26](https://github.com/mitsuhiko/fragile/issues/26)
* `Sticky` now tries to drop entries from the thread local registry eagerly
if it's dropped on the right thread.

## 1.2.1

Expand Down
9 changes: 9 additions & 0 deletions src/registry.rs
Expand Up @@ -34,6 +34,11 @@ mod slab_impl {
let _ = thread_id;
REGISTRY.with(|registry| unsafe { (*registry.get()).0.remove(item_id) })
}

pub fn try_remove(item_id: ItemId, thread_id: NonZeroUsize) -> Option<Entry> {
let _ = thread_id;
REGISTRY.with(|registry| unsafe { (*registry.get()).0.try_remove(item_id) })
}
}

#[cfg(not(feature = "slab"))]
Expand Down Expand Up @@ -76,6 +81,10 @@ mod map_impl {
REGISTRY
.with(|registry| unsafe { (*registry.get()).0.remove(&(thread_id, item_id)).unwrap() })
}

pub fn try_remove(item_id: ItemId, thread_id: NonZeroUsize) -> Option<Entry> {
REGISTRY.with(|registry| unsafe { (*registry.get()).0.remove(&(thread_id, item_id)) })
}
}

#[cfg(feature = "slab")]
Expand Down
12 changes: 12 additions & 0 deletions src/sticky.rs
Expand Up @@ -35,12 +35,24 @@ pub struct Sticky<T: 'static> {

impl<T> Drop for Sticky<T> {
fn drop(&mut self) {
// if the type needs dropping we can only do so on the
// right thread. worst case we leak the value until the
// thread dies.
if mem::needs_drop::<T>() {
unsafe {
if self.is_valid() {
self.unsafe_take_value();
}
}

// otherwise we take the liberty to drop the value
// right here and now. We can however only do that if
// we are on the right thread. If we are not, we again
// need to wait for the thread to shut down.
} else if let Some(entry) = registry::try_remove(self.item_id, self.thread_id) {
unsafe {
(entry.drop)(entry.ptr);
}
}
}
}
Expand Down

0 comments on commit a7e6ace

Please sign in to comment.