Skip to content

Commit

Permalink
Merge #1087
Browse files Browse the repository at this point in the history
1087: core: registry: Factor out "wait till out of work" part of the main loop. r=cuviper a=emilio

This was originally done for #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, perhaps.

Co-authored-by: Emilio Cobos Álvarez <emilio@crisal.io>
  • Loading branch information
bors[bot] and emilio committed Sep 20, 2023
2 parents 75524e2 + ea0c06d commit 082f215
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions rayon-core/src/registry.rs
Expand Up @@ -809,6 +809,20 @@ impl WorkerThread {
mem::forget(abort_guard); // successful execution, do not abort
}

unsafe fn wait_until_out_of_work(&self) {
debug_assert_eq!(self as *const _, WorkerThread::current());
let registry = &*self.registry;
let index = self.index;

self.wait_until(&registry.thread_infos[index].terminate);

// Should not be any work left in our queue.
debug_assert!(self.take_local_job().is_none());

// Let registry know we are done
Latch::set(&registry.thread_infos[index].stopped);
}

fn find_work(&self) -> Option<JobRef> {
// Try to find some work to do. We give preference first
// to things in our local deque, then in other workers
Expand Down Expand Up @@ -905,14 +919,7 @@ unsafe fn main_loop(thread: ThreadBuilder) {
registry.catch_unwind(|| handler(index));
}

let my_terminate_latch = &registry.thread_infos[index].terminate;
worker_thread.wait_until(my_terminate_latch);

// Should not be any work left in our queue.
debug_assert!(worker_thread.take_local_job().is_none());

// let registry know we are done
Latch::set(&registry.thread_infos[index].stopped);
worker_thread.wait_until_out_of_work();

// Normal termination, do not abort.
mem::forget(abort_guard);
Expand Down

0 comments on commit 082f215

Please sign in to comment.