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