-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Implement fine grain locking for build-dir
#16155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
epage marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| //! This module handles the locking logic during compilation. | ||
|
|
||
| use crate::{ | ||
| CargoResult, | ||
| core::compiler::{BuildRunner, Unit}, | ||
| util::{FileLock, Filesystem}, | ||
| }; | ||
| use anyhow::bail; | ||
| use std::{ | ||
| collections::HashMap, | ||
| fmt::{Display, Formatter}, | ||
| path::PathBuf, | ||
| sync::Mutex, | ||
| }; | ||
| use tracing::instrument; | ||
|
|
||
| /// A struct to store the lock handles for build units during compilation. | ||
| pub struct LockManager { | ||
| locks: Mutex<HashMap<LockKey, FileLock>>, | ||
| } | ||
|
|
||
| impl LockManager { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| locks: Mutex::new(HashMap::new()), | ||
| } | ||
| } | ||
|
|
||
| /// Takes a shared lock on a given [`Unit`] | ||
| /// This prevents other Cargo instances from compiling (writing) to | ||
| /// this build unit. | ||
| /// | ||
| /// This function returns a [`LockKey`] which can be used to | ||
| /// upgrade/unlock the lock. | ||
| #[instrument(skip_all, fields(key))] | ||
| pub fn lock_shared( | ||
|
epage marked this conversation as resolved.
|
||
| &self, | ||
| build_runner: &BuildRunner<'_, '_>, | ||
| unit: &Unit, | ||
| ) -> CargoResult<LockKey> { | ||
| let key = LockKey::from_unit(build_runner, unit); | ||
| tracing::Span::current().record("key", key.0.to_str()); | ||
|
|
||
| let mut locks = self.locks.lock().unwrap(); | ||
| if let Some(lock) = locks.get_mut(&key) { | ||
| lock.file().lock_shared()?; | ||
| } else { | ||
| let fs = Filesystem::new(key.0.clone()); | ||
| let lock_msg = format!( | ||
| "{} ({})", | ||
| unit.pkg.name(), | ||
| build_runner.files().unit_hash(unit) | ||
| ); | ||
| let lock = fs.open_ro_shared_create(&key.0, build_runner.bcx.gctx, &lock_msg)?; | ||
| locks.insert(key.clone(), lock); | ||
| } | ||
|
epage marked this conversation as resolved.
|
||
|
|
||
| Ok(key) | ||
| } | ||
|
|
||
| #[instrument(skip(self))] | ||
| pub fn lock(&self, key: &LockKey) -> CargoResult<()> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it work to also have a Blocking message for this?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked into this and I think it should be possible. We don't have direct access to I think we can modify the Though perhaps we defer that to a follow up PR? |
||
| let mut locks = self.locks.lock().unwrap(); | ||
| if let Some(lock) = locks.get_mut(&key) { | ||
| lock.file().lock()?; | ||
| } else { | ||
| bail!("lock was not found in lock manager: {key}"); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Upgrades an existing exclusive lock into a shared lock. | ||
| #[instrument(skip(self))] | ||
| pub fn downgrade_to_shared(&self, key: &LockKey) -> CargoResult<()> { | ||
| let mut locks = self.locks.lock().unwrap(); | ||
| let Some(lock) = locks.get_mut(key) else { | ||
| bail!("lock was not found in lock manager: {key}"); | ||
| }; | ||
| lock.file().lock_shared()?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[instrument(skip(self))] | ||
| pub fn unlock(&self, key: &LockKey) -> CargoResult<()> { | ||
| let mut locks = self.locks.lock().unwrap(); | ||
| if let Some(lock) = locks.get_mut(key) { | ||
| lock.file().unlock()?; | ||
| }; | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Hash, Eq, PartialEq)] | ||
| pub struct LockKey(PathBuf); | ||
|
|
||
| impl LockKey { | ||
| fn from_unit(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> Self { | ||
| Self(build_runner.files().build_unit_lock(unit)) | ||
| } | ||
| } | ||
|
|
||
| impl Display for LockKey { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
| write!(f, "{}", self.0.display()) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.