Skip to content

Commit

Permalink
mri: add track_caller to thread spawning methods for better backtraces
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Dec 20, 2024
1 parent 5dfe648 commit 8b2b635
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions library/std/src/sys/pal/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ unsafe impl Sync for Thread {}

impl Thread {
// 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> {
let p = Box::into_raw(Box::new(p));
let mut native: libc::pthread_t = mem::zeroed();
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/pal/windows/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct Thread {

impl Thread {
// 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> {
let p = Box::into_raw(Box::new(p));

Expand Down
4 changes: 4 additions & 0 deletions library/std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ impl Builder {
/// handler.join().unwrap();
/// ```
#[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>>
where
F: FnOnce() -> T,
Expand Down Expand Up @@ -458,6 +459,7 @@ impl Builder {
///
/// [`io::Result`]: crate::io::Result
#[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>>
where
F: FnOnce() -> T,
Expand All @@ -467,6 +469,7 @@ impl Builder {
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>(
self,
f: F,
Expand Down Expand Up @@ -721,6 +724,7 @@ impl Builder {
/// [`join`]: JoinHandle::join
/// [`Err`]: crate::result::Result::Err
#[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>
where
F: FnOnce() -> T,
Expand Down

0 comments on commit 8b2b635

Please sign in to comment.