Module safe_lock

Module safe_lock 

Source
Expand description

We use this mod to avoid deadlocks. Refer to this case: https://stackoverflow.com/q/79621758/9315497 And this case:

fn main() { use std::sync::Mutex; let lock = Mutex::new(“My String”.to_owned()); // let equals = { lock.lock().unwrap().clone() } == { lock.lock().unwrap().clone() }; // No deadlock let equals = lock.lock().unwrap().clone() == lock.lock().unwrap().clone(); // Deadlock println!(“{}”, equals); }

We don’t use this more common macro below, because it may cause deadlocks in Disposable of merge_all.rs.

macro_rules! safe_lock { ($lock_name:expr, $field_name:ident, $method_name:ident) => {{ use $crate::utils::types::MutableHelper; $lock_name.lock_mut().$field_name.$method_name() }}; }

The code like this may cause a deadlock: safe_lock!(self, subscriptions, clear);

We use this approach: Clone::clone(&*$lock_name.lock_ref()) instead of $lock_name.lock_ref().clone() to do the type checking.