Skip to content

Commit

Permalink
Add Default impl for MutexNode (#10)
Browse files Browse the repository at this point in the history
* Add Default impl for MutexNode

* Add test for MutexNode init
  • Loading branch information
pedromfedricci authored Mar 22, 2024
1 parent 702720a commit 4585cc7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/raw/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ impl MutexNode {
}
}

impl Default for MutexNode {
fn default() -> Self {
Self::new()
}
}

/// A mutual exclusion primitive useful for protecting shared data.
///
/// This mutex will block threads waiting for the lock to become available. The
Expand Down Expand Up @@ -656,6 +662,21 @@ mod test {
use crate::raw::yields::Mutex;
use crate::test::tests;

#[test]
fn node_default_and_new_init() {
use super::MutexNode;

let mut d = MutexNode::default();
let dinit = d.initialize();
assert!(dinit.next.get_mut().is_null());
assert!(*dinit.locked.get_mut());

let mut n = MutexNode::new();
let ninit = n.initialize();
assert!(ninit.next.get_mut().is_null());
assert!(*ninit.locked.get_mut());
}

#[test]
fn lots_and_lots() {
tests::lots_and_lots::<Mutex<_>>();
Expand Down
4 changes: 1 addition & 3 deletions src/thread_local/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,13 +762,11 @@ unsafe impl<T: ?Sized, R: Relax> crate::loom::Guard for MutexGuard<'_, T, R> {

#[cfg(all(not(loom), test))]
mod test {
use super::MutexUnchecked as InnerUnchecked;

use crate::relax::Yield;
use crate::test::tests;
use crate::thread_local::yields::Mutex;

type MutexUnchecked<T> = InnerUnchecked<T, Yield>;
type MutexUnchecked<T> = super::MutexUnchecked<T, Yield>;

#[test]
fn lots_and_lots() {
Expand Down

0 comments on commit 4585cc7

Please sign in to comment.