1
Fork 0

mri: add track_caller to thread spawning methods for better backtraces

This commit is contained in:
Ralf Jung 2024-12-20 15:03:51 +01:00
parent 5dfe648b45
commit 8b2b6359f9
3 changed files with 6 additions and 0 deletions

View file

@ -45,6 +45,7 @@ unsafe impl Sync for Thread {}
impl Thread { impl Thread {
// unsafe: see thread::Builder::spawn_unchecked for safety requirements // unsafe: see thread::Builder::spawn_unchecked for safety requirements
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> { pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
let p = Box::into_raw(Box::new(p)); let p = Box::into_raw(Box::new(p));
let mut native: libc::pthread_t = mem::zeroed(); let mut native: libc::pthread_t = mem::zeroed();

View file

@ -19,6 +19,7 @@ pub struct Thread {
impl Thread { impl Thread {
// unsafe: see thread::Builder::spawn_unchecked for safety requirements // unsafe: see thread::Builder::spawn_unchecked for safety requirements
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> { pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
let p = Box::into_raw(Box::new(p)); let p = Box::into_raw(Box::new(p));

View file

@ -391,6 +391,7 @@ impl Builder {
/// handler.join().unwrap(); /// handler.join().unwrap();
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub fn spawn<F, T>(self, f: F) -> io::Result<JoinHandle<T>> pub fn spawn<F, T>(self, f: F) -> io::Result<JoinHandle<T>>
where where
F: FnOnce() -> T, F: FnOnce() -> T,
@ -458,6 +459,7 @@ impl Builder {
/// ///
/// [`io::Result`]: crate::io::Result /// [`io::Result`]: crate::io::Result
#[stable(feature = "thread_spawn_unchecked", since = "1.82.0")] #[stable(feature = "thread_spawn_unchecked", since = "1.82.0")]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub unsafe fn spawn_unchecked<F, T>(self, f: F) -> io::Result<JoinHandle<T>> pub unsafe fn spawn_unchecked<F, T>(self, f: F) -> io::Result<JoinHandle<T>>
where where
F: FnOnce() -> T, F: FnOnce() -> T,
@ -467,6 +469,7 @@ impl Builder {
Ok(JoinHandle(unsafe { self.spawn_unchecked_(f, None) }?)) Ok(JoinHandle(unsafe { self.spawn_unchecked_(f, None) }?))
} }
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
unsafe fn spawn_unchecked_<'scope, F, T>( unsafe fn spawn_unchecked_<'scope, F, T>(
self, self,
f: F, f: F,
@ -721,6 +724,7 @@ impl Builder {
/// [`join`]: JoinHandle::join /// [`join`]: JoinHandle::join
/// [`Err`]: crate::result::Result::Err /// [`Err`]: crate::result::Result::Err
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub fn spawn<F, T>(f: F) -> JoinHandle<T> pub fn spawn<F, T>(f: F) -> JoinHandle<T>
where where
F: FnOnce() -> T, F: FnOnce() -> T,