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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use std::convert::AsRef;
use std::marker::PhantomData;

use tokio::prelude::{Async, future::{Future, IntoFuture}, task};

use parking_lot::{Mutex, MutexGuard};

/// Wrapper to use Mutex in Future-style
pub struct FutureLock<'a, R, T, F, I>
where
    R: AsRef<Mutex<T>>,
    F: FnOnce(MutexGuard<'_, T>) -> I,
    I: IntoFuture,
{
    lock: &'a R,
    inner: Option<F>,
    _contents: PhantomData<T>,
    future: Option<I::Future>,
}

impl<'a, R, T, F, I> FutureLock<'a, R, T, F, I>
where
    R: AsRef<Mutex<T>>,
    F: FnOnce(MutexGuard<'_, T>) -> I,
    I: IntoFuture,
{
    fn new(lock: &'a R, f: F) -> Self {
        FutureLock {
            lock,
            inner: Some(f),
            _contents: PhantomData,
            future: None,
        }
    }
}

impl<'a, R, T, F, I> Future for FutureLock<'a, R, T, F, I>
where
    R: AsRef<Mutex<T>>,
    F: FnOnce(MutexGuard<'_, T>) -> I,
    I: IntoFuture,
{
    type Item = <<I as IntoFuture>::Future as Future>::Item;
    type Error = <<I as IntoFuture>::Future as Future>::Error;

    fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error> {
        if let Some(ref mut future) = self.future {
            // Use cached future
            return future.poll();
        }

        match self.lock.as_ref().try_lock() {
            Some(read_lock) => {
                // Cache resulting future to avoid executing the inner function again
                let mut future = (self.inner.take().expect("Can't poll on FutureLock more than once"))(read_lock).into_future();
                let res = future.poll();
                self.future = Some(future);
                res
            },
            None => {
                // Notify current Task we can be polled again
                task::current().notify();
                Ok(Async::NotReady)
            },
        }
    }
}

/// Trait to permit FutureLock implementation on wrapped Mutex (not Mutex itself)
pub trait FutureLockable<R: AsRef<Mutex<T>>, T, I: IntoFuture> {
    /// Takes a closure that will be executed when the Futures gains the read-lock
    fn future_lock<F: FnOnce(MutexGuard<'_, T>) -> I>(&self, func: F) -> FutureLock<R, T, F, I>;
}

impl<R: AsRef<Mutex<T>>, T, I: IntoFuture> FutureLockable<R, T, I> for R {
    fn future_lock<F: FnOnce(MutexGuard<'_, T>) -> I>(&self, func: F) -> FutureLock<R, T, F, I> {
        FutureLock::new(self, func)
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::rc::Rc;

    use tokio::runtime::current_thread;
    use tokio::prelude::future::lazy;

    use parking_lot::Mutex;

    use super::{FutureLockable};

    use lazy_static::lazy_static;

    lazy_static! {
        static ref LOCK1: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
        static ref LOCK2: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
        static ref CONCURRENT_LOCK: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
    }

    #[test]
    fn current_thread_lazy_static() {
        current_thread::block_on_all(LOCK1.future_lock(|mut v| -> Result<(), ()> {
            v.push(String::from("It works!"));
            assert!(v.len() == 1 && v[0] == "It works!");
            Ok(())
        })).unwrap();
    }

    #[test]
    fn current_thread_local_arc() {
        let lock = Arc::new(Mutex::new(Vec::new()));
        current_thread::block_on_all(lock.future_lock(|mut v| -> Result<(), ()> {
            v.push(String::from("It works!"));
            assert!(v.len() == 1 && v[0] == "It works!");
            Ok(())
        })).unwrap();
    }

    #[test]
    fn current_thread_local_rc() {
        let lock = Rc::new(Mutex::new(Vec::new()));
        current_thread::block_on_all(lock.future_lock(|mut v| -> Result<(), ()> {
            v.push(String::from("It works!"));
            assert!(v.len() == 1 && v[0] == "It works!");
            Ok(())
        })).unwrap();
    }

    #[test]
    fn current_thread_local_box() {
        let lock = Box::new(Mutex::new(Vec::new()));
        current_thread::block_on_all(lock.future_lock(|mut v| -> Result<(), ()> {
            v.push(String::from("It works!"));
            assert!(v.len() == 1 && v[0] == "It works!");
            Ok(())
        })).unwrap();
    }

    #[test]
    fn multithread_lazy_static() {
        tokio::run(LOCK2.future_lock(|mut v| -> Result<(), ()> {
            v.push(String::from("It works!"));
            assert!(v.len() == 1 && v[0] == "It works!");
            Ok(())
        }));
    }

    // Implies a lifetime problem
    // #[test]
    // fn multithread_local_arc() {
    //     let lock = Arc::new(Mutex::new(Vec::new()));
    //     tokio::run(lock.future_lock(|mut v| {
    //         v.push(String::from("It works!"));
    //         assert!(v.len() == 1 && v[0] == "It works!");
    //         Ok(())
    //     });
    // }

    // Can't be done because Rc isn't Sync
    // #[test]
    // fn multithread_local_rc() {
    //     let lock = Rc::new(Mutex::new(Vec::new()));
    //     tokio::run(lock.future_lock(|mut v| {
    //         v.push(String::from("It works!"));
    //         assert!(v.len() == 1 && v[0] == "It works!");
    //         Ok(())
    //     });
    // }

    // Implies a lifetime problem
    // #[test]
    // fn multithread_local_box() {
    //     let lock = Box::new(Mutex::new(Vec::new()));
    //     tokio::run(lock.future_lock(|mut v| {
    //         v.push(String::from("It works!"));
    //         assert!(v.len() == 1 && v[0] == "It works!");
    //         Ok(())
    //     });
    // }

    #[test]
    fn multithread_concurrent_lazy_static() {
        tokio::run(lazy(|| {
            // spawn 10 concurrent futures
            for i in 0..100 {
                tokio::spawn(CONCURRENT_LOCK.future_lock(move |mut v| {
                    v.push(format!("{}", i));
                    println!("{:?}", v);
                    Ok(())
                }));
            }
            Ok(())
        }));
        let singleton = CONCURRENT_LOCK.lock();
        assert_eq!(singleton.len(), 100);
    }
}