Skip to content

Commit

Permalink
core: registry: Factor out "wait till out of work" part of the main l…
Browse files Browse the repository at this point in the history
…oop.

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.
  • Loading branch information
emilio authored and cuviper committed Sep 20, 2023
1 parent 75524e2 commit 5236c4f
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions rayon-core/src/registry.rs
Expand Up @@ -905,14 +905,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);
wait_until_out_of_work(worker_thread);

// Normal termination, do not abort.
mem::forget(abort_guard);
Expand All @@ -924,6 +917,20 @@ unsafe fn main_loop(thread: ThreadBuilder) {
}
}

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

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

// 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);
}

/// If already in a worker-thread, just execute `op`. Otherwise,
/// execute `op` in the default thread-pool. Either way, block until
/// `op` completes and return its return value. If `op` panics, that
Expand Down

0 comments on commit 5236c4f

Please sign in to comment.