Skip to content

Commit

Permalink
Merge pull request #259 from smoelius/lock-coverage-files
Browse files Browse the repository at this point in the history
Lock coverage files
  • Loading branch information
Manishearth authored Jul 27, 2022
2 parents d3282f8 + 148297b commit 6b52ec9
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,28 @@ use std::io::{self, BufReader};
use std::path::{Path, PathBuf};
use std::process::{Command, Output, ExitStatus, Stdio, Child};
use std::str;
use std::sync::{Arc, Mutex, RwLock};

use extract_gdb_version;

fn get_or_create_coverage_file(path: &Path, create: impl FnOnce() -> File) -> Arc<Mutex<File>> {
lazy_static::lazy_static! {
static ref COVERAGE_FILE_LOCKS: RwLock<HashMap<PathBuf, Arc<Mutex<File>>>> = RwLock::new(HashMap::new());
}

{
let locks = COVERAGE_FILE_LOCKS.read().unwrap();
locks.get(path).map(Arc::clone)
}
.unwrap_or_else(|| {
let mut locks = COVERAGE_FILE_LOCKS.write().unwrap();
locks
.entry(path.to_path_buf())
.or_insert_with(|| Arc::new(Mutex::new(create())))
.clone()
})
}

/// The name of the environment variable that holds dynamic library locations.
pub fn dylib_env_var() -> &'static str {
if cfg!(windows) {
Expand Down Expand Up @@ -2343,16 +2362,23 @@ actual:\n\
coverage_file_path.push("rustfix_missing_coverage.txt");
debug!("coverage_file_path: {}", coverage_file_path.display());

let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(coverage_file_path.as_path())
.expect("could not create or open file");

if let Err(_) = writeln!(file, "{}", self.testpaths.file.display()) {
let file_ref = get_or_create_coverage_file(&coverage_file_path, || {
OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(coverage_file_path.as_path())
.expect("could not create or open file")
});
let mut file = file_ref.lock().unwrap();

if writeln!(file, "{}", self.testpaths.file.display())
.and_then(|_| file.sync_data())
.is_err()
{
panic!("couldn't write to {}", coverage_file_path.display());
}
}
}
}

if self.props.run_rustfix {
Expand Down

0 comments on commit 6b52ec9

Please sign in to comment.