How to override RwLock.write?

I want to add one line of log on each calling of RwLock.write function, to address a dead lock problem. How can I implement it?

For example:

use std::sync;

pub struct RwLock<T: ?Sized> (pub syn::RwLock);

impl RwLock {

    #[inline]
    pub fn write(&self) -> LockResult<RwLockWriteGuard<T>> {
        println!("write lock");
        self.0.write()
    }

}

Is this the right direction? or any other good method?

I write a small working demo here: https://play.rust-lang.org/?gist=995c4134ea923dec874d5f3177d2aad6&version=stable&mode=debug&edition=2015

and the code:

use std::sync;
use std::sync::RwLockWriteGuard;
use std::sync::LockResult;

pub struct RwLock<T: ?Sized> (pub sync::RwLock<T>);

impl<T> RwLock<T> {
    pub fn new(t: T) -> RwLock<T> {
        RwLock( sync::RwLock::new(t) )
    }
}

impl<T: ?Sized> RwLock<T> {

    #[inline]
    pub fn write(&self) -> LockResult<RwLockWriteGuard<T>> {
        println!("write lock");
        self.0.write()
    }

}

fn main() {
    println!("Hello, world!");

    let lock = RwLock::new(5);
    let mut w = lock.write().unwrap();
    *w += 1;
    assert_eq!(*w, 6);
}

Hope to listen your idea~ thanks.

Another option is the parking lot crate which has an experimental deadlock detector for it’s locks.

1 Like