forked from mirror/async-std
fix test code
This commit is contained in:
parent
2b44c1be2e
commit
d7ee29a03f
1 changed files with 24 additions and 24 deletions
|
@ -5,30 +5,6 @@ use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use crossbeam_utils::Backoff;
|
use crossbeam_utils::Backoff;
|
||||||
|
|
||||||
/// A simple spinlock.
|
/// A simple spinlock.
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// # async_std::task::block_on(async {
|
|
||||||
/// #
|
|
||||||
/// use async_std::sync::{Arc, Spinlock};
|
|
||||||
/// use async_std::task;
|
|
||||||
///
|
|
||||||
/// let m = Arc::new(Spinlock::new(0));
|
|
||||||
/// let mut tasks = vec![];
|
|
||||||
///
|
|
||||||
/// for _ in 0..10 {
|
|
||||||
/// let m = m.clone();
|
|
||||||
/// tasks.push(task::spawn(async move {
|
|
||||||
/// *m.lock() += 1;
|
|
||||||
/// }));
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// for t in tasks {
|
|
||||||
/// t.await;
|
|
||||||
/// }
|
|
||||||
/// assert_eq!(*m.lock(), 10);
|
|
||||||
/// #
|
|
||||||
/// # })
|
|
||||||
/// ```
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Spinlock<T> {
|
pub struct Spinlock<T> {
|
||||||
locked: AtomicBool,
|
locked: AtomicBool,
|
||||||
|
@ -94,3 +70,27 @@ impl<'a, T> DerefMut for SpinlockGuard<'a, T> {
|
||||||
unsafe { &mut *self.parent.value.get() }
|
unsafe { &mut *self.parent.value.get() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn spinlock() {
|
||||||
|
crate::task::block_on(async {
|
||||||
|
use crate::sync::{Arc, Spinlock};
|
||||||
|
use crate::task;
|
||||||
|
|
||||||
|
let m = Arc::new(Spinlock::new(0));
|
||||||
|
let mut tasks = vec![];
|
||||||
|
|
||||||
|
for _ in 0..10 {
|
||||||
|
let m = m.clone();
|
||||||
|
tasks.push(task::spawn(async move {
|
||||||
|
*m.lock() += 1;
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
for t in tasks {
|
||||||
|
t.await;
|
||||||
|
}
|
||||||
|
assert_eq!(*m.lock(), 10);
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue