|
5 | 5 | //! parent directory, and otherwise documentation can be found throughout the `build`
|
6 | 6 | //! directory in each respective module.
|
7 | 7 |
|
8 |
| -use std::fs::{self, OpenOptions}; |
9 |
| -use std::io::{self, BufRead, BufReader, IsTerminal, Write}; |
| 8 | +use std::fs::{self, OpenOptions, TryLockError}; |
| 9 | +use std::io::{self, BufRead, BufReader, IsTerminal, Read, Write}; |
10 | 10 | use std::path::Path;
|
11 | 11 | use std::str::FromStr;
|
12 | 12 | use std::time::Instant;
|
@@ -39,38 +39,34 @@ fn main() {
|
39 | 39 | let config = Config::parse(flags);
|
40 | 40 |
|
41 | 41 | let mut build_lock;
|
42 |
| - let _build_lock_guard; |
43 | 42 |
|
44 | 43 | if !config.bypass_bootstrap_lock {
|
45 | 44 | // Display PID of process holding the lock
|
46 | 45 | // PID will be stored in a lock file
|
47 | 46 | let lock_path = config.out.join("lock");
|
48 |
| - let pid = fs::read_to_string(&lock_path); |
49 |
| - |
50 |
| - build_lock = fd_lock::RwLock::new(t!(fs::OpenOptions::new() |
| 47 | + build_lock = t!(fs::OpenOptions::new() |
| 48 | + .read(true) |
51 | 49 | .write(true)
|
52 |
| - .truncate(true) |
53 | 50 | .create(true)
|
54 |
| - .open(&lock_path))); |
55 |
| - _build_lock_guard = match build_lock.try_write() { |
56 |
| - Ok(mut lock) => { |
57 |
| - t!(lock.write(process::id().to_string().as_ref())); |
58 |
| - lock |
| 51 | + .truncate(false) |
| 52 | + .open(&lock_path)); |
| 53 | + t!(build_lock.try_lock().or_else(|e| { |
| 54 | + if let TryLockError::Error(e) = e { |
| 55 | + return Err(e); |
59 | 56 | }
|
60 |
| - err => { |
61 |
| - drop(err); |
62 |
| - // #135972: We can reach this point when the lock has been taken, |
63 |
| - // but the locker has not yet written its PID to the file |
64 |
| - if let Some(pid) = pid.ok().filter(|pid| !pid.is_empty()) { |
65 |
| - println!("WARNING: build directory locked by process {pid}, waiting for lock"); |
66 |
| - } else { |
67 |
| - println!("WARNING: build directory locked, waiting for lock"); |
68 |
| - } |
69 |
| - let mut lock = t!(build_lock.write()); |
70 |
| - t!(lock.write(process::id().to_string().as_ref())); |
71 |
| - lock |
| 57 | + let mut pid = String::new(); |
| 58 | + t!(build_lock.read_to_string(&mut pid)); |
| 59 | + // #135972: We can reach this point when the lock has been taken, |
| 60 | + // but the locker has not yet written its PID to the file |
| 61 | + if !pid.is_empty() { |
| 62 | + println!("WARNING: build directory locked by process {pid}, waiting for lock"); |
| 63 | + } else { |
| 64 | + println!("WARNING: build directory locked, waiting for lock"); |
72 | 65 | }
|
73 |
| - }; |
| 66 | + build_lock.lock() |
| 67 | + })); |
| 68 | + t!(build_lock.set_len(0)); |
| 69 | + t!(build_lock.write_all(process::id().to_string().as_bytes())); |
74 | 70 | }
|
75 | 71 |
|
76 | 72 | // check_version warnings are not printed during setup, or during CI
|
|
0 commit comments