2020-01-17 14:27:28 +01:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
2019-01-07 11:33:07 +01:00
|
|
|
// This file is part of Parity Ethereum.
|
2018-12-04 15:30:52 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2018-12-04 15:30:52 +01:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2018-12-04 15:30:52 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2018-12-04 15:30:52 +01:00
|
|
|
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
use std::sync::atomic::AtomicUsize;
|
|
|
|
use std::sync::atomic::Ordering;
|
|
|
|
|
|
|
|
use parking_lot::{Mutex, MutexGuard};
|
|
|
|
|
2018-12-13 19:07:56 +01:00
|
|
|
use Len;
|
2018-12-04 15:30:52 +01:00
|
|
|
|
2018-12-13 19:07:56 +01:00
|
|
|
/// Can be used in place of a [`Mutex`](../../lock_api/struct.Mutex.html) where reading `T`'s `len()` without
|
|
|
|
/// needing to lock, is advantageous.
|
|
|
|
/// When the Guard is released, `T`'s `len()` will be cached.
|
|
|
|
/// The cached `len()` may be at most 1 lock behind current state.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct LenCachingMutex<T: ?Sized> {
|
|
|
|
len: AtomicUsize,
|
|
|
|
data: Mutex<T>,
|
2018-12-04 15:30:52 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 19:07:56 +01:00
|
|
|
impl<T: Len + Default> Default for LenCachingMutex<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
LenCachingMutex::new(T::default())
|
|
|
|
}
|
2018-12-04 15:30:52 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 19:07:56 +01:00
|
|
|
impl<T: Len> From<T> for LenCachingMutex<T> {
|
|
|
|
fn from(data: T) -> Self {
|
|
|
|
LenCachingMutex::new(data)
|
|
|
|
}
|
2018-12-04 15:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Len> LenCachingMutex<T> {
|
2018-12-13 19:07:56 +01:00
|
|
|
/// Constructs a new LenCachingMutex
|
|
|
|
pub fn new(data: T) -> Self {
|
2018-12-04 15:30:52 +01:00
|
|
|
LenCachingMutex {
|
|
|
|
len: AtomicUsize::new(data.len()),
|
|
|
|
data: Mutex::new(data),
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 19:07:56 +01:00
|
|
|
}
|
2018-12-04 15:30:52 +01:00
|
|
|
|
2018-12-13 19:07:56 +01:00
|
|
|
impl<T: Len + ?Sized> LenCachingMutex<T> {
|
|
|
|
/// Load the cached value that was returned from your `T`'s `len()`
|
|
|
|
/// subsequent to the most recent lock being released.
|
2018-12-04 15:30:52 +01:00
|
|
|
pub fn load_len(&self) -> usize {
|
|
|
|
self.len.load(Ordering::SeqCst)
|
|
|
|
}
|
|
|
|
|
2018-12-13 19:07:56 +01:00
|
|
|
/// Delegates to `parking_lot::Mutex`
|
|
|
|
/// [`lock()`](../../lock_api/struct.Mutex.html#method.lock).
|
|
|
|
pub fn lock(&self) -> CachingMutexGuard<T> {
|
|
|
|
CachingMutexGuard {
|
2018-12-04 15:30:52 +01:00
|
|
|
mutex_guard: self.data.lock(),
|
|
|
|
len: &self.len,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 19:07:56 +01:00
|
|
|
/// Delegates to `parking_lot::Mutex`
|
|
|
|
/// [`try_lock()`](../../lock_api/struct.Mutex.html#method.try_lock).
|
|
|
|
pub fn try_lock(&self) -> Option<CachingMutexGuard<T>> {
|
|
|
|
Some(CachingMutexGuard {
|
2018-12-04 15:30:52 +01:00
|
|
|
mutex_guard: self.data.try_lock()?,
|
|
|
|
len: &self.len,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 19:07:56 +01:00
|
|
|
/// Guard comprising `MutexGuard` and `AtomicUsize` for cache
|
|
|
|
pub struct CachingMutexGuard<'a, T: Len + 'a + ?Sized> {
|
2018-12-04 15:30:52 +01:00
|
|
|
mutex_guard: MutexGuard<'a, T>,
|
|
|
|
len: &'a AtomicUsize,
|
|
|
|
}
|
|
|
|
|
2018-12-13 19:07:56 +01:00
|
|
|
impl<'a, T: Len + ?Sized> CachingMutexGuard<'a, T> {
|
|
|
|
/// Returns a mutable reference to the contained
|
|
|
|
/// [`MutexGuard`](../../parking_lot/mutex/type.MutexGuard.html)
|
2018-12-04 15:30:52 +01:00
|
|
|
pub fn inner_mut(&mut self) -> &mut MutexGuard<'a, T> {
|
|
|
|
&mut self.mutex_guard
|
|
|
|
}
|
|
|
|
|
2018-12-13 19:07:56 +01:00
|
|
|
/// Returns a non-mutable reference to the contained
|
|
|
|
/// [`MutexGuard`](../../parking_lot/mutex/type.MutexGuard.html)
|
2018-12-04 15:30:52 +01:00
|
|
|
pub fn inner(&self) -> &MutexGuard<'a, T> {
|
|
|
|
&self.mutex_guard
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 19:07:56 +01:00
|
|
|
impl<'a, T: Len + ?Sized> Drop for CachingMutexGuard<'a, T> {
|
2018-12-04 15:30:52 +01:00
|
|
|
fn drop(&mut self) {
|
|
|
|
self.len.store(self.mutex_guard.len(), Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 19:07:56 +01:00
|
|
|
impl<'a, T: Len + ?Sized> Deref for CachingMutexGuard<'a, T> {
|
2018-12-04 15:30:52 +01:00
|
|
|
type Target = T;
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
self.mutex_guard.deref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 19:07:56 +01:00
|
|
|
impl<'a, T: Len + ?Sized> DerefMut for CachingMutexGuard<'a, T> {
|
2018-12-04 15:30:52 +01:00
|
|
|
fn deref_mut(&mut self) -> &mut T {
|
|
|
|
self.mutex_guard.deref_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn caches_len() {
|
|
|
|
let v = vec![1,2,3];
|
|
|
|
let lcm = LenCachingMutex::new(v);
|
|
|
|
assert_eq!(lcm.load_len(), 3);
|
|
|
|
lcm.lock().push(4);
|
|
|
|
assert_eq!(lcm.load_len(), 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn works_with_vec() {
|
|
|
|
let v: Vec<i32> = Vec::new();
|
|
|
|
let lcm = LenCachingMutex::new(v);
|
|
|
|
assert!(lcm.lock().is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn works_with_vecdeque() {
|
|
|
|
let v: VecDeque<i32> = VecDeque::new();
|
|
|
|
let lcm = LenCachingMutex::new(v);
|
|
|
|
lcm.lock().push_front(4);
|
|
|
|
assert_eq!(lcm.load_len(), 1);
|
|
|
|
}
|
|
|
|
}
|