From a2141893629ce73a1c8ff6c464021ca27fd16c12 Mon Sep 17 00:00:00 2001 From: rosdf Date: Wed, 12 Feb 2025 11:25:07 +0400 Subject: [PATCH 1/2] Add extensions for ScopedJoinHandle like for JoinHandle --- library/std/src/os/unix/thread.rs | 13 ++++++++++++- library/std/src/os/windows/io/handle.rs | 16 ++++++++++++++++ library/std/src/os/windows/thread.rs | 16 ++++++++++++++++ library/std/src/thread/scoped.rs | 15 ++++++++++++++- 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/library/std/src/os/unix/thread.rs b/library/std/src/os/unix/thread.rs index 03dcc3a4f9ba0..a43f8c51cdeaa 100644 --- a/library/std/src/os/unix/thread.rs +++ b/library/std/src/os/unix/thread.rs @@ -7,7 +7,7 @@ #[allow(deprecated)] use crate::os::unix::raw::pthread_t; use crate::sys_common::{AsInner, IntoInner}; -use crate::thread::JoinHandle; +use crate::thread::{JoinHandle, ScopedJoinHandle}; #[stable(feature = "thread_extensions", since = "1.9.0")] #[allow(deprecated)] @@ -39,3 +39,14 @@ impl JoinHandleExt for JoinHandle { self.into_inner().into_id() as RawPthread } } + +#[stable(feature = "scoped_thread_extensions", since = "CURRENT_RUSTC_VERSION")] +impl JoinHandleExt for ScopedJoinHandle<'_, T> { + fn as_pthread_t(&self) -> RawPthread { + self.as_inner().id() as RawPthread + } + + fn into_pthread_t(self) -> RawPthread { + self.into_inner().into_id() as RawPthread + } +} diff --git a/library/std/src/os/windows/io/handle.rs b/library/std/src/os/windows/io/handle.rs index 76f5f549dd244..85cf678fbd891 100644 --- a/library/std/src/os/windows/io/handle.rs +++ b/library/std/src/os/windows/io/handle.rs @@ -660,3 +660,19 @@ impl From> for OwnedHandle { join_handle.into_inner().into_handle().into_inner() } } + +#[stable(feature = "scoped_thread_extensions", since = "CURRENT_RUSTC_VERSION")] +impl AsHandle for crate::thread::ScopedJoinHandle<'_, T> { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[stable(feature = "scoped_thread_extensions", since = "CURRENT_RUSTC_VERSION")] +impl From> for OwnedHandle { + #[inline] + fn from(join_handle: crate::thread::ScopedJoinHandle<'_, T>) -> OwnedHandle { + join_handle.into_inner().into_handle().into_inner() + } +} diff --git a/library/std/src/os/windows/thread.rs b/library/std/src/os/windows/thread.rs index d81d6d0ac28a9..e3c680937387b 100644 --- a/library/std/src/os/windows/thread.rs +++ b/library/std/src/os/windows/thread.rs @@ -23,3 +23,19 @@ impl IntoRawHandle for thread::JoinHandle { self.into_inner().into_handle().into_raw_handle() as *mut _ } } + +#[stable(feature = "scoped_thread_extensions", since = "CURRENT_RUSTC_VERSION")] +impl AsRawHandle for thread::ScopedJoinHandle<'_, T> { + #[inline] + fn as_raw_handle(&self) -> RawHandle { + self.as_inner().handle().as_raw_handle() as *mut _ + } +} + +#[stable(feature = "scoped_thread_extensions", since = "CURRENT_RUSTC_VERSION")] +impl IntoRawHandle for thread::ScopedJoinHandle<'_, T> { + #[inline] + fn into_raw_handle(self) -> RawHandle { + self.into_inner().into_handle().into_raw_handle() as *mut _ + } +} diff --git a/library/std/src/thread/scoped.rs b/library/std/src/thread/scoped.rs index 0033fc3a73283..393ebf8ed79aa 100644 --- a/library/std/src/thread/scoped.rs +++ b/library/std/src/thread/scoped.rs @@ -1,9 +1,10 @@ -use super::{Builder, JoinInner, Result, Thread, current_or_unnamed}; +use super::{Builder, JoinInner, Result, Thread, current_or_unnamed, imp}; use crate::marker::PhantomData; use crate::panic::{AssertUnwindSafe, catch_unwind, resume_unwind}; use crate::sync::Arc; use crate::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use crate::{fmt, io}; +use crate::sys_common::{AsInner, IntoInner}; /// A scope to spawn scoped threads in. /// @@ -347,3 +348,15 @@ impl<'scope, T> fmt::Debug for ScopedJoinHandle<'scope, T> { f.debug_struct("ScopedJoinHandle").finish_non_exhaustive() } } + +impl AsInner for ScopedJoinHandle<'_, T> { + fn as_inner(&self) -> &imp::Thread { + &self.0.native + } +} + +impl IntoInner for ScopedJoinHandle<'_, T> { + fn into_inner(self) -> imp::Thread { + self.0.native + } +} From d493ac8dd22e6c53fa612a38915714e1d49c9dd8 Mon Sep 17 00:00:00 2001 From: rosdf Date: Wed, 12 Feb 2025 12:18:54 +0400 Subject: [PATCH 2/2] fix import order --- library/std/src/thread/scoped.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/thread/scoped.rs b/library/std/src/thread/scoped.rs index 393ebf8ed79aa..fcd92f52eb082 100644 --- a/library/std/src/thread/scoped.rs +++ b/library/std/src/thread/scoped.rs @@ -3,8 +3,8 @@ use crate::marker::PhantomData; use crate::panic::{AssertUnwindSafe, catch_unwind, resume_unwind}; use crate::sync::Arc; use crate::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; -use crate::{fmt, io}; use crate::sys_common::{AsInner, IntoInner}; +use crate::{fmt, io}; /// A scope to spawn scoped threads in. ///