Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions crates/matrix-sdk/src/event_cache/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,14 +721,17 @@ mod private {
/// the same time.
pub struct RoomEventCacheStateLock {
/// The per-thread lock around the real state.
locked_state: RwLock<RoomEventCacheStateLockInner>,
locked_state: RwLock<RoomEventCacheState>,

/// A lock taken to avoid multiple attempts to upgrade from a read lock
/// to a write lock.
///
/// Please see inline comment of [`Self::read`] to understand why it
/// exists.
read_lock_acquisition: Mutex<()>,
state_lock_upgrade_mutex: Mutex<()>,
}

struct RoomEventCacheStateLockInner {
struct RoomEventCacheState {
/// Whether thread support has been enabled for the event cache.
enabled_thread_support: bool,

Expand Down Expand Up @@ -864,7 +867,7 @@ mod private {
let waited_for_initial_prev_token = Arc::new(AtomicBool::new(false));

Ok(Self {
locked_state: RwLock::new(RoomEventCacheStateLockInner {
locked_state: RwLock::new(RoomEventCacheState {
enabled_thread_support,
room_id,
store,
Expand All @@ -885,7 +888,7 @@ mod private {
waited_for_initial_prev_token,
subscriber_count: Default::default(),
}),
read_lock_acquisition: Mutex::new(()),
state_lock_upgrade_mutex: Mutex::new(()),
})
}

Expand Down Expand Up @@ -945,7 +948,7 @@ mod private {
//
// [^1]: https://docs.rs/lock_api/0.4.14/lock_api/struct.RwLock.html#method.upgradable_read
// [^2]: https://docs.rs/async-lock/3.4.1/async_lock/struct.RwLock.html#method.upgradable_read
let _one_reader_guard = self.read_lock_acquisition.lock().await;
let _state_lock_upgrade_guard = self.state_lock_upgrade_mutex.lock().await;

// Obtain a read lock.
let state_guard = self.locked_state.read().await;
Expand All @@ -957,7 +960,7 @@ mod private {
EventCacheStoreLockState::Dirty(store_guard) => {
// Drop the read lock, and take a write lock to modify the state.
// This is safe because only one reader at a time (see
// `Self::read_lock_acquisition`) is allowed.
// `Self::state_lock_upgrade_mutex`) is allowed.
drop(state_guard);
let state_guard = self.locked_state.write().await;

Expand Down Expand Up @@ -1053,19 +1056,17 @@ mod private {

/// The read lock guard returned by [`RoomEventCacheStateLock::read`].
pub struct RoomEventCacheStateLockReadGuard<'a> {
/// The per-thread read lock guard over the
/// [`RoomEventCacheStateLockInner`].
state: RwLockReadGuard<'a, RoomEventCacheStateLockInner>,
/// The per-thread read lock guard over the [`RoomEventCacheState`].
state: RwLockReadGuard<'a, RoomEventCacheState>,

/// The cross-process lock guard over the store.
store: EventCacheStoreLockGuard,
}

/// The write lock guard return by [`RoomEventCacheStateLock::write`].
pub struct RoomEventCacheStateLockWriteGuard<'a> {
/// The per-thread write lock guard over the
/// [`RoomEventCacheStateLockInner`].
state: RwLockWriteGuard<'a, RoomEventCacheStateLockInner>,
/// The per-thread write lock guard over the [`RoomEventCacheState`].
state: RwLockWriteGuard<'a, RoomEventCacheState>,

/// The cross-process lock guard over the store.
store: EventCacheStoreLockGuard,
Expand Down
Loading