diff options
| author | jakka <jakka@jakka.su> | 2025-07-17 13:19:54 +0300 |
|---|---|---|
| committer | jakka <jakka@jakka.su> | 2025-07-17 13:19:54 +0300 |
| commit | 411b5400d23a5f0cd91c007541cc777ec5e54d00 (patch) | |
| tree | 3a1a83e9a7e470ecc918f3c47b2e5efb9b5ae9b1 /src | |
| parent | 3763477554f210442f2aa7bdace2cf1f10f13b3a (diff) | |
switched to atomic usize for threadingnorayon
Diffstat (limited to 'src')
| -rw-r--r-- | src/files.rs | 22 |
1 files changed, 10 insertions, 12 deletions
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<AtomicBool>, 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<AtomicBool>, 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<AtomicBool>, 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))] |
