Skip to content

Commit

Permalink
add async
Browse files Browse the repository at this point in the history
  • Loading branch information
codabrink committed Dec 13, 2024
1 parent ba1c2cd commit 15fdc75
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
4 changes: 2 additions & 2 deletions xmtp_mls/src/groups/mls_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ where
id: ref msg_id,
..
} = *envelope;
let mut locked_openmls_group = openmls_group.lock();
let mut locked_openmls_group = openmls_group.lock().await;

if intent.state == IntentState::Committed {
return Ok(IntentState::Committed);
Expand Down Expand Up @@ -501,7 +501,7 @@ where
id: ref msg_id,
..
} = *envelope;
let mut locked_openmls_group = openmls_group.lock();
let mut locked_openmls_group = openmls_group.lock().await;

let decrypted_message = locked_openmls_group.process_message(provider, message)?;
let (sender_inbox_id, sender_installation_id) = extract_message_sender(
Expand Down
27 changes: 25 additions & 2 deletions xmtp_mls/src/groups/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,34 @@ impl<'a> DerefMut for SerialOpenMlsGroup<'a> {
}

pub(crate) trait OpenMlsLock {
fn lock<'a>(&'a mut self) -> SerialOpenMlsGroup<'a>;
fn lock_blocking<'a>(&'a mut self) -> SerialOpenMlsGroup<'a>;
async fn lock<'a>(&'a mut self) -> SerialOpenMlsGroup<'a>;
}

impl OpenMlsLock for OpenMlsGroup {
fn lock<'a>(&'a mut self) -> SerialOpenMlsGroup<'a> {
async fn lock<'a>(&'a mut self) -> SerialOpenMlsGroup<'a> {
// .clone() is important here so that the outer lock gets dropped
let mutex = MLS_COMMIT_LOCK
.lock()
.entry(self.group_id().to_vec())
.or_default()
.clone();

// this may block
let lock = mutex.lock().await;
let lock = unsafe {
// let the borrow checker know that this guard's mutex is going to be owned by the struct it's returning
std::mem::transmute::<MutexGuard<'_, ()>, MutexGuard<'a, ()>>(lock)
};

SerialOpenMlsGroup {
group: self,
lock,
_mutex: mutex,
}
}

fn lock_blocking<'a>(&'a mut self) -> SerialOpenMlsGroup<'a> {
// .clone() is important here so that the outer lock gets dropped
let mutex = MLS_COMMIT_LOCK
.lock()
Expand Down

0 comments on commit 15fdc75

Please sign in to comment.