From 411b5400d23a5f0cd91c007541cc777ec5e54d00 Mon Sep 17 00:00:00 2001 From: jakka Date: Thu, 17 Jul 2025 13:19:54 +0300 Subject: switched to atomic usize for threading --- src/files.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/files.rs b/src/files.rs index 8181d1b..1ffbe7a 100644 --- a/src/files.rs +++ b/src/files.rs @@ -7,11 +7,11 @@ use std::{ fmt::Display, path::{Path, PathBuf}, sync::{ - Arc, Mutex, RwLock, - atomic::{AtomicBool, Ordering}, + Arc, Mutex, + atomic::{AtomicBool, AtomicUsize, Ordering}, }, thread::{self, sleep}, - time::{self, UNIX_EPOCH}, + time::{Duration, UNIX_EPOCH}, }; use walkdir::WalkDir; @@ -141,13 +141,13 @@ pub fn reencode_files(conn: Connection, handler: Arc, threads: usize let mut files = conn.get_toencode_files()?.into_iter(); - let thread_counter = Arc::new(RwLock::new(0_usize)); + let thread_counter = Arc::new(AtomicUsize::new(0)); let lock = Arc::new(Mutex::new(conn)); while handler.load(Ordering::SeqCst) { - if *thread_counter.read().unwrap() >= threads { - sleep(time::Duration::from_millis(100)); + if thread_counter.load(Ordering::Relaxed) >= threads { + sleep(Duration::from_millis(100)); continue; } @@ -161,7 +161,7 @@ pub fn reencode_files(conn: Connection, handler: Arc, threads: usize #[cfg(not(test))] let newbar = bar.clone(); let newcounter = thread_counter.clone(); - *thread_counter.write().unwrap() += 1; + thread_counter.fetch_add(1, Ordering::Relaxed); let _ = thread::spawn(move || { match handle_encode(&file, newhandler) { @@ -182,14 +182,12 @@ pub fn reencode_files(conn: Connection, handler: Arc, threads: usize } Ok(true) => {} }; - *newcounter.write().unwrap() -= 1; + newcounter.fetch_sub(1, Ordering::Relaxed); }); } - loop { - if *thread_counter.read().unwrap() == 0 { - break; - } + while thread_counter.load(Ordering::Relaxed) != 0 { + sleep(Duration::from_millis(100)); } #[cfg(not(test))] -- cgit v1.3.1