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 committed Sep 4, 2023
1 parent 2a342a6 commit 81234d7
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions rayon-core/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,18 +947,15 @@ unsafe fn main_loop(thread: ThreadBuilder) {
registry.catch_unwind(|| handler(index));
}

let my_terminate_latch = &registry.thread_infos[index].terminate;
worker_thread.log(|| ThreadStart {
worker: index,
terminate_addr: my_terminate_latch.as_core_latch().addr(),
terminate_addr: registry.thread_infos[index]
.terminate
.as_core_latch()
.addr(),
});
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 @@ -972,6 +969,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 81234d7

Please sign in to comment.