keyfork-qrcode: add multithreaded scanner #82

Manually merged
ryan merged 5 commits from ryansquared/test-threaded-scan into main 2025-05-10 20:21:18 +00:00
1 changed files with 6 additions and 8 deletions
Showing only changes of commit 2083eb216f - Show all commits

View File

@ -244,22 +244,21 @@ pub fn scan_camera(timeout: Duration, index: usize) -> Result<Option<String>, QR
} }
let mut queue = condvar let mut queue = condvar
.wait_while(queue, |queue| { .wait_while(queue, |queue| {
eprintln!("thread {i} received signal"); queue.images.is_empty() && !queue.shutdown
queue.images.is_empty() || queue.shutdown
}) })
.expect(bug::bug!(POISONED_MUTEX)); .expect(bug::bug!(POISONED_MUTEX));
if let Some(image) = queue.images.pop() { if let Some(image) = queue.images.pop() {
eprintln!("thread {i} received image");
// drop the queue here since this is what's going to take time // drop the queue here since this is what's going to take time
drop(queue); drop(queue);
if let Some(content) = scanner.scan_image(image) { if let Some(content) = scanner.scan_image(image) {
println!("scanned: {content}"); eprintln!("scanned: {content}");
tx.send(content).expect( if tx.send(content).is_err() {
bug::bug!("A scanned image could not be sent") break;
);
} }
} }
} }
}
eprintln!("shutting down thread {i}");
}); });
} }
@ -284,7 +283,6 @@ pub fn scan_camera(timeout: Duration, index: usize) -> Result<Option<String>, QR
.expect(bug::bug!(POISONED_MUTEX)) .expect(bug::bug!(POISONED_MUTEX))
.images .images
.push(image); .push(image);
eprintln!("notifying any thread");
arced.1.notify_one(); arced.1.notify_one();
} }