From 0ceac216c9ea96b4b63cba77ae1fabfc8e0320a1 Mon Sep 17 00:00:00 2001 From: ywxt Date: Wed, 25 Jun 2025 19:38:40 +0800 Subject: Only work-steal in the main loop for rustc_thread_pool Co-authored-by: Zoxc --- compiler/rustc_thread_pool/src/join/mod.rs | 80 +++++++++------------------- compiler/rustc_thread_pool/src/join/tests.rs | 1 + 2 files changed, 25 insertions(+), 56 deletions(-) (limited to 'compiler/rustc_thread_pool/src/join') diff --git a/compiler/rustc_thread_pool/src/join/mod.rs b/compiler/rustc_thread_pool/src/join/mod.rs index f285362c19b..08c4c4e96ab 100644 --- a/compiler/rustc_thread_pool/src/join/mod.rs +++ b/compiler/rustc_thread_pool/src/join/mod.rs @@ -1,10 +1,8 @@ -use std::any::Any; +use std::sync::atomic::{AtomicBool, Ordering}; use crate::job::StackJob; use crate::latch::SpinLatch; -use crate::registry::{self, WorkerThread}; -use crate::tlv::{self, Tlv}; -use crate::{FnContext, unwind}; +use crate::{FnContext, registry, tlv, unwind}; #[cfg(test)] mod tests; @@ -134,68 +132,38 @@ where // Create virtual wrapper for task b; this all has to be // done here so that the stack frame can keep it all live // long enough. - let job_b = StackJob::new(tlv, call_b(oper_b), SpinLatch::new(worker_thread)); + let job_b_started = AtomicBool::new(false); + let job_b = StackJob::new( + tlv, + |migrated| { + job_b_started.store(true, Ordering::Relaxed); + call_b(oper_b)(migrated) + }, + SpinLatch::new(worker_thread), + ); let job_b_ref = job_b.as_job_ref(); let job_b_id = job_b_ref.id(); worker_thread.push(job_b_ref); // Execute task a; hopefully b gets stolen in the meantime. let status_a = unwind::halt_unwinding(call_a(oper_a, injected)); - let result_a = match status_a { - Ok(v) => v, - Err(err) => join_recover_from_panic(worker_thread, &job_b.latch, err, tlv), - }; - - // Now that task A has finished, try to pop job B from the - // local stack. It may already have been popped by job A; it - // may also have been stolen. There may also be some tasks - // pushed on top of it in the stack, and we will have to pop - // those off to get to it. - while !job_b.latch.probe() { - if let Some(job) = worker_thread.take_local_job() { - if job_b_id == job.id() { - // Found it! Let's run it. - // - // Note that this could panic, but it's ok if we unwind here. - - // Restore the TLV since we might have run some jobs overwriting it when waiting for job b. - tlv::set(tlv); - - let result_b = job_b.run_inline(injected); - return (result_a, result_b); - } else { - worker_thread.execute(job); - } - } else { - // Local deque is empty. Time to steal from other - // threads. - worker_thread.wait_until(&job_b.latch); - debug_assert!(job_b.latch.probe()); - break; - } - } + worker_thread.wait_for_jobs::<_, false>( + &job_b.latch, + || job_b_started.load(Ordering::Relaxed), + |job| job.id() == job_b_id, + |job| { + debug_assert_eq!(job.id(), job_b_id); + job_b.run_inline(injected); + }, + ); // Restore the TLV since we might have run some jobs overwriting it when waiting for job b. tlv::set(tlv); + let result_a = match status_a { + Ok(v) => v, + Err(err) => unwind::resume_unwinding(err), + }; (result_a, job_b.into_result()) }) } - -/// If job A panics, we still cannot return until we are sure that job -/// B is complete. This is because it may contain references into the -/// enclosing stack frame(s). -#[cold] // cold path -unsafe fn join_recover_from_panic( - worker_thread: &WorkerThread, - job_b_latch: &SpinLatch<'_>, - err: Box, - tlv: Tlv, -) -> ! { - unsafe { worker_thread.wait_until(job_b_latch) }; - - // Restore the TLV since we might have run some jobs overwriting it when waiting for job b. - tlv::set(tlv); - - unwind::resume_unwinding(err) -} diff --git a/compiler/rustc_thread_pool/src/join/tests.rs b/compiler/rustc_thread_pool/src/join/tests.rs index 9df99072c3a..ec196632f75 100644 --- a/compiler/rustc_thread_pool/src/join/tests.rs +++ b/compiler/rustc_thread_pool/src/join/tests.rs @@ -97,6 +97,7 @@ fn join_context_both() { } #[test] +#[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] fn join_context_neither() { // If we're already in a 1-thread pool, neither job should be stolen. -- cgit 1.4.1-3-g733a5 From 46e18d1fe048a80613afacb05472d3eb44cec535 Mon Sep 17 00:00:00 2001 From: ywxt Date: Thu, 26 Jun 2025 14:49:20 +0800 Subject: Add FIXMEs for those ignored tests. --- compiler/rustc_thread_pool/src/broadcast/tests.rs | 2 ++ compiler/rustc_thread_pool/src/join/tests.rs | 1 + compiler/rustc_thread_pool/src/scope/tests.rs | 11 +++++++++++ compiler/rustc_thread_pool/src/spawn/tests.rs | 6 ++++++ compiler/rustc_thread_pool/src/thread_pool/tests.rs | 6 ++++++ compiler/rustc_thread_pool/tests/stack_overflow_crash.rs | 1 + 6 files changed, 27 insertions(+) (limited to 'compiler/rustc_thread_pool/src/join') diff --git a/compiler/rustc_thread_pool/src/broadcast/tests.rs b/compiler/rustc_thread_pool/src/broadcast/tests.rs index 201cb932192..2fe1319726c 100644 --- a/compiler/rustc_thread_pool/src/broadcast/tests.rs +++ b/compiler/rustc_thread_pool/src/broadcast/tests.rs @@ -64,6 +64,7 @@ fn spawn_broadcast_self() { assert!(v.into_iter().eq(0..7)); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -99,6 +100,7 @@ fn spawn_broadcast_mutual() { assert_eq!(rx.into_iter().count(), 3 * 7); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] diff --git a/compiler/rustc_thread_pool/src/join/tests.rs b/compiler/rustc_thread_pool/src/join/tests.rs index ec196632f75..71a971435bc 100644 --- a/compiler/rustc_thread_pool/src/join/tests.rs +++ b/compiler/rustc_thread_pool/src/join/tests.rs @@ -96,6 +96,7 @@ fn join_context_both() { assert!(b_migrated); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] diff --git a/compiler/rustc_thread_pool/src/scope/tests.rs b/compiler/rustc_thread_pool/src/scope/tests.rs index 049548f4a18..9b9ac98d066 100644 --- a/compiler/rustc_thread_pool/src/scope/tests.rs +++ b/compiler/rustc_thread_pool/src/scope/tests.rs @@ -289,6 +289,7 @@ macro_rules! test_order { }}; } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -299,6 +300,7 @@ fn lifo_order() { assert_eq!(vec, expected); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -335,6 +337,7 @@ macro_rules! test_nested_order { }}; } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -345,6 +348,7 @@ fn nested_lifo_order() { assert_eq!(vec, expected); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -355,6 +359,7 @@ fn nested_fifo_order() { assert_eq!(vec, expected); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -365,6 +370,7 @@ fn nested_lifo_fifo_order() { assert_eq!(vec, expected); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -407,6 +413,7 @@ macro_rules! test_mixed_order { }}; } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -418,6 +425,7 @@ fn mixed_lifo_order() { assert_eq!(vec, expected); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -427,6 +435,7 @@ fn mixed_fifo_order() { assert_eq!(vec, expected); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -438,6 +447,7 @@ fn mixed_lifo_fifo_order() { assert_eq!(vec, expected); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -553,6 +563,7 @@ fn scope_fifo_spawn_broadcast() { assert_eq!(sum.into_inner(), n * (n - 1) / 2); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] fn scope_spawn_broadcast_nested() { diff --git a/compiler/rustc_thread_pool/src/spawn/tests.rs b/compiler/rustc_thread_pool/src/spawn/tests.rs index a4989759cf9..119cfc7ca5e 100644 --- a/compiler/rustc_thread_pool/src/spawn/tests.rs +++ b/compiler/rustc_thread_pool/src/spawn/tests.rs @@ -166,6 +166,7 @@ macro_rules! test_order { }}; } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -176,6 +177,7 @@ fn lifo_order() { assert_eq!(vec, expected); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -186,6 +188,7 @@ fn fifo_order() { assert_eq!(vec, expected); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -196,6 +199,7 @@ fn lifo_fifo_order() { assert_eq!(vec, expected); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -233,6 +237,7 @@ macro_rules! test_mixed_order { }}; } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -242,6 +247,7 @@ fn mixed_lifo_fifo_order() { assert_eq!(vec, expected); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] diff --git a/compiler/rustc_thread_pool/src/thread_pool/tests.rs b/compiler/rustc_thread_pool/src/thread_pool/tests.rs index 9feaed7efd0..f2baab4c859 100644 --- a/compiler/rustc_thread_pool/src/thread_pool/tests.rs +++ b/compiler/rustc_thread_pool/src/thread_pool/tests.rs @@ -151,6 +151,7 @@ fn self_install() { assert!(pool.install(|| pool.install(|| true))); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -172,6 +173,7 @@ fn mutual_install() { assert!(ok); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -228,6 +230,7 @@ macro_rules! test_scope_order { }}; } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -237,6 +240,7 @@ fn scope_lifo_order() { assert_eq!(vec, expected); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -279,6 +283,7 @@ fn spawn_fifo_order() { assert_eq!(vec, expected); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] @@ -316,6 +321,7 @@ fn nested_scopes() { assert_eq!(counter.into_inner(), pools.len()); } +// FIXME: We should fix or remove this ignored test. #[test] #[ignore] #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] diff --git a/compiler/rustc_thread_pool/tests/stack_overflow_crash.rs b/compiler/rustc_thread_pool/tests/stack_overflow_crash.rs index 2b89ea4be19..d854751542f 100644 --- a/compiler/rustc_thread_pool/tests/stack_overflow_crash.rs +++ b/compiler/rustc_thread_pool/tests/stack_overflow_crash.rs @@ -35,6 +35,7 @@ fn overflow_code() -> Option { ExitStatus::from_raw(0xc00000fd /*STATUS_STACK_OVERFLOW*/).code() } +// FIXME: We should fix or remove this test on Windows. #[test] #[cfg_attr(not(any(unix)), ignore)] fn stack_overflow_crash() { -- cgit 1.4.1-3-g733a5