mirror of
https://github.com/astral-sh/uv.git
synced 2025-07-07 21:35:00 +00:00

## Summary All of the resolver code is run on the main thread, so a lot of the `Send` bounds and uses of `DashMap` and `Arc` are unnecessary. We could also switch to using single-threaded versions of `Mutex` and `Notify` in some places, but there isn't really a crate that provides those I would be comfortable with using. The `Arc` in `OnceMap` can't easily be removed because of the uv-auth code which uses the [reqwest-middleware](https://docs.rs/reqwest-middleware/latest/reqwest_middleware/trait.Middleware.html) crate, that seems to adds unnecessary `Send` bounds because of `async-trait`. We could duplicate the code and create a `OnceMapLocal` variant, but I don't feel that's worth it.
20 lines
600 B
Rust
20 lines
600 B
Rust
use std::rc::Rc;
|
|
|
|
use rustc_hash::FxHashMap;
|
|
use tokio::sync::Mutex;
|
|
|
|
use distribution_types::{Identifier, ResourceId};
|
|
|
|
/// A set of locks used to prevent concurrent access to the same resource.
|
|
#[derive(Debug, Default)]
|
|
pub(crate) struct Locks(Mutex<FxHashMap<ResourceId, Rc<Mutex<()>>>>);
|
|
|
|
impl Locks {
|
|
/// Acquire a lock on the given resource.
|
|
pub(crate) async fn acquire(&self, dist: &impl Identifier) -> Rc<Mutex<()>> {
|
|
let mut map = self.0.lock().await;
|
|
map.entry(dist.resource_id())
|
|
.or_insert_with(|| Rc::new(Mutex::new(())))
|
|
.clone()
|
|
}
|
|
}
|