@@ -33,9 +33,28 @@ use std::io::{self, BufReader};
33
33
use std:: path:: { Path , PathBuf } ;
34
34
use std:: process:: { Command , Output , ExitStatus , Stdio , Child } ;
35
35
use std:: str;
36
+ use std:: sync:: { Arc , Mutex , RwLock } ;
36
37
37
38
use extract_gdb_version;
38
39
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
+
39
58
/// The name of the environment variable that holds dynamic library locations.
40
59
pub fn dylib_env_var ( ) -> & ' static str {
41
60
if cfg ! ( windows) {
@@ -2343,16 +2362,23 @@ actual:\n\
2343
2362
coverage_file_path. push ( "rustfix_missing_coverage.txt" ) ;
2344
2363
debug ! ( "coverage_file_path: {}" , coverage_file_path. display( ) ) ;
2345
2364
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
+ {
2353
2379
panic ! ( "couldn't write to {}" , coverage_file_path. display( ) ) ;
2354
2380
}
2355
- }
2381
+ }
2356
2382
}
2357
2383
2358
2384
if self . props . run_rustfix {
0 commit comments