]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unix/ext/thread.rs
759ef6236e80459f272ca07f71aa33fa1ba0527a
[rust.git] / library / std / src / sys / unix / ext / thread.rs
1 //! Unix-specific extensions to primitives in the `std::thread` module.
2
3 #![stable(feature = "thread_extensions", since = "1.9.0")]
4
5 #[allow(deprecated)]
6 use crate::os::unix::raw::pthread_t;
7 use crate::sys_common::{AsInner, IntoInner};
8 use crate::thread::JoinHandle;
9
10 #[stable(feature = "thread_extensions", since = "1.9.0")]
11 #[allow(deprecated)]
12 pub type RawPthread = pthread_t;
13
14 /// Unix-specific extensions to [`thread::JoinHandle`].
15 ///
16 /// [`thread::JoinHandle`]: ../../../../std/thread/struct.JoinHandle.html
17 #[stable(feature = "thread_extensions", since = "1.9.0")]
18 pub trait JoinHandleExt {
19     /// Extracts the raw pthread_t without taking ownership
20     #[stable(feature = "thread_extensions", since = "1.9.0")]
21     fn as_pthread_t(&self) -> RawPthread;
22
23     /// Consumes the thread, returning the raw pthread_t
24     ///
25     /// This function **transfers ownership** of the underlying pthread_t to
26     /// the caller. Callers are then the unique owners of the pthread_t and
27     /// must either detach or join the pthread_t once it's no longer needed.
28     #[stable(feature = "thread_extensions", since = "1.9.0")]
29     fn into_pthread_t(self) -> RawPthread;
30 }
31
32 #[stable(feature = "thread_extensions", since = "1.9.0")]
33 impl<T> JoinHandleExt for JoinHandle<T> {
34     fn as_pthread_t(&self) -> RawPthread {
35         self.as_inner().id() as RawPthread
36     }
37
38     fn into_pthread_t(self) -> RawPthread {
39         self.into_inner().into_id() as RawPthread
40     }
41 }