std/thread/
lifecycle.rs

1//! The inner logic for thread spawning and joining.
2
3use super::current::set_current;
4use super::id::ThreadId;
5use super::scoped::ScopeData;
6use super::thread::Thread;
7use super::{Result, spawnhook};
8use crate::cell::UnsafeCell;
9use crate::marker::PhantomData;
10use crate::mem::{ManuallyDrop, MaybeUninit};
11use crate::sync::Arc;
12use crate::sync::atomic::{Atomic, AtomicUsize, Ordering};
13use crate::sys::thread as imp;
14use crate::sys_common::{AsInner, IntoInner};
15use crate::{env, io, panic};
16
17#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
18pub(super) unsafe fn spawn_unchecked<'scope, F, T>(
19    name: Option<String>,
20    stack_size: Option<usize>,
21    no_hooks: bool,
22    scope_data: Option<Arc<ScopeData>>,
23    f: F,
24) -> io::Result<JoinInner<'scope, T>>
25where
26    F: FnOnce() -> T,
27    F: Send,
28    T: Send,
29{
30    let stack_size = stack_size.unwrap_or_else(|| {
31        static MIN: Atomic<usize> = AtomicUsize::new(0);
32
33        match MIN.load(Ordering::Relaxed) {
34            0 => {}
35            n => return n - 1,
36        }
37
38        let amt = env::var_os("RUST_MIN_STACK")
39            .and_then(|s| s.to_str().and_then(|s| s.parse().ok()))
40            .unwrap_or(imp::DEFAULT_MIN_STACK_SIZE);
41
42        // 0 is our sentinel value, so ensure that we'll never see 0 after
43        // initialization has run
44        MIN.store(amt + 1, Ordering::Relaxed);
45        amt
46    });
47
48    let id = ThreadId::new();
49    let thread = Thread::new(id, name);
50
51    let hooks = if no_hooks {
52        spawnhook::ChildSpawnHooks::default()
53    } else {
54        spawnhook::run_spawn_hooks(&thread)
55    };
56
57    let my_packet: Arc<Packet<'scope, T>> =
58        Arc::new(Packet { scope: scope_data, result: UnsafeCell::new(None), _marker: PhantomData });
59    let their_packet = my_packet.clone();
60
61    // Pass `f` in `MaybeUninit` because actually that closure might *run longer than the lifetime of `F`*.
62    // See <https://github.com/rust-lang/rust/issues/101983> for more details.
63    // To prevent leaks we use a wrapper that drops its contents.
64    #[repr(transparent)]
65    struct MaybeDangling<T>(MaybeUninit<T>);
66    impl<T> MaybeDangling<T> {
67        fn new(x: T) -> Self {
68            MaybeDangling(MaybeUninit::new(x))
69        }
70        fn into_inner(self) -> T {
71            // Make sure we don't drop.
72            let this = ManuallyDrop::new(self);
73            // SAFETY: we are always initialized.
74            unsafe { this.0.assume_init_read() }
75        }
76    }
77    impl<T> Drop for MaybeDangling<T> {
78        fn drop(&mut self) {
79            // SAFETY: we are always initialized.
80            unsafe { self.0.assume_init_drop() };
81        }
82    }
83
84    let f = MaybeDangling::new(f);
85
86    // The entrypoint of the Rust thread, after platform-specific thread
87    // initialization is done.
88    let rust_start = move || {
89        let f = f.into_inner();
90        let try_result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
91            crate::sys::backtrace::__rust_begin_short_backtrace(|| hooks.run());
92            crate::sys::backtrace::__rust_begin_short_backtrace(f)
93        }));
94        // SAFETY: `their_packet` as been built just above and moved by the
95        // closure (it is an Arc<...>) and `my_packet` will be stored in the
96        // same `JoinInner` as this closure meaning the mutation will be
97        // safe (not modify it and affect a value far away).
98        unsafe { *their_packet.result.get() = Some(try_result) };
99        // Here `their_packet` gets dropped, and if this is the last `Arc` for that packet that
100        // will call `decrement_num_running_threads` and therefore signal that this thread is
101        // done.
102        drop(their_packet);
103        // Here, the lifetime `'scope` can end. `main` keeps running for a bit
104        // after that before returning itself.
105    };
106
107    if let Some(scope_data) = &my_packet.scope {
108        scope_data.increment_num_running_threads();
109    }
110
111    // SAFETY: dynamic size and alignment of the Box remain the same. See below for why the
112    // lifetime change is justified.
113    let rust_start = unsafe {
114        let ptr = Box::into_raw(Box::new(rust_start));
115        let ptr = crate::mem::transmute::<
116            *mut (dyn FnOnce() + Send + '_),
117            *mut (dyn FnOnce() + Send + 'static),
118        >(ptr);
119        Box::from_raw(ptr)
120    };
121
122    let init = Box::new(ThreadInit { handle: thread.clone(), rust_start });
123
124    Ok(JoinInner {
125        // SAFETY:
126        //
127        // `imp::Thread::new` takes a closure with a `'static` lifetime, since it's passed
128        // through FFI or otherwise used with low-level threading primitives that have no
129        // notion of or way to enforce lifetimes.
130        //
131        // As mentioned in the `Safety` section of this function's documentation, the caller of
132        // this function needs to guarantee that the passed-in lifetime is sufficiently long
133        // for the lifetime of the thread.
134        //
135        // Similarly, the `sys` implementation must guarantee that no references to the closure
136        // exist after the thread has terminated, which is signaled by `Thread::join`
137        // returning.
138        native: unsafe { imp::Thread::new(stack_size, init)? },
139        thread,
140        packet: my_packet,
141    })
142}
143
144/// The data passed to the spawned thread for thread initialization. Any thread
145/// implementation should start a new thread by calling .init() on this before
146/// doing anything else to ensure the current thread is properly initialized and
147/// the global allocator works.
148pub(crate) struct ThreadInit {
149    pub handle: Thread,
150    pub rust_start: Box<dyn FnOnce() + Send>,
151}
152
153impl ThreadInit {
154    /// Initialize the 'current thread' mechanism on this thread, returning the
155    /// Rust entry point.
156    pub fn init(self: Box<Self>) -> Box<dyn FnOnce() + Send> {
157        // Set the current thread before any (de)allocations on the global allocator occur,
158        // so that it may call std::thread::current() in its implementation. This is also
159        // why we take Box<Self>, to ensure the Box is not destroyed until after this point.
160        // Cloning the handle does not invoke the global allocator, it is an Arc.
161        if let Err(_thread) = set_current(self.handle.clone()) {
162            // The current thread should not have set yet. Use an abort to save binary size (see #123356).
163            rtabort!("current thread handle already set during thread spawn");
164        }
165
166        if let Some(name) = self.handle.cname() {
167            imp::set_name(name);
168        }
169
170        self.rust_start
171    }
172}
173
174// This packet is used to communicate the return value between the spawned
175// thread and the rest of the program. It is shared through an `Arc` and
176// there's no need for a mutex here because synchronization happens with `join()`
177// (the caller will never read this packet until the thread has exited).
178//
179// An Arc to the packet is stored into a `JoinInner` which in turns is placed
180// in `JoinHandle`.
181struct Packet<'scope, T> {
182    scope: Option<Arc<ScopeData>>,
183    result: UnsafeCell<Option<Result<T>>>,
184    _marker: PhantomData<Option<&'scope ScopeData>>,
185}
186
187// Due to the usage of `UnsafeCell` we need to manually implement Sync.
188// The type `T` should already always be Send (otherwise the thread could not
189// have been created) and the Packet is Sync because all access to the
190// `UnsafeCell` synchronized (by the `join()` boundary), and `ScopeData` is Sync.
191unsafe impl<'scope, T: Send> Sync for Packet<'scope, T> {}
192
193impl<'scope, T> Drop for Packet<'scope, T> {
194    fn drop(&mut self) {
195        // If this packet was for a thread that ran in a scope, the thread
196        // panicked, and nobody consumed the panic payload, we make sure
197        // the scope function will panic.
198        let unhandled_panic = matches!(self.result.get_mut(), Some(Err(_)));
199        // Drop the result without causing unwinding.
200        // This is only relevant for threads that aren't join()ed, as
201        // join() will take the `result` and set it to None, such that
202        // there is nothing left to drop here.
203        // If this panics, we should handle that, because we're outside the
204        // outermost `catch_unwind` of our thread.
205        // We just abort in that case, since there's nothing else we can do.
206        // (And even if we tried to handle it somehow, we'd also need to handle
207        // the case where the panic payload we get out of it also panics on
208        // drop, and so on. See issue #86027.)
209        if let Err(_) = panic::catch_unwind(panic::AssertUnwindSafe(|| {
210            *self.result.get_mut() = None;
211        })) {
212            rtabort!("thread result panicked on drop");
213        }
214        // Book-keeping so the scope knows when it's done.
215        if let Some(scope) = &self.scope {
216            // Now that there will be no more user code running on this thread
217            // that can use 'scope, mark the thread as 'finished'.
218            // It's important we only do this after the `result` has been dropped,
219            // since dropping it might still use things it borrowed from 'scope.
220            scope.decrement_num_running_threads(unhandled_panic);
221        }
222    }
223}
224
225/// Inner representation for JoinHandle
226pub(super) struct JoinInner<'scope, T> {
227    native: imp::Thread,
228    thread: Thread,
229    packet: Arc<Packet<'scope, T>>,
230}
231
232impl<'scope, T> JoinInner<'scope, T> {
233    pub(super) fn is_finished(&self) -> bool {
234        Arc::strong_count(&self.packet) == 1
235    }
236
237    pub(super) fn thread(&self) -> &Thread {
238        &self.thread
239    }
240
241    pub(super) fn join(mut self) -> Result<T> {
242        self.native.join();
243        Arc::get_mut(&mut self.packet)
244            // FIXME(fuzzypixelz): returning an error instead of panicking here
245            // would require updating the documentation of
246            // `std::thread::Result`; currently we can return `Err` if and only
247            // if the thread had panicked.
248            .expect("threads should not terminate unexpectedly")
249            .result
250            .get_mut()
251            .take()
252            .unwrap()
253    }
254}
255
256impl<T> AsInner<imp::Thread> for JoinInner<'static, T> {
257    fn as_inner(&self) -> &imp::Thread {
258        &self.native
259    }
260}
261
262impl<T> IntoInner<imp::Thread> for JoinInner<'static, T> {
263    fn into_inner(self) -> imp::Thread {
264        self.native
265    }
266}