diff --git a/library/std/src/thread/mod.rs b/library/std/src/thread/mod.rs index 22215873933d6..45a26bcc92fb2 100644 --- a/library/std/src/thread/mod.rs +++ b/library/std/src/thread/mod.rs @@ -164,7 +164,6 @@ use crate::env; use crate::ffi::{CStr, CString}; use crate::fmt; use crate::io; -use crate::marker::PhantomData; use crate::mem::{self, forget}; use crate::num::NonZero; use crate::panic; @@ -459,7 +458,7 @@ impl Builder { unsafe fn spawn_unchecked_<'a, 'scope, F, T>( self, f: F, - scope_data: Option>, + scope_data: Option<&'scope scoped::ScopeData>, ) -> io::Result> where F: FnOnce() -> T, @@ -494,11 +493,8 @@ impl Builder { }); let their_thread = my_thread.clone(); - let my_packet: Arc> = Arc::new(Packet { - scope: scope_data, - result: UnsafeCell::new(None), - _marker: PhantomData, - }); + let my_packet: Arc> = + Arc::new(Packet { scope: scope_data, result: UnsafeCell::new(None) }); let their_packet = my_packet.clone(); let output_capture = crate::io::set_output_capture(None); @@ -1535,9 +1531,8 @@ pub type Result = crate::result::Result>; // An Arc to the packet is stored into a `JoinInner` which in turns is placed // in `JoinHandle`. struct Packet<'scope, T> { - scope: Option>, + scope: Option<&'scope scoped::ScopeData>, result: UnsafeCell>>, - _marker: PhantomData>, } // Due to the usage of `UnsafeCell` we need to manually implement Sync. diff --git a/library/std/src/thread/scoped.rs b/library/std/src/thread/scoped.rs index e2e22e5194f4a..49f7ed4af9c76 100644 --- a/library/std/src/thread/scoped.rs +++ b/library/std/src/thread/scoped.rs @@ -11,7 +11,7 @@ use crate::sync::Arc; /// See [`scope`] for details. #[stable(feature = "scoped_threads", since = "1.63.0")] pub struct Scope<'scope, 'env: 'scope> { - data: Arc, + data: ScopeData, /// Invariance over 'scope, to make sure 'scope cannot shrink, /// which is necessary for soundness. /// @@ -136,14 +136,12 @@ pub fn scope<'env, F, T>(f: F) -> T where F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>) -> T, { - // We put the `ScopeData` into an `Arc` so that other threads can finish their - // `decrement_num_running_threads` even after this function returns. let scope = Scope { - data: Arc::new(ScopeData { + data: ScopeData { num_running_threads: AtomicUsize::new(0), main_thread: current(), a_thread_panicked: AtomicBool::new(false), - }), + }, env: PhantomData, scope: PhantomData, }; @@ -258,7 +256,7 @@ impl Builder { F: FnOnce() -> T + Send + 'scope, T: Send + 'scope, { - Ok(ScopedJoinHandle(unsafe { self.spawn_unchecked_(f, Some(scope.data.clone())) }?)) + Ok(ScopedJoinHandle(unsafe { self.spawn_unchecked_(f, Some(&scope.data)) }?)) } }