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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use crate::inner::OrderedInner;
use std::fmt::Debug;
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll};

/// The Ordered RW Lock will be locking all reads, which starting after write and unlocking them only when write will realize.
/// It may be slow down the reads speed, but decrease time to write on systems, where it is critical.
///
/// **BUT RW Lock has some limitations. You should avoid acquiring the second reading before realizing first inside the one future.
/// Because it can happen that between your readings a write from another thread will acquire the mutex, and you will get a deadlock.**
#[derive(Debug)]
pub struct OrderedRwLock<T: ?Sized> {
    readers: AtomicUsize,
    inner: OrderedInner<T>,
}

impl<T> OrderedRwLock<T> {
    /// Create a new `OrderedRWLock`
    #[inline]
    pub const fn new(data: T) -> OrderedRwLock<T> {
        OrderedRwLock {
            readers: AtomicUsize::new(0),
            inner: OrderedInner::new(data),
        }
    }
}

impl<T: ?Sized> OrderedRwLock<T> {
    /// Acquires the mutex for are write.
    ///
    /// Returns a guard that releases the mutex and wake the next locker when it will be dropped.
    ///
    /// # Examples
    ///
    /// ```
    /// use fast_async_mutex::rwlock_ordered::OrderedRwLock;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let mutex = OrderedRwLock::new(10);
    ///     let mut guard = mutex.write().await;
    ///     *guard += 1;
    ///     assert_eq!(*guard, 11);
    /// }
    /// ```
    #[inline]
    pub fn write(&self) -> OrderedRwLockWriteGuardFuture<T> {
        OrderedRwLockWriteGuardFuture {
            mutex: &self,
            id: self.inner.generate_id(),
            is_realized: false,
        }
    }

    /// Acquires the mutex for are write.
    ///
    /// Returns a guard that releases the mutex and wake the next locker when it will be dropped.
    /// `WriteLockOwnedGuard` have a `'static` lifetime, but requires the `Arc<RWLock<T>>` type
    ///
    /// # Examples
    ///
    /// ```
    /// use fast_async_mutex::rwlock_ordered::OrderedRwLock;
    /// use std::sync::Arc;
    /// #[tokio::main]
    /// async fn main() {
    ///     let mutex = Arc::new(OrderedRwLock::new(10));
    ///     let mut guard = mutex.write_owned().await;
    ///     *guard += 1;
    ///     assert_eq!(*guard, 11);
    /// }
    /// ```
    #[inline]
    pub fn write_owned(self: &Arc<Self>) -> OrderedRwLockWriteOwnedGuardFuture<T> {
        OrderedRwLockWriteOwnedGuardFuture {
            mutex: self.clone(),
            id: self.inner.generate_id(),
            is_realized: false,
        }
    }

    /// Acquires the mutex for are read.
    ///
    /// Returns a guard that releases the mutex and wake the next locker when it will be dropped.
    ///
    /// # Examples
    ///
    /// ```
    /// use fast_async_mutex::rwlock_ordered::OrderedRwLock;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let mutex = OrderedRwLock::new(10);
    ///     let guard = mutex.read().await;
    ///     let guard2 = mutex.read().await;
    ///     assert_eq!(*guard, *guard2);
    /// }
    /// ```
    #[inline]
    pub fn read(&self) -> OrderedRwLockReadGuardFuture<T> {
        OrderedRwLockReadGuardFuture {
            mutex: &self,
            id: self.inner.generate_id(),
            is_realized: false,
        }
    }

    /// Acquires the mutex for are write.
    ///
    /// Returns a guard that releases the mutex and wake the next locker when it will be dropped.
    /// `WriteLockOwnedGuard` have a `'static` lifetime, but requires the `Arc<RWLock<T>>` type
    ///
    /// # Examples
    ///
    /// ```
    /// use fast_async_mutex::rwlock_ordered::OrderedRwLock;
    /// use std::sync::Arc;
    /// #[tokio::main]
    /// async fn main() {
    ///     let mutex = Arc::new(OrderedRwLock::new(10));
    ///     let guard = mutex.read().await;
    ///     let guard2 = mutex.read().await;
    ///     assert_eq!(*guard, *guard2);
    /// }
    /// ```
    #[inline]
    pub fn read_owned(self: &Arc<Self>) -> OrderedRwLockReadOwnedGuardFuture<T> {
        OrderedRwLockReadOwnedGuardFuture {
            mutex: self.clone(),
            id: self.inner.generate_id(),
            is_realized: false,
        }
    }

    #[inline]
    fn unlock_reader(&self) {
        self.readers.fetch_sub(1, Ordering::Release);
        self.inner.unlock()
    }

    #[inline]
    fn add_reader(&self) {
        self.readers.fetch_add(1, Ordering::Release);
    }

    #[inline]
    pub fn try_acquire_reader(&self, id: usize) -> bool {
        id == self.inner.current.load(Ordering::Acquire) + self.readers.load(Ordering::Acquire)
    }
}

/// The Simple Write Lock Guard
/// As long as you have this guard, you have exclusive access to the underlying `T`. The guard internally borrows the RWLock, so the mutex will not be dropped while a guard exists.
/// The lock is automatically released and waked the next locker whenever the guard is dropped, at which point lock will succeed yet again.
#[derive(Debug)]
pub struct OrderedRwLockWriteGuard<'a, T: ?Sized> {
    mutex: &'a OrderedRwLock<T>,
}

#[derive(Debug)]
pub struct OrderedRwLockWriteGuardFuture<'a, T: ?Sized> {
    mutex: &'a OrderedRwLock<T>,
    id: usize,
    is_realized: bool,
}

/// An owned handle to a held RWLock.
/// This guard is only available from a RWLock that is wrapped in an `Arc`. It is identical to `WriteLockGuard`, except that rather than borrowing the `RWLock`, it clones the `Arc`, incrementing the reference count. This means that unlike `WriteLockGuard`, it will have the `'static` lifetime.
/// As long as you have this guard, you have exclusive access to the underlying `T`. The guard internally keeps a reference-couned pointer to the original `RWLock`, so even if the lock goes away, the guard remains valid.
/// The lock is automatically released and waked the next locker whenever the guard is dropped, at which point lock will succeed yet again.
#[derive(Debug)]
pub struct OrderedRwLockWriteOwnedGuard<T: ?Sized> {
    mutex: Arc<OrderedRwLock<T>>,
}

#[derive(Debug)]
pub struct OrderedRwLockWriteOwnedGuardFuture<T: ?Sized> {
    mutex: Arc<OrderedRwLock<T>>,
    id: usize,
    is_realized: bool,
}

/// The Simple Write Lock Guard
/// As long as you have this guard, you have shared access to the underlying `T`. The guard internally borrows the `RWLock`, so the mutex will not be dropped while a guard exists.
/// The lock is automatically released and waked the next locker whenever the guard is dropped, at which point lock will succeed yet again.
#[derive(Debug)]
pub struct OrderedRwLockReadGuard<'a, T: ?Sized> {
    mutex: &'a OrderedRwLock<T>,
}

#[derive(Debug)]
pub struct OrderedRwLockReadGuardFuture<'a, T: ?Sized> {
    mutex: &'a OrderedRwLock<T>,
    id: usize,
    is_realized: bool,
}

/// An owned handle to a held RWLock.
/// This guard is only available from a RWLock that is wrapped in an `Arc`. It is identical to `WriteLockGuard`, except that rather than borrowing the `RWLock`, it clones the `Arc`, incrementing the reference count. This means that unlike `WriteLockGuard`, it will have the `'static` lifetime.
/// As long as you have this guard, you have shared access to the underlying `T`. The guard internally keeps a reference-couned pointer to the original `RWLock`, so even if the lock goes away, the guard remains valid.
/// The lock is automatically released and waked the next locker whenever the guard is dropped, at which point lock will succeed yet again.
#[derive(Debug)]
pub struct OrderedRwLockReadOwnedGuard<T: ?Sized> {
    mutex: Arc<OrderedRwLock<T>>,
}

#[derive(Debug)]
pub struct OrderedRwLockReadOwnedGuardFuture<T: ?Sized> {
    mutex: Arc<OrderedRwLock<T>>,
    id: usize,
    is_realized: bool,
}

impl<'a, T: ?Sized> Future for OrderedRwLockWriteGuardFuture<'a, T> {
    type Output = OrderedRwLockWriteGuard<'a, T>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        if self.mutex.inner.try_acquire(self.id) {
            self.is_realized = true;
            Poll::Ready(OrderedRwLockWriteGuard { mutex: self.mutex })
        } else {
            self.mutex.inner.store_waker(cx.waker());
            Poll::Pending
        }
    }
}

impl<T: ?Sized> Future for OrderedRwLockWriteOwnedGuardFuture<T> {
    type Output = OrderedRwLockWriteOwnedGuard<T>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        if self.mutex.inner.try_acquire(self.id) {
            self.is_realized = true;
            Poll::Ready(OrderedRwLockWriteOwnedGuard {
                mutex: self.mutex.clone(),
            })
        } else {
            self.mutex.inner.store_waker(cx.waker());
            Poll::Pending
        }
    }
}

impl<'a, T: ?Sized> Future for OrderedRwLockReadGuardFuture<'a, T> {
    type Output = OrderedRwLockReadGuard<'a, T>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        if self.mutex.try_acquire_reader(self.id) {
            self.is_realized = true;
            self.mutex.add_reader();
            Poll::Ready(OrderedRwLockReadGuard { mutex: &self.mutex })
        } else {
            self.mutex.inner.store_waker(cx.waker());
            Poll::Pending
        }
    }
}

impl<T: ?Sized> Future for OrderedRwLockReadOwnedGuardFuture<T> {
    type Output = OrderedRwLockReadOwnedGuard<T>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        if self.mutex.try_acquire_reader(self.id) {
            self.is_realized = true;
            self.mutex.add_reader();
            Poll::Ready(OrderedRwLockReadOwnedGuard {
                mutex: self.mutex.clone(),
            })
        } else {
            self.mutex.inner.store_waker(cx.waker());
            Poll::Pending
        }
    }
}

crate::impl_send_sync_rwlock!(
    OrderedRwLock,
    OrderedRwLockReadGuard,
    OrderedRwLockReadOwnedGuard,
    OrderedRwLockWriteGuard,
    OrderedRwLockWriteOwnedGuard
);

crate::impl_deref_mut!(OrderedRwLockWriteGuard, 'a);
crate::impl_deref_mut!(OrderedRwLockWriteOwnedGuard);
crate::impl_deref!(OrderedRwLockReadGuard, 'a);
crate::impl_deref!(OrderedRwLockReadOwnedGuard);

crate::impl_drop_guard!(OrderedRwLockWriteGuard, 'a, unlock);
crate::impl_drop_guard!(OrderedRwLockWriteOwnedGuard, unlock);
crate::impl_drop_guard_self!(OrderedRwLockReadGuard, 'a, unlock_reader);
crate::impl_drop_guard_self!(OrderedRwLockReadOwnedGuard, unlock_reader);

crate::impl_drop_guard_future!(OrderedRwLockWriteGuardFuture, 'a, unlock);
crate::impl_drop_guard_future!(OrderedRwLockWriteOwnedGuardFuture, unlock);
crate::impl_drop_guard_future!(OrderedRwLockReadGuardFuture, 'a, unlock);
crate::impl_drop_guard_future!(OrderedRwLockReadOwnedGuardFuture, unlock);

#[cfg(test)]
mod tests {
    use crate::rwlock_ordered::{
        OrderedRwLock, OrderedRwLockReadGuard, OrderedRwLockWriteGuard,
        OrderedRwLockWriteOwnedGuard,
    };
    use futures::executor::block_on;
    use futures::{FutureExt, StreamExt, TryStreamExt};
    use std::ops::AddAssign;
    use std::sync::atomic::AtomicUsize;
    use std::sync::Arc;
    use tokio::time::{sleep, Duration};

    #[tokio::test(flavor = "multi_thread", worker_threads = 12)]
    async fn test_mutex() {
        let c = OrderedRwLock::new(0);

        futures::stream::iter(0..10000)
            .for_each_concurrent(None, |_| async {
                let mut co: OrderedRwLockWriteGuard<i32> = c.write().await;
                *co += 1;
            })
            .await;

        let co = c.write().await;
        assert_eq!(*co, 10000)
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 12)]
    async fn test_mutex_delay() {
        let expected_result = 100;
        let c = OrderedRwLock::new(0);

        futures::stream::iter(0..expected_result)
            .then(|i| c.write().map(move |co| (i, co)))
            .for_each_concurrent(None, |(i, mut co)| async move {
                sleep(Duration::from_millis(expected_result - i)).await;
                *co += 1;
            })
            .await;

        let co = c.write().await;
        assert_eq!(*co, expected_result)
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 12)]
    async fn test_owned_mutex() {
        let c = Arc::new(OrderedRwLock::new(0));

        futures::stream::iter(0..10000)
            .for_each_concurrent(None, |_| async {
                let mut co: OrderedRwLockWriteOwnedGuard<i32> = c.write_owned().await;
                *co += 1;
            })
            .await;

        let co = c.write_owned().await;
        assert_eq!(*co, 10000)
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 12)]
    async fn test_container() {
        let c = OrderedRwLock::new(String::from("lol"));

        let mut co: OrderedRwLockWriteGuard<String> = c.write().await;
        co.add_assign("lol");

        assert_eq!(*co, "lollol");
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 12)]
    async fn test_overflow() {
        let mut c = OrderedRwLock::new(String::from("lol"));

        c.inner.state = AtomicUsize::new(usize::max_value());
        c.inner.current = AtomicUsize::new(usize::max_value());

        let mut co: OrderedRwLockWriteGuard<String> = c.write().await;
        co.add_assign("lol");

        assert_eq!(*co, "lollol");
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 12)]
    async fn test_timeout() {
        let c = OrderedRwLock::new(String::from("lol"));

        let co: OrderedRwLockWriteGuard<String> = c.write().await;

        futures::stream::iter(0..10000i32)
            .then(|_| tokio::time::timeout(Duration::from_nanos(1), c.write()))
            .try_for_each_concurrent(None, |_c| futures::future::ok(()))
            .await
            .expect_err("timout must be");

        drop(co);

        let mut co: OrderedRwLockWriteGuard<String> = c.write().await;
        co.add_assign("lol");

        assert_eq!(*co, "lollol");
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 12)]
    async fn test_concurrent_reading() {
        let c = OrderedRwLock::new(String::from("lol"));

        let co: OrderedRwLockReadGuard<String> = c.read().await;

        futures::stream::iter(0..10000i32)
            .then(|_| c.read())
            .inspect(|c| assert_eq!(*co, **c))
            .for_each_concurrent(None, |_c| futures::future::ready(()))
            .await;

        assert!(matches!(
            tokio::time::timeout(Duration::from_millis(1), c.write()).await,
            Err(_)
        ));

        let co2: OrderedRwLockReadGuard<String> = c.read().await;
        assert_eq!(*co, *co2);
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 12)]
    async fn test_concurrent_reading_writing() {
        let c = OrderedRwLock::new(String::from("lol"));

        let co: OrderedRwLockReadGuard<String> = c.read().await;
        let co2: OrderedRwLockReadGuard<String> = c.read().await;
        assert_eq!(*co, *co2);

        drop(co);
        drop(co2);

        let mut co: OrderedRwLockWriteGuard<String> = c.write().await;

        assert!(matches!(
            tokio::time::timeout(Duration::from_millis(1), c.read()).await,
            Err(_)
        ));

        *co += "lol";

        drop(co);

        let co: OrderedRwLockReadGuard<String> = c.read().await;
        let co2: OrderedRwLockReadGuard<String> = c.read().await;
        assert_eq!(*co, "lollol");
        assert_eq!(*co, *co2);
    }

    #[test]
    fn multithreading_test() {
        let num = 100;
        let mutex = Arc::new(OrderedRwLock::new(0));
        let ths: Vec<_> = (0..num)
            .map(|i| {
                let mutex = mutex.clone();
                std::thread::spawn(move || {
                    block_on(async {
                        if i % 2 == 0 {
                            let mut lock = mutex.write().await;
                            *lock += 1;
                            drop(lock);
                        } else {
                            let _lock = mutex.read().await;
                        }
                    })
                })
            })
            .collect();

        for thread in ths {
            thread.join().unwrap();
        }

        block_on(async {
            let lock = mutex.read().await;
            assert_eq!(num / 2, *lock)
        })
    }
}