forked from rust-lang/cargo
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lock all cargo invocations with an OS semaphore.
Using cargo concurrently on the same system often results in badness such as corruption of the global git database, corruption of the local target directory, corruption of output artifacts, etc. This is a pretty rare situation, but is quite troubling to track down when it occurs. To prevent this badness, cargo needs some method of synchronizing with other cargo processes. This is often done with a file lock, but sadly file locks are difficult to use correctly on linux and other flavors of unix [1]. Instead, another mechanism, an OS semaphore, is used. Semaphores provide the exact functionality that is required here as there's no real need to lock a file but rather just synchronize with another process. I wrote external bindings for these semaphores [2] instead of bundling it directly in cargo itself. Currently only windows, linux, and OSX are supported in ipc-rs, but adding new platforms with System V compatibility is just a matter of looking up some constants (nothing too too hard). [1]: http://0pointer.de/blog/projects/locking.html [2]: https://github.com/alexcrichton/ipc-rs Closes rust-lang#354
- Loading branch information
1 parent
0569c62
commit dbbc696
Showing
6 changed files
with
45 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use term; | ||
use ipc::Semaphore; | ||
|
||
use util::{CargoResult, ChainError, internal}; | ||
use core::MultiShell; | ||
|
||
#[must_use] | ||
pub struct Guard { | ||
sem: Semaphore, | ||
} | ||
|
||
pub fn global_lock(shell: &mut MultiShell) -> CargoResult<Guard> { | ||
let sem = try!(Semaphore::new("cargo-lock", 1).chain_error(|| { | ||
internal("failed to create the OS semaphore") | ||
})); | ||
|
||
if !sem.try_acquire() { | ||
try!(shell.say("Waiting for another cargo process to exit...", | ||
term::color::YELLOW)); | ||
sem.acquire() | ||
} | ||
Ok(Guard { sem: sem }) | ||
} | ||
|
||
impl Drop for Guard { | ||
fn drop(&mut self) { | ||
self.sem.release(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters