add try_lock to MutexMap; allow TryFrom constructions
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
49023aa295
commit
7c0c029a4a
4 changed files with 71 additions and 19 deletions
26
Cargo.lock
generated
26
Cargo.lock
generated
|
@ -3188,7 +3188,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma"
|
name = "ruma"
|
||||||
version = "0.10.1"
|
version = "0.10.1"
|
||||||
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
|
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"assign",
|
"assign",
|
||||||
"js_int",
|
"js_int",
|
||||||
|
@ -3210,7 +3210,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-appservice-api"
|
name = "ruma-appservice-api"
|
||||||
version = "0.10.0"
|
version = "0.10.0"
|
||||||
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
|
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"js_int",
|
"js_int",
|
||||||
"ruma-common",
|
"ruma-common",
|
||||||
|
@ -3222,7 +3222,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-client-api"
|
name = "ruma-client-api"
|
||||||
version = "0.18.0"
|
version = "0.18.0"
|
||||||
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
|
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"as_variant",
|
"as_variant",
|
||||||
"assign",
|
"assign",
|
||||||
|
@ -3245,7 +3245,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-common"
|
name = "ruma-common"
|
||||||
version = "0.13.0"
|
version = "0.13.0"
|
||||||
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
|
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"as_variant",
|
"as_variant",
|
||||||
"base64 0.22.1",
|
"base64 0.22.1",
|
||||||
|
@ -3276,7 +3276,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-events"
|
name = "ruma-events"
|
||||||
version = "0.28.1"
|
version = "0.28.1"
|
||||||
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
|
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"as_variant",
|
"as_variant",
|
||||||
"indexmap 2.7.0",
|
"indexmap 2.7.0",
|
||||||
|
@ -3301,7 +3301,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-federation-api"
|
name = "ruma-federation-api"
|
||||||
version = "0.9.0"
|
version = "0.9.0"
|
||||||
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
|
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytes",
|
"bytes",
|
||||||
"http",
|
"http",
|
||||||
|
@ -3319,7 +3319,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-identifiers-validation"
|
name = "ruma-identifiers-validation"
|
||||||
version = "0.9.5"
|
version = "0.9.5"
|
||||||
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
|
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"js_int",
|
"js_int",
|
||||||
"thiserror 2.0.11",
|
"thiserror 2.0.11",
|
||||||
|
@ -3328,7 +3328,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-identity-service-api"
|
name = "ruma-identity-service-api"
|
||||||
version = "0.9.0"
|
version = "0.9.0"
|
||||||
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
|
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"js_int",
|
"js_int",
|
||||||
"ruma-common",
|
"ruma-common",
|
||||||
|
@ -3338,7 +3338,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-macros"
|
name = "ruma-macros"
|
||||||
version = "0.13.0"
|
version = "0.13.0"
|
||||||
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
|
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"proc-macro-crate",
|
"proc-macro-crate",
|
||||||
|
@ -3353,7 +3353,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-push-gateway-api"
|
name = "ruma-push-gateway-api"
|
||||||
version = "0.9.0"
|
version = "0.9.0"
|
||||||
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
|
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"js_int",
|
"js_int",
|
||||||
"ruma-common",
|
"ruma-common",
|
||||||
|
@ -3365,7 +3365,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-server-util"
|
name = "ruma-server-util"
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
|
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"headers",
|
"headers",
|
||||||
"http",
|
"http",
|
||||||
|
@ -3378,7 +3378,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-signatures"
|
name = "ruma-signatures"
|
||||||
version = "0.15.0"
|
version = "0.15.0"
|
||||||
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
|
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64 0.22.1",
|
"base64 0.22.1",
|
||||||
"ed25519-dalek",
|
"ed25519-dalek",
|
||||||
|
@ -3394,7 +3394,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruma-state-res"
|
name = "ruma-state-res"
|
||||||
version = "0.11.0"
|
version = "0.11.0"
|
||||||
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
|
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"js_int",
|
"js_int",
|
||||||
|
|
|
@ -332,7 +332,7 @@ version = "0.1.2"
|
||||||
[workspace.dependencies.ruma]
|
[workspace.dependencies.ruma]
|
||||||
git = "https://github.com/girlbossceo/ruwuma"
|
git = "https://github.com/girlbossceo/ruwuma"
|
||||||
#branch = "conduwuit-changes"
|
#branch = "conduwuit-changes"
|
||||||
rev = "c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
|
rev = "427877d5bc14988ed877e500bbb27f8bc08b84e8"
|
||||||
features = [
|
features = [
|
||||||
"compat",
|
"compat",
|
||||||
"rand",
|
"rand",
|
||||||
|
|
|
@ -725,7 +725,7 @@ pub(super) async fn force_set_room_state_from_server(
|
||||||
.save_state(room_id.clone().as_ref(), new_room_state)
|
.save_state(room_id.clone().as_ref(), new_room_state)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let state_lock = self.services.rooms.state.mutex.lock(&room_id).await;
|
let state_lock = self.services.rooms.state.mutex.lock(&*room_id).await;
|
||||||
self.services
|
self.services
|
||||||
.rooms
|
.rooms
|
||||||
.state
|
.state
|
||||||
|
|
|
@ -1,7 +1,13 @@
|
||||||
use std::{fmt::Debug, hash::Hash, sync::Arc};
|
use std::{
|
||||||
|
fmt::Debug,
|
||||||
|
hash::Hash,
|
||||||
|
sync::{Arc, TryLockError::WouldBlock},
|
||||||
|
};
|
||||||
|
|
||||||
use tokio::sync::OwnedMutexGuard as Omg;
|
use tokio::sync::OwnedMutexGuard as Omg;
|
||||||
|
|
||||||
|
use crate::{err, Result};
|
||||||
|
|
||||||
/// Map of Mutexes
|
/// Map of Mutexes
|
||||||
pub struct MutexMap<Key, Val> {
|
pub struct MutexMap<Key, Val> {
|
||||||
map: Map<Key, Val>,
|
map: Map<Key, Val>,
|
||||||
|
@ -30,16 +36,17 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(level = "trace", skip(self))]
|
#[tracing::instrument(level = "trace", skip(self))]
|
||||||
pub async fn lock<K>(&self, k: &K) -> Guard<Key, Val>
|
pub async fn lock<'a, K>(&'a self, k: &'a K) -> Guard<Key, Val>
|
||||||
where
|
where
|
||||||
K: Debug + Send + ?Sized + Sync,
|
K: Debug + Send + ?Sized + Sync,
|
||||||
Key: for<'a> From<&'a K>,
|
Key: TryFrom<&'a K>,
|
||||||
|
<Key as TryFrom<&'a K>>::Error: Debug,
|
||||||
{
|
{
|
||||||
let val = self
|
let val = self
|
||||||
.map
|
.map
|
||||||
.lock()
|
.lock()
|
||||||
.expect("locked")
|
.expect("locked")
|
||||||
.entry(k.into())
|
.entry(k.try_into().expect("failed to construct key"))
|
||||||
.or_default()
|
.or_default()
|
||||||
.clone();
|
.clone();
|
||||||
|
|
||||||
|
@ -49,6 +56,51 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(level = "trace", skip(self))]
|
||||||
|
pub fn try_lock<'a, K>(&self, k: &'a K) -> Result<Guard<Key, Val>>
|
||||||
|
where
|
||||||
|
K: Debug + Send + ?Sized + Sync,
|
||||||
|
Key: TryFrom<&'a K>,
|
||||||
|
<Key as TryFrom<&'a K>>::Error: Debug,
|
||||||
|
{
|
||||||
|
let val = self
|
||||||
|
.map
|
||||||
|
.lock()
|
||||||
|
.expect("locked")
|
||||||
|
.entry(k.try_into().expect("failed to construct key"))
|
||||||
|
.or_default()
|
||||||
|
.clone();
|
||||||
|
|
||||||
|
Ok(Guard::<Key, Val> {
|
||||||
|
map: Arc::clone(&self.map),
|
||||||
|
val: val.try_lock_owned().map_err(|_| err!("would yield"))?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(level = "trace", skip(self))]
|
||||||
|
pub fn try_try_lock<'a, K>(&self, k: &'a K) -> Result<Guard<Key, Val>>
|
||||||
|
where
|
||||||
|
K: Debug + Send + ?Sized + Sync,
|
||||||
|
Key: TryFrom<&'a K>,
|
||||||
|
<Key as TryFrom<&'a K>>::Error: Debug,
|
||||||
|
{
|
||||||
|
let val = self
|
||||||
|
.map
|
||||||
|
.try_lock()
|
||||||
|
.map_err(|e| match e {
|
||||||
|
| WouldBlock => err!("would block"),
|
||||||
|
| _ => panic!("{e:?}"),
|
||||||
|
})?
|
||||||
|
.entry(k.try_into().expect("failed to construct key"))
|
||||||
|
.or_default()
|
||||||
|
.clone();
|
||||||
|
|
||||||
|
Ok(Guard::<Key, Val> {
|
||||||
|
map: Arc::clone(&self.map),
|
||||||
|
val: val.try_lock_owned().map_err(|_| err!("would yield"))?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn contains(&self, k: &Key) -> bool { self.map.lock().expect("locked").contains_key(k) }
|
pub fn contains(&self, k: &Key) -> bool { self.map.lock().expect("locked").contains_key(k) }
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue