Stabilize scoped threads.
This commit is contained in:
parent
14947924df
commit
ae0a533b0b
3 changed files with 18 additions and 16 deletions
|
@ -350,7 +350,7 @@ impl AtomicBool {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// #![feature(atomic_from_mut, inline_const, scoped_threads)]
|
/// #![feature(atomic_from_mut, inline_const)]
|
||||||
/// use std::sync::atomic::{AtomicBool, Ordering};
|
/// use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
///
|
///
|
||||||
/// let mut some_bools = [const { AtomicBool::new(false) }; 10];
|
/// let mut some_bools = [const { AtomicBool::new(false) }; 10];
|
||||||
|
@ -381,7 +381,7 @@ impl AtomicBool {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// #![feature(atomic_from_mut, scoped_threads)]
|
/// #![feature(atomic_from_mut)]
|
||||||
/// use std::sync::atomic::{AtomicBool, Ordering};
|
/// use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
///
|
///
|
||||||
/// let mut some_bools = [false; 10];
|
/// let mut some_bools = [false; 10];
|
||||||
|
@ -1015,7 +1015,7 @@ impl<T> AtomicPtr<T> {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// #![feature(atomic_from_mut, inline_const, scoped_threads)]
|
/// #![feature(atomic_from_mut, inline_const)]
|
||||||
/// use std::ptr::null_mut;
|
/// use std::ptr::null_mut;
|
||||||
/// use std::sync::atomic::{AtomicPtr, Ordering};
|
/// use std::sync::atomic::{AtomicPtr, Ordering};
|
||||||
///
|
///
|
||||||
|
@ -1052,7 +1052,7 @@ impl<T> AtomicPtr<T> {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// #![feature(atomic_from_mut, scoped_threads)]
|
/// #![feature(atomic_from_mut)]
|
||||||
/// use std::ptr::null_mut;
|
/// use std::ptr::null_mut;
|
||||||
/// use std::sync::atomic::{AtomicPtr, Ordering};
|
/// use std::sync::atomic::{AtomicPtr, Ordering};
|
||||||
///
|
///
|
||||||
|
@ -1607,7 +1607,7 @@ macro_rules! atomic_int {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// #![feature(atomic_from_mut, inline_const, scoped_threads)]
|
/// #![feature(atomic_from_mut, inline_const)]
|
||||||
#[doc = concat!($extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};")]
|
#[doc = concat!($extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};")]
|
||||||
///
|
///
|
||||||
#[doc = concat!("let mut some_ints = [const { ", stringify!($atomic_type), "::new(0) }; 10];")]
|
#[doc = concat!("let mut some_ints = [const { ", stringify!($atomic_type), "::new(0) }; 10];")]
|
||||||
|
@ -1640,7 +1640,7 @@ macro_rules! atomic_int {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// #![feature(atomic_from_mut, scoped_threads)]
|
/// #![feature(atomic_from_mut)]
|
||||||
#[doc = concat!($extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};")]
|
#[doc = concat!($extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};")]
|
||||||
///
|
///
|
||||||
/// let mut some_ints = [0; 10];
|
/// let mut some_ints = [0; 10];
|
||||||
|
|
|
@ -183,10 +183,10 @@ use crate::time::Duration;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod local;
|
mod local;
|
||||||
|
|
||||||
#[unstable(feature = "scoped_threads", issue = "93203")]
|
#[stable(feature = "scoped_threads", since = "1.63.0")]
|
||||||
mod scoped;
|
mod scoped;
|
||||||
|
|
||||||
#[unstable(feature = "scoped_threads", issue = "93203")]
|
#[stable(feature = "scoped_threads", since = "1.63.0")]
|
||||||
pub use scoped::{scope, Scope, ScopedJoinHandle};
|
pub use scoped::{scope, Scope, ScopedJoinHandle};
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
|
|
@ -9,6 +9,7 @@ use crate::sync::Arc;
|
||||||
/// A scope to spawn scoped threads in.
|
/// A scope to spawn scoped threads in.
|
||||||
///
|
///
|
||||||
/// See [`scope`] for details.
|
/// See [`scope`] for details.
|
||||||
|
#[stable(feature = "scoped_threads", since = "1.63.0")]
|
||||||
pub struct Scope<'scope, 'env: 'scope> {
|
pub struct Scope<'scope, 'env: 'scope> {
|
||||||
data: ScopeData,
|
data: ScopeData,
|
||||||
/// Invariance over 'scope, to make sure 'scope cannot shrink,
|
/// Invariance over 'scope, to make sure 'scope cannot shrink,
|
||||||
|
@ -17,8 +18,6 @@ pub struct Scope<'scope, 'env: 'scope> {
|
||||||
/// Without invariance, this would compile fine but be unsound:
|
/// Without invariance, this would compile fine but be unsound:
|
||||||
///
|
///
|
||||||
/// ```compile_fail,E0373
|
/// ```compile_fail,E0373
|
||||||
/// #![feature(scoped_threads)]
|
|
||||||
///
|
|
||||||
/// std::thread::scope(|s| {
|
/// std::thread::scope(|s| {
|
||||||
/// s.spawn(|| {
|
/// s.spawn(|| {
|
||||||
/// let a = String::from("abcd");
|
/// let a = String::from("abcd");
|
||||||
|
@ -33,6 +32,7 @@ pub struct Scope<'scope, 'env: 'scope> {
|
||||||
/// An owned permission to join on a scoped thread (block on its termination).
|
/// An owned permission to join on a scoped thread (block on its termination).
|
||||||
///
|
///
|
||||||
/// See [`Scope::spawn`] for details.
|
/// See [`Scope::spawn`] for details.
|
||||||
|
#[stable(feature = "scoped_threads", since = "1.63.0")]
|
||||||
pub struct ScopedJoinHandle<'scope, T>(JoinInner<'scope, T>);
|
pub struct ScopedJoinHandle<'scope, T>(JoinInner<'scope, T>);
|
||||||
|
|
||||||
pub(super) struct ScopeData {
|
pub(super) struct ScopeData {
|
||||||
|
@ -82,7 +82,6 @@ impl ScopeData {
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// #![feature(scoped_threads)]
|
|
||||||
/// use std::thread;
|
/// use std::thread;
|
||||||
///
|
///
|
||||||
/// let mut a = vec![1, 2, 3];
|
/// let mut a = vec![1, 2, 3];
|
||||||
|
@ -126,6 +125,7 @@ impl ScopeData {
|
||||||
///
|
///
|
||||||
/// The `'env: 'scope` bound is part of the definition of the `Scope` type.
|
/// The `'env: 'scope` bound is part of the definition of the `Scope` type.
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
|
#[stable(feature = "scoped_threads", since = "1.63.0")]
|
||||||
pub fn scope<'env, F, T>(f: F) -> T
|
pub fn scope<'env, F, T>(f: F) -> T
|
||||||
where
|
where
|
||||||
F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>) -> T,
|
F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>) -> T,
|
||||||
|
@ -183,6 +183,7 @@ impl<'scope, 'env> Scope<'scope, 'env> {
|
||||||
/// to recover from such errors.
|
/// to recover from such errors.
|
||||||
///
|
///
|
||||||
/// [`join`]: ScopedJoinHandle::join
|
/// [`join`]: ScopedJoinHandle::join
|
||||||
|
#[stable(feature = "scoped_threads", since = "1.63.0")]
|
||||||
pub fn spawn<F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T>
|
pub fn spawn<F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T>
|
||||||
where
|
where
|
||||||
F: FnOnce() -> T + Send + 'scope,
|
F: FnOnce() -> T + Send + 'scope,
|
||||||
|
@ -207,7 +208,6 @@ impl Builder {
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// #![feature(scoped_threads)]
|
|
||||||
/// use std::thread;
|
/// use std::thread;
|
||||||
///
|
///
|
||||||
/// let mut a = vec![1, 2, 3];
|
/// let mut a = vec![1, 2, 3];
|
||||||
|
@ -240,6 +240,7 @@ impl Builder {
|
||||||
/// a.push(4);
|
/// a.push(4);
|
||||||
/// assert_eq!(x, a.len());
|
/// assert_eq!(x, a.len());
|
||||||
/// ```
|
/// ```
|
||||||
|
#[stable(feature = "scoped_threads", since = "1.63.0")]
|
||||||
pub fn spawn_scoped<'scope, 'env, F, T>(
|
pub fn spawn_scoped<'scope, 'env, F, T>(
|
||||||
self,
|
self,
|
||||||
scope: &'scope Scope<'scope, 'env>,
|
scope: &'scope Scope<'scope, 'env>,
|
||||||
|
@ -259,8 +260,6 @@ impl<'scope, T> ScopedJoinHandle<'scope, T> {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// #![feature(scoped_threads)]
|
|
||||||
///
|
|
||||||
/// use std::thread;
|
/// use std::thread;
|
||||||
///
|
///
|
||||||
/// thread::scope(|s| {
|
/// thread::scope(|s| {
|
||||||
|
@ -271,6 +270,7 @@ impl<'scope, T> ScopedJoinHandle<'scope, T> {
|
||||||
/// });
|
/// });
|
||||||
/// ```
|
/// ```
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
#[stable(feature = "scoped_threads", since = "1.63.0")]
|
||||||
pub fn thread(&self) -> &Thread {
|
pub fn thread(&self) -> &Thread {
|
||||||
&self.0.thread
|
&self.0.thread
|
||||||
}
|
}
|
||||||
|
@ -292,8 +292,6 @@ impl<'scope, T> ScopedJoinHandle<'scope, T> {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// #![feature(scoped_threads)]
|
|
||||||
///
|
|
||||||
/// use std::thread;
|
/// use std::thread;
|
||||||
///
|
///
|
||||||
/// thread::scope(|s| {
|
/// thread::scope(|s| {
|
||||||
|
@ -303,6 +301,7 @@ impl<'scope, T> ScopedJoinHandle<'scope, T> {
|
||||||
/// assert!(t.join().is_err());
|
/// assert!(t.join().is_err());
|
||||||
/// });
|
/// });
|
||||||
/// ```
|
/// ```
|
||||||
|
#[stable(feature = "scoped_threads", since = "1.63.0")]
|
||||||
pub fn join(self) -> Result<T> {
|
pub fn join(self) -> Result<T> {
|
||||||
self.0.join()
|
self.0.join()
|
||||||
}
|
}
|
||||||
|
@ -316,11 +315,13 @@ impl<'scope, T> ScopedJoinHandle<'scope, T> {
|
||||||
///
|
///
|
||||||
/// This function does not block. To block while waiting on the thread to finish,
|
/// This function does not block. To block while waiting on the thread to finish,
|
||||||
/// use [`join`][Self::join].
|
/// use [`join`][Self::join].
|
||||||
|
#[stable(feature = "scoped_threads", since = "1.63.0")]
|
||||||
pub fn is_finished(&self) -> bool {
|
pub fn is_finished(&self) -> bool {
|
||||||
Arc::strong_count(&self.0.packet) == 1
|
Arc::strong_count(&self.0.packet) == 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "scoped_threads", since = "1.63.0")]
|
||||||
impl fmt::Debug for Scope<'_, '_> {
|
impl fmt::Debug for Scope<'_, '_> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
f.debug_struct("Scope")
|
f.debug_struct("Scope")
|
||||||
|
@ -331,6 +332,7 @@ impl fmt::Debug for Scope<'_, '_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "scoped_threads", since = "1.63.0")]
|
||||||
impl<'scope, T> fmt::Debug for ScopedJoinHandle<'scope, T> {
|
impl<'scope, T> fmt::Debug for ScopedJoinHandle<'scope, T> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
f.debug_struct("ScopedJoinHandle").finish_non_exhaustive()
|
f.debug_struct("ScopedJoinHandle").finish_non_exhaustive()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue