Skip to content

Commit 15ecfc4

Browse files
committed
0.7.0
1 parent 48a43ba commit 15ecfc4

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/engine/cpp.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fs;
22
use std::path::{Path, PathBuf};
33
use std::process::{Command, Stdio};
4+
use std::sync::{Mutex, OnceLock};
45
use std::time::{Duration, Instant};
56

67
use anyhow::{Context, Result};
@@ -1108,6 +1109,10 @@ fn run_cpp_binary(binary: &Path) -> Result<std::process::Output> {
11081109
}
11091110

11101111
fn ensure_global_cpp_pch(compiler: &Path) -> Option<PathBuf> {
1112+
static PCH_BUILD_LOCK: OnceLock<Mutex<()>> = OnceLock::new();
1113+
let lock = PCH_BUILD_LOCK.get_or_init(|| Mutex::new(()));
1114+
let _guard = lock.lock().ok()?;
1115+
11111116
let cache_dir = std::env::temp_dir().join("run-compile-cache");
11121117
std::fs::create_dir_all(&cache_dir).ok()?;
11131118
let header = cache_dir.join("run_cpp_pch.hpp");
@@ -1122,7 +1127,9 @@ fn ensure_global_cpp_pch(compiler: &Path) -> Option<PathBuf> {
11221127
"#include <algorithm>\n",
11231128
"#include <utility>\n"
11241129
);
1125-
std::fs::write(&header, contents).ok()?;
1130+
let tmp_header = cache_dir.join(format!("run_cpp_pch.hpp.tmp.{}", std::process::id()));
1131+
std::fs::write(&tmp_header, contents).ok()?;
1132+
std::fs::rename(&tmp_header, &header).ok()?;
11261133
}
11271134
let needs_build = if !gch.exists() {
11281135
true
@@ -1132,21 +1139,24 @@ fn ensure_global_cpp_pch(compiler: &Path) -> Option<PathBuf> {
11321139
h > g
11331140
};
11341141
if needs_build {
1142+
let tmp_gch = cache_dir.join(format!("run_cpp_pch.hpp.gch.tmp.{}", std::process::id()));
11351143
let mut pch_cmd = compiler_command(compiler);
11361144
let out = pch_cmd
11371145
.arg("-std=c++17")
11381146
.arg("-x")
11391147
.arg("c++-header")
11401148
.arg(&header)
11411149
.arg("-o")
1142-
.arg(&gch)
1150+
.arg(&tmp_gch)
11431151
.stdout(Stdio::piped())
11441152
.stderr(Stdio::piped())
11451153
.output()
11461154
.ok()?;
11471155
if !out.status.success() {
1156+
let _ = std::fs::remove_file(&tmp_gch);
11481157
return None;
11491158
}
1159+
std::fs::rename(&tmp_gch, &gch).ok()?;
11501160
}
11511161
Some(header)
11521162
}

src/engine/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ pub fn hash_source(source: &str) -> u64 {
8787
hash
8888
}
8989

90-
9190
fn cache_id(namespace: &str, source_hash: u64) -> String {
9291
format!("{namespace}-{:016x}", source_hash)
9392
}
@@ -297,7 +296,6 @@ pub fn wait_with_timeout(mut child: Child, timeout: Duration) -> Result<std::pro
297296
loop {
298297
match child.try_wait() {
299298
Ok(Some(_status)) => {
300-
301299
return child.wait_with_output().map_err(Into::into);
302300
}
303301
Ok(None) => {

0 commit comments

Comments
 (0)