crossbeam_epoch::Guard::defer

Now, again, how is the following in TreiberStack::try_pop a safe operation?

guard.defer(move || head_shared.into_owned());
return Some(ptr::read(&(*head).data));

What we need to explore now is defer, which lives in crossbeam-epoch at src/guard.rs:

    pub unsafe fn defer<F, R>(&self, f: F)
    where
        F: FnOnce() -> R,
    {
        let garbage = Garbage::new(|| drop(f()));

        if let Some(local) = self.local.as_ref() {
            local.defer(garbage, self);
        }
    }

We need to understand Garbage. It's defined in src/garbage.rs as:

pub struct Garbage {
    func: Deferred,
}


unsafe impl Sync for Garbage {}
unsafe impl Send for Garbage {}

The implementation of Garbage is very brief: 

impl Garbage { /// Make a closure that will later be called. pub fn new<F: ...

Get Hands-On Concurrency with Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.