std/os/unix/thread.rs
1//! Unix-specific extensions to primitives in the [`std::thread`] module.
2//!
3//! [`std::thread`]: crate::thread
4
5#![stable(feature = "thread_extensions", since = "1.9.0")]
6
7use crate::macros::rustdoc_skip_body;
8#[allow(deprecated)]
9use crate::os::unix::raw::pthread_t;
10use crate::sys::{AsInner, IntoInner};
11use crate::thread::JoinHandle;
12
13#[stable(feature = "thread_extensions", since = "1.9.0")]
14#[allow(deprecated)]
15pub type RawPthread = pthread_t;
16
17/// Unix-specific extensions to [`JoinHandle`].
18#[stable(feature = "thread_extensions", since = "1.9.0")]
19pub trait JoinHandleExt {
20 /// Extracts the raw pthread_t without taking ownership
21 #[stable(feature = "thread_extensions", since = "1.9.0")]
22 fn as_pthread_t(&self) -> RawPthread;
23
24 /// Consumes the thread, returning the raw pthread_t
25 ///
26 /// This function **transfers ownership** of the underlying pthread_t to
27 /// the caller. Callers are then the unique owners of the pthread_t and
28 /// must either detach or join the pthread_t once it's no longer needed.
29 #[stable(feature = "thread_extensions", since = "1.9.0")]
30 fn into_pthread_t(self) -> RawPthread;
31}
32
33#[stable(feature = "thread_extensions", since = "1.9.0")]
34impl<T> JoinHandleExt for JoinHandle<T> {
35 // This is an int2ptr cast on some platforms (e.g., *-musl) where RawPthread
36 // is an integer but libc::pthread_t is a pointer. Exposed provenance is the
37 // safe choice here, but `as` also works when it's int2int or ptr2ptr.
38 #[allow(implicit_provenance_casts)]
39 #[rustdoc_skip_body]
40 fn as_pthread_t(&self) -> RawPthread {
41 self.as_inner().id() as RawPthread
42 }
43
44 #[allow(implicit_provenance_casts)] // see above for why
45 #[rustdoc_skip_body]
46 fn into_pthread_t(self) -> RawPthread {
47 self.into_inner().into_id() as RawPthread
48 }
49}