Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions compiler/rustc_thread_pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ impl ThreadPoolBuilder {
/// 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()`.
/// Once the pool is built, this waits for all its threads to exit before returning.
///
/// [`std::thread::scope`]: https://doc.rust-lang.org/std/thread/fn.scope.html
///
Expand Down Expand Up @@ -345,6 +346,7 @@ impl ThreadPoolBuilder {
F: FnOnce(&ThreadPool) -> R,
{
std::thread::scope(|scope| {
let mut handles = Vec::new();
let pool = self
.spawn_handler(|thread| {
let mut builder = std::thread::Builder::new();
Expand All @@ -354,15 +356,24 @@ impl ThreadPoolBuilder {
if let Some(size) = thread.stack_size() {
builder = builder.stack_size(size);
}
builder.spawn_scoped(scope, || wrapper(thread))?;
handles.push(builder.spawn_scoped(scope, || wrapper(thread))?);
Ok(())
})
.build()?;
let result = unwind::halt_unwinding(|| with_pool(&pool));
pool.wait_until_stopped();
match result {
Ok(result) => Ok(result),
Err(err) => unwind::resume_unwinding(err),
// `wait_until_stopped` and the scope's implicit join only wait for the
// closures to return, not for the threads to exit (TLS destructors etc.).
// A process exit in between can crash on windows-gnu.
let mut worker_panic = None;
for handle in handles {
if let Err(err) = handle.join() {
worker_panic.get_or_insert(err);
}
}
match (result, worker_panic) {
(Ok(result), None) => Ok(result),
(Err(err), _) | (Ok(_), Some(err)) => unwind::resume_unwinding(err),
}
})
}
Expand Down
34 changes: 34 additions & 0 deletions compiler/rustc_thread_pool/tests/scoped_threadpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,37 @@ fn build_scoped_tls_threadpool() {
});
});
}

#[test]
#[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)]
fn build_scoped_thread_exit() {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

const NUM_THREADS: usize = 4;
static EXITED: AtomicUsize = AtomicUsize::new(0);

struct OnExit;
impl Drop for OnExit {
fn drop(&mut self) {
// Make exiting slow enough that returning early is visible.
std::thread::sleep(Duration::from_millis(10));
EXITED.fetch_add(1, Ordering::SeqCst);
}
}
// TLS destructors run while the thread exits, after its closure has returned.
thread_local!(static ON_EXIT: OnExit = const { OnExit });

ThreadPoolBuilder::new()
.num_threads(NUM_THREADS)
.build_scoped(
|thread| {
ON_EXIT.with(|_| {});
thread.run()
},
|pool| pool.install(|| {}),
)
.expect("thread pool created");
// `build_scoped` waits for the threads to exit.
assert_eq!(EXITED.load(Ordering::SeqCst), NUM_THREADS);
}
Loading