feat: Shutdown propogation and thread count arg

The Stop command will be handled specially by the Dispatcher to act as a
sort of broadcast into the thread pool. Instead of asking the caller to
feed in the appropriate number of stop commands, a single one will queue
up a stop for all threads. Important for the ergonomics of having a
variable number of threads (instead of the earlier magic constant).
Moreover, it's necessary if the dispatcher stops using the round-robin
style assignment.

The pool size is specifiable as an argument to Dispatcher::new().
This commit is contained in:
2023-06-25 14:29:35 -05:00
parent 9873c5596d
commit adaf277cba
2 changed files with 18 additions and 13 deletions

View File

@@ -66,21 +66,16 @@ fn main() {
};
thread::scope(|s| {
let (mut dispatcher, scanline_receiver) = thread_utils::Dispatcher::new(&small_rng);
let (mut dispatcher, scanline_receiver) = thread_utils::Dispatcher::new(&small_rng, 4);
s.spawn(move || {
for y in (0..image.1).rev() {
eprintln!("Submitting scanline: {}", y);
let job = RenderCommand::Line { line_num: y, context: context.clone() };
dispatcher.submit_job(job);
dispatcher.submit_job(job).unwrap();
}
//TODO: Dispatcher shutdown mechanism
// Just gonna take advantage of the round-robin dispatching to
// get a stop command to each thread
dispatcher.submit_job(RenderCommand::Stop);
dispatcher.submit_job(RenderCommand::Stop);
dispatcher.submit_job(RenderCommand::Stop);
dispatcher.submit_job(RenderCommand::Stop);
dispatcher.submit_job(RenderCommand::Stop).unwrap();
// ... also I happen to know there are 4 threads.
});