Skip to content

Commit 6b52ec9

Browse files
authored
Merge pull request #259 from smoelius/lock-coverage-files
Lock coverage files
2 parents d3282f8 + 148297b commit 6b52ec9

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/runtest.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,28 @@ use std::io::{self, BufReader};
3333
use std::path::{Path, PathBuf};
3434
use std::process::{Command, Output, ExitStatus, Stdio, Child};
3535
use std::str;
36+
use std::sync::{Arc, Mutex, RwLock};
3637

3738
use extract_gdb_version;
3839

40+
fn get_or_create_coverage_file(path: &Path, create: impl FnOnce() -> File) -> Arc<Mutex<File>> {
41+
lazy_static::lazy_static! {
42+
static ref COVERAGE_FILE_LOCKS: RwLock<HashMap<PathBuf, Arc<Mutex<File>>>> = RwLock::new(HashMap::new());
43+
}
44+
45+
{
46+
let locks = COVERAGE_FILE_LOCKS.read().unwrap();
47+
locks.get(path).map(Arc::clone)
48+
}
49+
.unwrap_or_else(|| {
50+
let mut locks = COVERAGE_FILE_LOCKS.write().unwrap();
51+
locks
52+
.entry(path.to_path_buf())
53+
.or_insert_with(|| Arc::new(Mutex::new(create())))
54+
.clone()
55+
})
56+
}
57+
3958
/// The name of the environment variable that holds dynamic library locations.
4059
pub fn dylib_env_var() -> &'static str {
4160
if cfg!(windows) {
@@ -2343,16 +2362,23 @@ actual:\n\
23432362
coverage_file_path.push("rustfix_missing_coverage.txt");
23442363
debug!("coverage_file_path: {}", coverage_file_path.display());
23452364

2346-
let mut file = OpenOptions::new()
2347-
.create(true)
2348-
.append(true)
2349-
.open(coverage_file_path.as_path())
2350-
.expect("could not create or open file");
2351-
2352-
if let Err(_) = writeln!(file, "{}", self.testpaths.file.display()) {
2365+
let file_ref = get_or_create_coverage_file(&coverage_file_path, || {
2366+
OpenOptions::new()
2367+
.create(true)
2368+
.write(true)
2369+
.truncate(true)
2370+
.open(coverage_file_path.as_path())
2371+
.expect("could not create or open file")
2372+
});
2373+
let mut file = file_ref.lock().unwrap();
2374+
2375+
if writeln!(file, "{}", self.testpaths.file.display())
2376+
.and_then(|_| file.sync_data())
2377+
.is_err()
2378+
{
23532379
panic!("couldn't write to {}", coverage_file_path.display());
23542380
}
2355-
}
2381+
}
23562382
}
23572383

23582384
if self.props.run_rustfix {

0 commit comments

Comments
 (0)