refactor(common): Rename CrossProcessStoreLock* to CrossProcessLock*.

This patch renames `CrossProcessStoreLock` and
`CrossProcessStoreLockGuard` to `CrossProcessLock` and
`CrossProcessLockGuard`.
This commit is contained in:
Ivan Enderlin
2025-09-10 14:51:37 +02:00
parent 0233ac906e
commit 5fe5cfd85f
10 changed files with 50 additions and 55 deletions

View File

@@ -28,7 +28,7 @@ mod memory_store;
mod traits;
use matrix_sdk_common::store_locks::{
BackingStore, CrossProcessStoreLock, CrossProcessStoreLockGuard, LockStoreError,
BackingStore, CrossProcessLock, CrossProcessLockGuard, LockStoreError,
};
pub use matrix_sdk_store_encryption::Error as StoreEncryptionError;
use ruma::{
@@ -49,7 +49,7 @@ pub use self::{
#[derive(Clone)]
pub struct EventCacheStoreLock {
/// The inner cross process lock that is used to lock the `EventCacheStore`.
cross_process_lock: Arc<CrossProcessStoreLock<LockableEventCacheStore>>,
cross_process_lock: Arc<CrossProcessLock<LockableEventCacheStore>>,
/// The store itself.
///
@@ -68,7 +68,7 @@ impl EventCacheStoreLock {
/// Create a new lock around the [`EventCacheStore`].
///
/// The `holder` argument represents the holder inside the
/// [`CrossProcessStoreLock::new`].
/// [`CrossProcessLock::new`].
pub fn new<S>(store: S, holder: String) -> Self
where
S: IntoEventCacheStore,
@@ -76,7 +76,7 @@ impl EventCacheStoreLock {
let store = store.into_event_cache_store();
Self {
cross_process_lock: Arc::new(CrossProcessStoreLock::new(
cross_process_lock: Arc::new(CrossProcessLock::new(
LockableEventCacheStore(store.clone()),
"default".to_owned(),
holder,
@@ -85,7 +85,7 @@ impl EventCacheStoreLock {
}
}
/// Acquire a spin lock (see [`CrossProcessStoreLock::spin_lock`]).
/// Acquire a spin lock (see [`CrossProcessLock::spin_lock`]).
pub async fn lock(&self) -> Result<EventCacheStoreLockGuard<'_>, LockStoreError> {
let cross_process_lock_guard = self.cross_process_lock.spin_lock(None).await?;
@@ -99,7 +99,7 @@ impl EventCacheStoreLock {
pub struct EventCacheStoreLockGuard<'a> {
/// The cross process lock guard.
#[allow(unused)]
cross_process_lock_guard: CrossProcessStoreLockGuard,
cross_process_lock_guard: CrossProcessLockGuard,
/// A reference to the store.
store: &'a DynEventCacheStore,

View File

@@ -32,7 +32,7 @@ use std::fmt;
use std::{ops::Deref, sync::Arc};
use matrix_sdk_common::store_locks::{
BackingStore, CrossProcessStoreLock, CrossProcessStoreLockGuard, LockStoreError,
BackingStore, CrossProcessLock, CrossProcessLockGuard, LockStoreError,
};
use matrix_sdk_store_encryption::Error as StoreEncryptionError;
pub use traits::{DynMediaStore, IntoMediaStore, MediaStore, MediaStoreInner};
@@ -88,7 +88,7 @@ pub type Result<T, E = MediaStoreError> = std::result::Result<T, E>;
#[derive(Clone)]
pub struct MediaStoreLock {
/// The inner cross process lock that is used to lock the `MediaStore`.
cross_process_lock: Arc<CrossProcessStoreLock<LockableMediaStore>>,
cross_process_lock: Arc<CrossProcessLock<LockableMediaStore>>,
/// The store itself.
///
@@ -107,7 +107,7 @@ impl MediaStoreLock {
/// Create a new lock around the [`MediaStore`].
///
/// The `holder` argument represents the holder inside the
/// [`CrossProcessStoreLock::new`].
/// [`CrossProcessLock::new`].
pub fn new<S>(store: S, holder: String) -> Self
where
S: IntoMediaStore,
@@ -115,7 +115,7 @@ impl MediaStoreLock {
let store = store.into_media_store();
Self {
cross_process_lock: Arc::new(CrossProcessStoreLock::new(
cross_process_lock: Arc::new(CrossProcessLock::new(
LockableMediaStore(store.clone()),
"default".to_owned(),
holder,
@@ -124,7 +124,7 @@ impl MediaStoreLock {
}
}
/// Acquire a spin lock (see [`CrossProcessStoreLock::spin_lock`]).
/// Acquire a spin lock (see [`CrossProcessLock::spin_lock`]).
pub async fn lock(&self) -> Result<MediaStoreLockGuard<'_>, LockStoreError> {
let cross_process_lock_guard = self.cross_process_lock.spin_lock(None).await?;
@@ -138,7 +138,7 @@ impl MediaStoreLock {
pub struct MediaStoreLockGuard<'a> {
/// The cross process lock guard.
#[allow(unused)]
cross_process_lock_guard: CrossProcessStoreLockGuard,
cross_process_lock_guard: CrossProcessLockGuard,
/// A reference to the store.
store: &'a DynMediaStore,

View File

@@ -791,7 +791,7 @@ impl StoreConfig {
/// Create a new default `StoreConfig`.
///
/// To learn more about `cross_process_store_locks_holder_name`, please read
/// [`CrossProcessStoreLock::new`](matrix_sdk_common::store_locks::CrossProcessStoreLock::new).
/// [`CrossProcessLock::new`](matrix_sdk_common::store_locks::CrossProcessLock::new).
#[must_use]
pub fn new(cross_process_store_locks_holder_name: String) -> Self {
Self {

View File

@@ -82,16 +82,16 @@ enum WaitingTime {
Stop,
}
/// A guard on the store lock.
/// A guard of a cross-process lock.
///
/// The lock will be automatically released a short period of time after all the
/// guards have dropped.
#[derive(Debug)]
pub struct CrossProcessStoreLockGuard {
pub struct CrossProcessLockGuard {
num_holders: Arc<AtomicU32>,
}
impl Drop for CrossProcessStoreLockGuard {
impl Drop for CrossProcessLockGuard {
fn drop(&mut self) {
self.num_holders.fetch_sub(1, atomic::Ordering::SeqCst);
}
@@ -101,7 +101,7 @@ impl Drop for CrossProcessStoreLockGuard {
///
/// See the doc-comment of this module for more information.
#[derive(Clone, Debug)]
pub struct CrossProcessStoreLock<S: BackingStore + Clone + SendOutsideWasm + 'static> {
pub struct CrossProcessLock<S: BackingStore + Clone + SendOutsideWasm + 'static> {
/// The store we're using to lock.
store: S,
@@ -149,7 +149,7 @@ const INITIAL_BACKOFF_MS: u32 = 10;
/// we'll wait for the lock, *between two attempts*.
pub const MAX_BACKOFF_MS: u32 = 1000;
impl<S: BackingStore + Clone + SendOutsideWasm + 'static> CrossProcessStoreLock<S> {
impl<S: BackingStore + Clone + SendOutsideWasm + 'static> CrossProcessLock<S> {
/// Create a new store-based lock implemented as a value in the store.
///
/// # Parameters
@@ -170,9 +170,7 @@ impl<S: BackingStore + Clone + SendOutsideWasm + 'static> CrossProcessStoreLock<
/// Try to lock once, returns whether the lock was obtained or not.
#[instrument(skip(self), fields(?self.lock_key, ?self.lock_holder))]
pub async fn try_lock_once(
&self,
) -> Result<Option<CrossProcessStoreLockGuard>, LockStoreError> {
pub async fn try_lock_once(&self) -> Result<Option<CrossProcessLockGuard>, LockStoreError> {
// Hold onto the locking attempt mutex for the entire lifetime of this
// function, to avoid multiple reentrant calls.
let mut _attempt = self.locking_attempt.lock().await;
@@ -186,7 +184,7 @@ impl<S: BackingStore + Clone + SendOutsideWasm + 'static> CrossProcessStoreLock<
// taken by at least one thread.
trace!("We already had the lock, incrementing holder count");
self.num_holders.fetch_add(1, atomic::Ordering::SeqCst);
let guard = CrossProcessStoreLockGuard { num_holders: self.num_holders.clone() };
let guard = CrossProcessLockGuard { num_holders: self.num_holders.clone() };
return Ok(Some(guard));
}
@@ -266,7 +264,7 @@ impl<S: BackingStore + Clone + SendOutsideWasm + 'static> CrossProcessStoreLock<
self.num_holders.fetch_add(1, atomic::Ordering::SeqCst);
let guard = CrossProcessStoreLockGuard { num_holders: self.num_holders.clone() };
let guard = CrossProcessLockGuard { num_holders: self.num_holders.clone() };
Ok(Some(guard))
}
@@ -282,7 +280,7 @@ impl<S: BackingStore + Clone + SendOutsideWasm + 'static> CrossProcessStoreLock<
pub async fn spin_lock(
&self,
max_backoff: Option<u32>,
) -> Result<CrossProcessStoreLockGuard, LockStoreError> {
) -> Result<CrossProcessLockGuard, LockStoreError> {
let max_backoff = max_backoff.unwrap_or(MAX_BACKOFF_MS);
// Note: reads/writes to the backoff are racy across threads in theory, but the
@@ -359,7 +357,7 @@ mod tests {
};
use super::{
BackingStore, CrossProcessStoreLock, CrossProcessStoreLockGuard, EXTEND_LEASE_EVERY_MS,
BackingStore, CrossProcessLock, CrossProcessLockGuard, EXTEND_LEASE_EVERY_MS,
LockStoreError, memory_store_helper::try_take_leased_lock,
};
@@ -391,7 +389,7 @@ mod tests {
}
}
async fn release_lock(guard: Option<CrossProcessStoreLockGuard>) {
async fn release_lock(guard: Option<CrossProcessLockGuard>) {
drop(guard);
sleep(Duration::from_millis(EXTEND_LEASE_EVERY_MS)).await;
}
@@ -401,7 +399,7 @@ mod tests {
#[async_test]
async fn test_simple_lock_unlock() -> TestResult {
let store = TestStore::default();
let lock = CrossProcessStoreLock::new(store, "key".to_owned(), "first".to_owned());
let lock = CrossProcessLock::new(store, "key".to_owned(), "first".to_owned());
// The lock plain works when used with a single holder.
let acquired = lock.try_lock_once().await?;
@@ -425,7 +423,7 @@ mod tests {
#[async_test]
async fn test_self_recovery() -> TestResult {
let store = TestStore::default();
let lock = CrossProcessStoreLock::new(store.clone(), "key".to_owned(), "first".to_owned());
let lock = CrossProcessLock::new(store.clone(), "key".to_owned(), "first".to_owned());
// When a lock is acquired...
let acquired = lock.try_lock_once().await?;
@@ -436,7 +434,7 @@ mod tests {
drop(lock);
// And when rematerializing the lock with the same key/value...
let lock = CrossProcessStoreLock::new(store.clone(), "key".to_owned(), "first".to_owned());
let lock = CrossProcessLock::new(store.clone(), "key".to_owned(), "first".to_owned());
// We still got it.
let acquired = lock.try_lock_once().await?;
@@ -449,7 +447,7 @@ mod tests {
#[async_test]
async fn test_multiple_holders_same_process() -> TestResult {
let store = TestStore::default();
let lock = CrossProcessStoreLock::new(store, "key".to_owned(), "first".to_owned());
let lock = CrossProcessLock::new(store, "key".to_owned(), "first".to_owned());
// Taking the lock twice...
let acquired = lock.try_lock_once().await?;
@@ -473,8 +471,8 @@ mod tests {
#[async_test]
async fn test_multiple_processes() -> TestResult {
let store = TestStore::default();
let lock1 = CrossProcessStoreLock::new(store.clone(), "key".to_owned(), "first".to_owned());
let lock2 = CrossProcessStoreLock::new(store, "key".to_owned(), "second".to_owned());
let lock1 = CrossProcessLock::new(store.clone(), "key".to_owned(), "first".to_owned());
let lock2 = CrossProcessLock::new(store, "key".to_owned(), "second".to_owned());
// When the first process takes the lock...
let acquired1 = lock1.try_lock_once().await?;

View File

@@ -2,7 +2,7 @@ use std::{future, ops::Deref, sync::Arc};
use futures_core::Stream;
use futures_util::StreamExt;
use matrix_sdk_common::store_locks::CrossProcessStoreLock;
use matrix_sdk_common::store_locks::CrossProcessLock;
use ruma::{DeviceId, OwnedDeviceId, OwnedUserId, UserId};
use tokio::sync::{broadcast, Mutex};
use tokio_stream::wrappers::{errors::BroadcastStreamRecvError, BroadcastStream};
@@ -390,14 +390,14 @@ impl CryptoStoreWrapper {
})
}
/// Creates a `CrossProcessStoreLock` for this store, that will contain the
/// Creates a [`CrossProcessLock`] for this store, that will contain the
/// given key and value when hold.
pub(crate) fn create_store_lock(
&self,
lock_key: String,
lock_value: String,
) -> CrossProcessStoreLock<LockableCryptoStore> {
CrossProcessStoreLock::new(LockableCryptoStore(self.store.clone()), lock_key, lock_value)
) -> CrossProcessLock<LockableCryptoStore> {
CrossProcessLock::new(LockableCryptoStore(self.store.clone()), lock_key, lock_value)
}
}

View File

@@ -99,7 +99,7 @@ pub mod integration_tests;
pub(crate) use crypto_store_wrapper::CryptoStoreWrapper;
pub use error::{CryptoStoreError, Result};
use matrix_sdk_common::{
deserialized_responses::WithheldCode, store_locks::CrossProcessStoreLock, timeout::timeout,
deserialized_responses::WithheldCode, store_locks::CrossProcessLock, timeout::timeout,
};
pub use memorystore::MemoryStore;
pub use traits::{CryptoStore, DynCryptoStore, IntoCryptoStore};
@@ -1261,13 +1261,13 @@ impl Store {
self.inner.store.identities_stream().map(|(_, identities, devices)| (identities, devices))
}
/// Creates a `CrossProcessStoreLock` for this store, that will contain the
/// Creates a [`CrossProcessLock`] for this store, that will contain the
/// given key and value when hold.
pub fn create_store_lock(
&self,
lock_key: String,
lock_value: String,
) -> CrossProcessStoreLock<LockableCryptoStore> {
) -> CrossProcessLock<LockableCryptoStore> {
self.inner.store.create_store_lock(lock_key, lock_value)
}

View File

@@ -5,9 +5,7 @@ use matrix_sdk_base::crypto::{
store::{LockableCryptoStore, Store},
CryptoStoreError,
};
use matrix_sdk_common::store_locks::{
CrossProcessStoreLock, CrossProcessStoreLockGuard, LockStoreError,
};
use matrix_sdk_common::store_locks::{CrossProcessLock, CrossProcessLockGuard, LockStoreError};
use sha2::{Digest as _, Sha256};
use thiserror::Error;
use tokio::sync::{Mutex, OwnedMutexGuard};
@@ -61,13 +59,13 @@ fn compute_session_hash(tokens: &SessionTokens) -> SessionHash {
#[derive(Clone)]
pub(super) struct CrossProcessRefreshManager {
store: Store,
store_lock: CrossProcessStoreLock<LockableCryptoStore>,
store_lock: CrossProcessLock<LockableCryptoStore>,
known_session_hash: Arc<Mutex<Option<SessionHash>>>,
}
impl CrossProcessRefreshManager {
/// Create a new `CrossProcessRefreshManager`.
pub fn new(store: Store, lock: CrossProcessStoreLock<LockableCryptoStore>) -> Self {
pub fn new(store: Store, lock: CrossProcessLock<LockableCryptoStore>) -> Self {
Self { store, store_lock: lock, known_session_hash: Arc::new(Mutex::new(None)) }
}
@@ -134,7 +132,7 @@ pub(super) struct CrossProcessRefreshLockGuard {
hash_guard: OwnedMutexGuard<Option<SessionHash>>,
/// Cross-process lock being hold.
_store_guard: CrossProcessStoreLockGuard,
_store_guard: CrossProcessLockGuard,
/// Reference to the underlying store, for storing the hash of the latest
/// known session (as a custom value).

View File

@@ -482,7 +482,7 @@ impl ClientBuilder {
/// Set the cross-process store locks holder name.
///
/// The SDK provides cross-process store locks (see
/// [`matrix_sdk_common::store_locks::CrossProcessStoreLock`]). The
/// [`matrix_sdk_common::store_locks::CrossProcessLock`]). The
/// `holder_name` will be the value used for all cross-process store locks
/// used by the `Client` being built.
///

View File

@@ -111,7 +111,7 @@ use crate::{
#[cfg(feature = "e2e-encryption")]
use crate::{
encryption::{Encryption, EncryptionData, EncryptionSettings, VerificationState},
store_locks::CrossProcessStoreLock,
store_locks::CrossProcessLock,
};
mod builder;
@@ -235,8 +235,7 @@ pub(crate) struct ClientLocks {
pub(crate) read_receipt_deduplicated_handler: DeduplicatingHandler<(String, OwnedEventId)>,
#[cfg(feature = "e2e-encryption")]
pub(crate) cross_process_crypto_store_lock:
OnceCell<CrossProcessStoreLock<LockableCryptoStore>>,
pub(crate) cross_process_crypto_store_lock: OnceCell<CrossProcessLock<LockableCryptoStore>>,
/// Latest "generation" of data known by the crypto store.
///
@@ -302,7 +301,7 @@ pub(crate) struct ClientInner {
/// The cross-process store locks holder name.
///
/// The SDK provides cross-process store locks (see
/// [`matrix_sdk_common::store_locks::CrossProcessStoreLock`]). The
/// [`matrix_sdk_common::store_locks::CrossProcessLock`]). The
/// `holder_name` is the value used for all cross-process store locks
/// used by this `Client`.
///
@@ -520,7 +519,7 @@ impl Client {
/// The cross-process store locks holder name.
///
/// The SDK provides cross-process store locks (see
/// [`matrix_sdk_common::store_locks::CrossProcessStoreLock`]). The
/// [`matrix_sdk_common::store_locks::CrossProcessLock`]). The
/// `holder_name` is the value used for all cross-process store locks
/// used by this `Client`.
pub fn cross_process_store_locks_holder_name(&self) -> &str {
@@ -2765,10 +2764,10 @@ impl Client {
/// Create a new specialized `Client` that can process notifications.
///
/// See [`CrossProcessStoreLock::new`] to learn more about
/// See [`CrossProcessLock::new`] to learn more about
/// `cross_process_store_locks_holder_name`.
///
/// [`CrossProcessStoreLock::new`]: matrix_sdk_common::store_locks::CrossProcessStoreLock::new
/// [`CrossProcessLock::new`]: matrix_sdk_common::store_locks::CrossProcessLock::new
pub async fn notification_client(
&self,
cross_process_store_locks_holder_name: String,

View File

@@ -92,7 +92,7 @@ use crate::{
attachment::Thumbnail,
client::{ClientInner, WeakClient},
error::HttpResult,
store_locks::CrossProcessStoreLockGuard,
store_locks::CrossProcessLockGuard,
Client, Error, HttpError, Result, Room, RumaApiError, TransmissionProgress,
};
@@ -238,7 +238,7 @@ pub enum VerificationState {
/// Wraps together a `CrossProcessLockStoreGuard` and a generation number.
#[derive(Debug)]
pub struct CrossProcessLockStoreGuardWithGeneration {
_guard: CrossProcessStoreLockGuard,
_guard: CrossProcessLockGuard,
generation: u64,
}