2021-12-30 00:14:23 +00:00
|
|
|
/*
|
2022-06-02 18:51:06 +00:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2021-12-30 00:14:23 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use reverie::Tool;
|
|
|
|
|
2022-11-03 19:53:11 +00:00
|
|
|
#[derive(Debug, Default, Clone)]
|
2021-12-30 00:14:23 +00:00
|
|
|
struct LocalState;
|
|
|
|
|
|
|
|
#[reverie::tool]
|
2022-11-18 20:36:51 +00:00
|
|
|
impl Tool for LocalState {
|
|
|
|
type GlobalState = ();
|
|
|
|
type ThreadState = ();
|
|
|
|
}
|
2021-12-30 00:14:23 +00:00
|
|
|
|
|
|
|
#[cfg(all(not(sanitized), test))]
|
|
|
|
mod tests {
|
2022-06-30 21:56:20 +00:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
|
|
|
use std::sync::atomic::Ordering;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::thread;
|
|
|
|
use std::time;
|
2021-12-30 00:14:23 +00:00
|
|
|
|
2022-08-06 15:21:44 +00:00
|
|
|
use reverie_ptrace::testing::check_fn;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2021-12-30 00:14:23 +00:00
|
|
|
#[test]
|
|
|
|
fn run_guest_spinlock_test() {
|
|
|
|
check_fn::<LocalState, _>(move || {
|
|
|
|
let lock = Arc::new(AtomicUsize::new(0));
|
|
|
|
let mut handles: Vec<_> = (0..10)
|
|
|
|
.map(|_| {
|
|
|
|
let lock = lock.clone();
|
|
|
|
thread::spawn(move || while lock.load(Ordering::Acquire) != 10 {})
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
handles.push(thread::spawn(move || {
|
|
|
|
for _ in 0..10 {
|
|
|
|
lock.fetch_add(1, Ordering::Release);
|
|
|
|
let dur = time::Duration::from_millis(10);
|
|
|
|
thread::sleep(dur);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
for h in handles {
|
|
|
|
let _ = h.join();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|