Skip to content

Commit

Permalink
Switch build_scoped to std::thread::scope (Rust 1.63)
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Aug 11, 2022
1 parent f79c44b commit def184b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
23 changes: 8 additions & 15 deletions rayon-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,12 @@ where
impl ThreadPoolBuilder {
/// Creates a scoped `ThreadPool` initialized using this configuration.
///
/// This is a convenience function for building a pool using [`crossbeam::scope`]
/// This is a convenience function for building a pool using [`std::thread::scope`]
/// to spawn threads in a [`spawn_handler`](#method.spawn_handler).
/// The threads in this pool will start by calling `wrapper`, which should
/// do initialization and continue by calling `ThreadBuilder::run()`.
///
/// [`crossbeam::scope`]: https://docs.rs/crossbeam/0.8/crossbeam/fn.scope.html
/// [`std::thread::scope`]: https://doc.rust-lang.org/std/thread/fn.scope.html
///
/// # Examples
///
Expand Down Expand Up @@ -305,28 +305,22 @@ impl ThreadPoolBuilder {
W: Fn(ThreadBuilder) + Sync, // expected to call `run()`
F: FnOnce(&ThreadPool) -> R,
{
let result = crossbeam_utils::thread::scope(|scope| {
let wrapper = &wrapper;
std::thread::scope(|scope| {
let pool = self
.spawn_handler(|thread| {
let mut builder = scope.builder();
let mut builder = std::thread::Builder::new();
if let Some(name) = thread.name() {
builder = builder.name(name.to_string());
}
if let Some(size) = thread.stack_size() {
builder = builder.stack_size(size);
}
builder.spawn(move |_| wrapper(thread))?;
builder.spawn_scoped(scope, || wrapper(thread))?;
Ok(())
})
.build()?;
Ok(with_pool(&pool))
});

match result {
Ok(result) => result,
Err(err) => unwind::resume_unwinding(err),
}
})
}
}

Expand All @@ -335,13 +329,11 @@ impl<S> ThreadPoolBuilder<S> {
///
/// Note that the threads will not exit until after the pool is dropped. It
/// is up to the caller to wait for thread termination if that is important
/// for any invariants. For instance, threads created in [`crossbeam::scope`]
/// for any invariants. For instance, threads created in [`std::thread::scope`]
/// will be joined before that scope returns, and this will block indefinitely
/// if the pool is leaked. Furthermore, the global thread pool doesn't terminate
/// until the entire process exits!
///
/// [`crossbeam::scope`]: https://docs.rs/crossbeam/0.8/crossbeam/fn.scope.html
///
/// # Examples
///
/// A minimal spawn handler just needs to call `run()` from an independent thread.
Expand Down Expand Up @@ -390,6 +382,7 @@ impl<S> ThreadPoolBuilder<S> {
/// or [`std::thread::scope`] introduced in Rust 1.63, which is encapsulated in
/// [`build_scoped`](#method.build_scoped).
///
/// [`crossbeam::scope`]: https://docs.rs/crossbeam/0.8/crossbeam/fn.scope.html
/// [`std::thread::scope`]: https://doc.rust-lang.org/std/thread/fn.scope.html
///
/// ```
Expand Down
2 changes: 1 addition & 1 deletion rayon-core/tests/scoped_threadpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn build_scoped_tls_threadpool() {
},
)
.expect("thread pool created");
// Internally, `crossbeam::scope` will wait for the threads to exit before returning.
// Internally, `std::thread::scope` will wait for the threads to exit before returning.
});
});
}

0 comments on commit def184b

Please sign in to comment.