Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from AtomicU64 to Mutex. #12981

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/cargo-util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-util"
version = "0.2.8"
version = "0.2.9"
rust-version.workspace = true
edition.workspace = true
license.workspace = true
Expand Down
9 changes: 5 additions & 4 deletions crates/cargo-util/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use anyhow::{Context, Result};
use ignore::overrides::OverrideBuilder;
use ignore::{WalkBuilder, WalkState};
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};

/// Determines the disk usage of all files in the given directory.
Expand Down Expand Up @@ -40,7 +39,7 @@ fn du_inner(path: &Path, patterns: &[&str]) -> Result<u64> {
.git_ignore(false)
.git_exclude(false);
let walker = builder.build_parallel();
let total = Arc::new(AtomicU64::new(0));
let total = Arc::new(Mutex::new(0u64));
// A slot used to indicate there was an error while walking.
//
// It is possible that more than one error happens (such as in different
Expand All @@ -52,7 +51,8 @@ fn du_inner(path: &Path, patterns: &[&str]) -> Result<u64> {
Ok(entry) => match entry.metadata() {
Ok(meta) => {
if meta.is_file() {
total.fetch_add(meta.len(), Ordering::SeqCst);
let mut lock = total.lock().unwrap();
*lock += meta.len();
}
}
Err(e) => {
Expand All @@ -73,5 +73,6 @@ fn du_inner(path: &Path, patterns: &[&str]) -> Result<u64> {
return Err(e);
}

Ok(total.load(Ordering::SeqCst))
let total = *total.lock().unwrap();
Ok(total)
}