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
use std::ops::Deref;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::SeqCst;
use std::sync::{Arc};

use parking_lot::{Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
//use tokio::sync::{Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::future::Future;
use core::pin::Pin;
use crate::Data;

pub struct MaybeSingleAsync<T: 'static> {
    data: Arc<RwLock<Option<Arc<T>>>>,
    lock_mutex: Arc<RwLock<()>>,
    init: fn() -> Pin<Box<dyn Future<Output = T>>>,
    callers: Arc<Mutex<AtomicUsize>>,
}

impl<T: 'static> MaybeSingleAsync<T> {

    pub fn new(init: fn() -> Pin<Box<dyn Future<Output = T>>>) -> Self {
        MaybeSingleAsync {
            data: Arc::new(RwLock::new(None)),
            init,
            lock_mutex: Arc::new(RwLock::new(())),
            callers: Arc::new(Mutex::new(AtomicUsize::new(0))),
        }
    }

    pub async fn data<'a>(&'a self, serial: bool) -> Data<'a, T> {
        {
            let lock = self.callers.lock();
            let callers = lock.load(SeqCst) + 1;
            lock.store(callers, SeqCst);
        }

        let data_arc = {
            let mut lock = self.data.read();

            lock = if lock.is_none() {
                drop(lock);
                {
                    let mut write_lock = self.data.write();

                    if write_lock.is_none() {
                        //  println!("--- INIT ---");
                        let init = {
                            (self.init)().await
                        };

                        *write_lock = Some(Arc::new(init));
                    }

                }
                self.data.read()
            } else {
                lock
            };
            //println!("---- Exec {}", rnd);
            match lock.as_ref() {
                Some(data) => data.clone(),
                None => panic!("There should always be data here!"),
            }
        };

        let (read_lock, write_lock) = if serial {
            (None, Some(self.lock_mutex.write()))
        } else {
            (Some(self.lock_mutex.read()), None)
        };

        Data {
            data_arc,
            data: self.data.clone(),
            callers: self.callers.clone(),
            read_lock,
            write_lock,
        }

    }
}

#[cfg(test)]
mod test {

    use super::*;
    use rand::{thread_rng, Rng};
    use std::thread::sleep;
    use std::time::Duration;
    use tokio::time::Instant;

    #[test]
    fn maybe_should_be_send() {
        let maybe = MaybeSingleAsync::new(|| Box::pin(async {}));
        need_send(maybe);
    }

    fn need_send<T: Send>(_t: T) {}
    fn need_sync<T: Sync>(_t: T) {}

    #[test]
    fn maybe_should_be_sync() {
        let maybe = MaybeSingleAsync::new(|| Box::pin(async {}));
        need_sync(maybe);
    }

    /*
    #[tokio::test]
    async fn should_execute_in_parallel() {
        let maybe = MaybeSingleAsync::new(|| Box::pin(async {
        }));
        let maybe = Arc::new(maybe);
        let _data = maybe.data(false).await;

        let maybe_clone = maybe.clone();
        tokio::spawn(async move {
        //    let _data = maybe_clone.data(false).await;
        });

        let mut handles = vec![];

        for i in 0..100 {
            let maybe = maybe.clone();
            handles.push(tokio::spawn( async move {
                let _data = maybe.data(true).await;
                println!(" exec {} start", i);
                tokio::time::delay_until(Instant::now() + Duration::from_nanos(thread_rng().gen_range(0, 1000))).await;
                println!(" exec {} end", i);
            }));

        }

        for handle in handles {
            let _ = handle.await.unwrap(); // maybe consider handling errors propagated from the thread here
        }
    }

    #[tokio::test]
    async fn should_execute_serially() {
        let maybe = MaybeSingleAsync::new(|| Box::pin(async {
        }));
        let maybe = Arc::new(maybe);
        let _data = maybe.data(false).await;

        let maybe_clone = maybe.clone();
        tokio::spawn(async move {
            //    let _data = maybe_clone.data(false).await;
        });

        let mut handles = vec![];

        for i in 0..100 {
            let maybe = maybe.clone();
            handles.push(tokio::spawn( async move {
                let _data = maybe.data(true).await;
                println!(" exec {} start", i);
                tokio::time::delay_until(Instant::now() + Duration::from_nanos(thread_rng().gen_range(0, 1000))).await;
                println!(" exec {} end", i);
            }));

        }

        for handle in handles {
            let _ = handle.await.unwrap(); // maybe consider handling errors propagated from the thread here
        }
    }
    */

}