Skip to content

Commit 561b81f

Browse files
emiliocuviper
authored andcommitted
core: registry: Factor out "wait till out of work" part of the main loop.
This was originally done for rayon-rs#1063, in order to reuse this to allow cleaning up the TLS data allocated by use_current_thread. We ended up not using that, but this refactoring seems useful on its own. (cherry picked from commit ea0c06d)
1 parent 7e35a4d commit 561b81f

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

rayon-core/src/registry.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,21 @@ impl WorkerThread {
851851
mem::forget(abort_guard); // successful execution, do not abort
852852
}
853853

854+
unsafe fn wait_until_out_of_work(&self) {
855+
debug_assert_eq!(self as *const _, WorkerThread::current());
856+
let registry = &*self.registry;
857+
let index = self.index;
858+
859+
registry.acquire_thread();
860+
self.wait_until(&registry.thread_infos[index].terminate);
861+
862+
// Should not be any work left in our queue.
863+
debug_assert!(self.take_local_job().is_none());
864+
865+
// Let registry know we are done
866+
Latch::set(&registry.thread_infos[index].stopped);
867+
}
868+
854869
fn find_work(&self) -> Option<JobRef> {
855870
// Try to find some work to do. We give preference first
856871
// to things in our local deque, then in other workers
@@ -947,15 +962,7 @@ unsafe fn main_loop(thread: ThreadBuilder) {
947962
registry.catch_unwind(|| handler(index));
948963
}
949964

950-
let my_terminate_latch = &registry.thread_infos[index].terminate;
951-
registry.acquire_thread();
952-
worker_thread.wait_until(my_terminate_latch);
953-
954-
// Should not be any work left in our queue.
955-
debug_assert!(worker_thread.take_local_job().is_none());
956-
957-
// let registry know we are done
958-
Latch::set(&registry.thread_infos[index].stopped);
965+
worker_thread.wait_until_out_of_work();
959966

960967
// Normal termination, do not abort.
961968
mem::forget(abort_guard);

0 commit comments

Comments
 (0)