Skip to content

Commit

Permalink
dont use mutex anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
frederik-uni committed Aug 29, 2024
1 parent 7a9d7d8 commit 28c5930
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargotom"
version = "0.5.0"
version = "0.5.1"
edition = "2021"

[dependencies]
Expand Down
40 changes: 19 additions & 21 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum InfoCacheEntry {
impl CratesIoStorage {
pub async fn search_online(&self, query: &str) -> Result<Vec<Crate>, reqwest::Error> {
let notify = Arc::new(Notify::new());
let cache_lock = self.search_cache.lock().await;
let cache_lock = self.search_cache.read().await;

if let Some(entry) = cache_lock.get(query) {
match entry {
Expand All @@ -25,7 +25,7 @@ impl CratesIoStorage {
drop(cache_lock);
cloned_notify.notified().await;

let cache_lock = self.search_cache.lock().await;
let cache_lock = self.search_cache.read().await;
if let Some(SearchCacheEntry::Ready(result)) = cache_lock.get(query) {
return Ok(result.clone());
}
Expand All @@ -36,10 +36,10 @@ impl CratesIoStorage {
}
}

{
let mut cache_lock = self.search_cache.lock().await;
cache_lock.insert(query.to_string(), SearchCacheEntry::Pending(notify.clone()));
}
// {
// let mut cache_lock = self.search_cache.lock().await;
// cache_lock.insert(query.to_string(), SearchCacheEntry::Pending(notify.clone()));
// }

let url = format!(
"https://crates.io/api/v1/crates?page=1&per_page={}&q={}&sort=relevance",
Expand All @@ -55,7 +55,7 @@ impl CratesIoStorage {

match res {
Ok(search_response) => {
let mut cache_lock = self.search_cache.lock().await;
let mut cache_lock = self.search_cache.write().await;
cache_lock.insert(
query.to_string(),
SearchCacheEntry::Ready(search_response.crates.clone()),
Expand All @@ -64,7 +64,7 @@ impl CratesIoStorage {
Ok(search_response.crates)
}
Err(e) => {
let mut cache_lock = self.search_cache.lock().await;
let mut cache_lock = self.search_cache.write().await;
cache_lock.remove(query);
notify.notify_waiters();
Err(e)
Expand All @@ -76,17 +76,17 @@ impl CratesIoStorage {
&self,
name: &str,
) -> Result<Vec<VersionExport>, reqwest::Error> {
let notify = Arc::new(Notify::new());
let cache_lock = self.versions_cache.lock().await;
//let notify = Arc::new(Notify::new());
let cache_lock = self.versions_cache.read().await;

if let Some(entry) = cache_lock.get(name) {
match entry {
InfoCacheEntry::Pending(existing_notify) => {
let cloned_notify = existing_notify.clone();
let notify = existing_notify.clone();
drop(cache_lock);
cloned_notify.notified().await;
notify.notified().await;

let cache_lock = self.versions_cache.lock().await;
let cache_lock = self.versions_cache.read().await;
if let Some(InfoCacheEntry::Ready(result)) = cache_lock.get(name) {
return Ok(result.clone());
}
Expand All @@ -96,11 +96,10 @@ impl CratesIoStorage {
}
}
}
//let mut cache_lock = self.versions_cache.lock().await;

{
let mut cache_lock = self.versions_cache.lock().await;
cache_lock.insert(name.to_string(), InfoCacheEntry::Pending(notify.clone()));
}
//cache_lock.insert(name.to_string(), InfoCacheEntry::Pending(notify.clone()));
//drop(cache_lock);

let url = format!("https://crates.io/api/v1/crates/{}/versions", name);

Expand All @@ -110,18 +109,17 @@ impl CratesIoStorage {
Err(e) => Err(e),
};

let mut cache_lock = self.versions_cache.write().await;
match res {
Ok(search_response) => {
let mut cache_lock = self.versions_cache.lock().await;
let versions = search_response.versions();
cache_lock.insert(name.to_string(), InfoCacheEntry::Ready(versions.clone()));
notify.notify_waiters();
//notify.notify_waiters();
Ok(versions)
}
Err(e) => {
let mut cache_lock = self.search_cache.lock().await;
cache_lock.remove(name);
notify.notify_waiters();
//notify.notify_waiters();
Err(e)
}
}
Expand Down
29 changes: 16 additions & 13 deletions src/crate_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ type OfflineCratesData = Option<Trie<u8, Vec<(String, Vec<String>)>>>;

use reqwest::Client;
use taplo::HashMap;
use tokio::{sync::Mutex, time::sleep};
use tokio::{
sync::{Mutex, RwLock},
time::sleep,
};
use trie_rs::map::{Trie, TrieBuilder};

use crate::{
api::{InfoCacheEntry, RustVersion, SearchCacheEntry},
git::updated_local_git,
};

pub type Shared<T> = Arc<Mutex<T>>;
pub type Shared<T> = Arc<RwLock<T>>;
#[derive(Clone)]
pub struct CratesIoStorage {
pub search_cache: Shared<HashMap<String, SearchCacheEntry>>,
Expand Down Expand Up @@ -83,7 +86,7 @@ fn read_data(path: &Path) -> OfflineCratesData {
}

pub fn shared<T>(t: T) -> Shared<T> {
Arc::new(Mutex::new(t))
Arc::new(RwLock::new(t))
}

impl CratesIoStorage {
Expand All @@ -109,7 +112,7 @@ impl CratesIoStorage {
}

pub async fn search(&self, query: &str) -> Vec<(String, Option<String>, String)> {
let lock = self.data.lock().await;
let lock = self.data.read().await;
if let Some(v) = &*lock {
let search = v
.predictive_search(query.to_lowercase())
Expand Down Expand Up @@ -151,7 +154,7 @@ impl CratesIoStorage {
}

pub async fn get_version_local(&self, name: &str) -> Option<Vec<RustVersion>> {
let lock = self.data.lock().await;
let lock = self.data.read().await;
if let Some(v) = &*lock {
let search = v.exact_match(normalize_key(name))?;
let (_, versions) = search
Expand All @@ -164,7 +167,7 @@ impl CratesIoStorage {
.collect::<Vec<_>>(),
)
} else {
let v = self.versions_cache.lock().await;
let v = self.versions_cache.read().await;
match v.get(name) {
Some(v) => match v {
InfoCacheEntry::Pending(_) => None,
Expand Down Expand Up @@ -200,7 +203,7 @@ impl CratesIoStorage {
}

pub async fn get_versions(&self, name: &str, version_filter: &str) -> Option<Vec<RustVersion>> {
let lock = self.data.lock().await;
let lock = self.data.read().await;
if let Some(v) = &*lock {
let search = v.exact_match(normalize_key(name))?;
let (_, versions) = search
Expand Down Expand Up @@ -234,8 +237,8 @@ fn normalize_key(key: &str) -> String {
pub fn update_thread(data: CratesIoStorage, path: PathBuf) {
tokio::spawn(async move {
let need_update = {
let updating = *data.updating.lock().await;
let last_checked = *data.last_checked.lock().await;
let updating = *data.updating.read().await;
let last_checked = *data.last_checked.read().await;
match updating {
true => false,
false => match last_checked
Expand All @@ -255,13 +258,13 @@ pub fn update_thread(data: CratesIoStorage, path: PathBuf) {
}

async fn update(toml_data: CratesIoStorage, path: &Path) {
*toml_data.updating.lock().await = true;
*toml_data.updating.write().await = true;

let update = updated_local_git(path);
if update {
let data = read_data(path);
*toml_data.data.lock().await = data;
*toml_data.data.write().await = data;
}
*toml_data.updating.lock().await = false;
*toml_data.last_checked.lock().await = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
*toml_data.updating.write().await = false;
*toml_data.last_checked.write().await = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
}
14 changes: 7 additions & 7 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ impl Store {

pub async fn needs_update(
&self,
crates: &Arc<Mutex<CratesIoStorage>>,
crates: &Shared<CratesIoStorage>,
) -> Vec<(String, RangeExclusive, String)> {
let lock = crates.lock().await;
let lock = crates.read().await;
let mut updates = vec![];
for cr in self.crates_info.iter() {
let crate_name = &cr.key.value;
Expand Down Expand Up @@ -141,7 +141,7 @@ impl LanguageServer for Backend {
.map(serde_json::from_value)
.and_then(|v| v.ok())
.unwrap_or_default();
*self.crates.lock().await = CratesIoStorage::new(
*self.crates.write().await = CratesIoStorage::new(
&self.path,
config.stable.unwrap_or(true),
config.offline.unwrap_or(true),
Expand Down Expand Up @@ -332,7 +332,7 @@ impl LanguageServer for Backend {
//todo: use pub text_edit:
let crate_ = &path[0];
if let KeyOrValueOwned::Key(key) = crate_ {
let result = self.crates.lock().await.search(&key.value).await;
let result = self.crates.read().await.search(&key.value).await;
let v = result
.into_iter()
.map(|(name, detail, version)| CompletionItem {
Expand All @@ -358,7 +358,7 @@ impl LanguageServer for Backend {
KeyOrValueOwned::Value(Value::String { value, .. }) => {
if let Some(v) = self
.crates
.lock()
.read()
.await
.get_versions(&key.value, value)
.await
Expand Down Expand Up @@ -392,7 +392,7 @@ impl LanguageServer for Backend {
{
let v = self
.crates
.lock()
.read()
.await
.get_features(
&crate_.value,
Expand All @@ -413,7 +413,7 @@ impl LanguageServer for Backend {
"version}" => {
if let Some(v) = self
.crates
.lock()
.read()
.await
.get_versions(&crate_.value, value)
.await
Expand Down

0 comments on commit 28c5930

Please sign in to comment.