Skip to content

Commit

Permalink
Handle destructors properly in conc::Atomic.
Browse files Browse the repository at this point in the history
Previously, they simple werent being run.
  • Loading branch information
ticki committed Jun 10, 2017
1 parent c7c79f8 commit d88fe23
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions conc/src/atomic.rs
Expand Up @@ -279,6 +279,20 @@ impl<T> Default for Atomic<T> {
}
}

impl<T> Drop for Atomic<T> {
fn drop(&mut self) {
// We use the neat `get_mut` to get around the overhead of atomics.
let ptr = self.inner.get_mut();

if !ptr.is_null() {
// As the read pointer was not null, we can safely call its destructor.
unsafe {
drop(Box::from_raw(ptr));
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 4 additions & 0 deletions conc/src/sync/treiber.rs
Expand Up @@ -123,6 +123,8 @@ mod tests {
assert_eq!(*stack.pop().unwrap(), 200);
assert_eq!(*stack.pop().unwrap(), 1);
assert!(stack.pop().is_none());

::gc();
}

#[test]
Expand All @@ -146,6 +148,8 @@ mod tests {
assert!(stack.pop().is_none());
assert!(stack.pop().is_none());
}

::gc();
}

#[test]
Expand Down

0 comments on commit d88fe23

Please sign in to comment.