1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::Arc;
use tokio::sync::{Mutex, RwLock};

#[derive(Clone)]
pub struct AsyncLocker<K> {
    mutexes: Arc<RwLock<HashMap<K, Arc<Mutex<()>>>>>,
}

impl<K: Eq + Hash> AsyncLocker<K> {
    pub fn new() -> Self {
        AsyncLocker {
            mutexes: Arc::new(RwLock::new(HashMap::<K, Arc<Mutex<()>>>::new())),
        }
    }

    /// Return reference to existig `Mutex` or insert new one.
    ///
    /// Locks the current task until it is able to return `Mutex`.
    ///
    /// # Examples
    /// ```ignore
    /// use std::time::Duration;
    /// use locker::AsyncLocker;
    /// use tokio::time::delay_for;
    ///
    /// let locker = Locker::new();
    /// let mutex = locker.get_mutex(1).await;
    /// let _guard = mutex.lock().await; // lock
    /// let locker_clone = locker.clone();
    /// tokio::spawn(async move {
    ///     let mutex = locker.get_mutex(1).await;
    ///     let _guard = mutex.lock().await; // wait
    /// });
    /// delay_for(Duration::from_millis(200)).await;
    /// ```
    pub async fn get_mutex(&self, key: K) -> Arc<Mutex<()>> {
        {
            let mutexes = self.mutexes.read().await;
            let mutex_opt = mutexes.get(&key);
            if let Some(mutex) = mutex_opt {
                return mutex.clone();
            };
        }
        let mut mutexes = self.mutexes.write().await;
        let new_mutex = Arc::new(Mutex::new(()));
        mutexes.entry(key).or_insert(new_mutex).clone()
    }
}

#[cfg(test)]
mod tests {
    use super::AsyncLocker;
    use std::time::Duration;
    use tokio::time::delay_for;

    #[tokio::test]
    async fn test_async_locker() {
        let locker = AsyncLocker::new();
        let locker_clone = locker.clone();

        let handle = tokio::spawn(async move {
            let mutex = locker_clone.get_mutex(1).await;
            loop {
                println!("task mutex try to lock");
                match mutex.try_lock() {
                    Ok(_) => {
                        println!("task mutex locked");
                        delay_for(Duration::from_millis(100)).await;
                        println!("task mutex unlocked");
                        break;
                    }
                    Err(_) => {
                        println!("task mutex wait unlock");
                        delay_for(Duration::from_millis(100)).await;
                        continue;
                    }
                }
            }
        });

        let mutex = locker.get_mutex(1).await;
        loop {
            println!("main mutex try to lock");
            match mutex.try_lock() {
                Ok(_) => {
                    println!("main mutex locked");
                    delay_for(Duration::from_millis(100)).await;
                    println!("main mutex unlocked");
                    break;
                }
                Err(_) => {
                    println!("main mutex wait for unlock");
                    delay_for(Duration::from_millis(100)).await;
                    continue;
                }
            }
        }

        handle.await.unwrap();
    }
}