Import upstream v0.16.22, stripped
Upstream commit: 474dd0229cb20cf513036619781ed97bd8073c3f Enterprise-only files removed or emptied: 63 Enterprise-only snippets removed: 117 in 50 files Dangling module declarations removed: 5 Cargo edits turning enterprise off: 14 Verification: clean Enterprise feature gates left for rebuilt features: 19 in 18 files Produced by tools/fork/strip.py. The full report is in docs/fork/strip-reports/ on main.
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
|
||||
*/
|
||||
|
||||
use core::hash::Hash;
|
||||
use std::hash::Hasher;
|
||||
|
||||
use ahash::AHasher;
|
||||
use tokio::sync::{Mutex, MutexGuard};
|
||||
|
||||
pub struct MutexMap<T: Default> {
|
||||
map: Box<[Mutex<T>]>,
|
||||
mask: u64,
|
||||
hasher: AHasher,
|
||||
}
|
||||
|
||||
pub struct MutexMapLockError;
|
||||
pub type Result<T> = std::result::Result<T, MutexMapLockError>;
|
||||
|
||||
#[allow(clippy::mutex_atomic)]
|
||||
impl<T: Default> MutexMap<T> {
|
||||
pub fn with_capacity(size: usize) -> MutexMap<T> {
|
||||
let size = size.next_power_of_two();
|
||||
MutexMap {
|
||||
map: (0..size)
|
||||
.map(|_| T::default().into())
|
||||
.collect::<Vec<Mutex<T>>>()
|
||||
.into_boxed_slice(),
|
||||
mask: (size - 1) as u64,
|
||||
hasher: AHasher::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn lock<U>(&self, key: U) -> MutexGuard<'_, T>
|
||||
where
|
||||
U: Into<u64> + Copy,
|
||||
{
|
||||
let hash = key.into() & self.mask;
|
||||
self.map[hash as usize].lock().await
|
||||
}
|
||||
|
||||
/*pub async fn try_lock<U>(&self, key: U, timeout: Duration) -> Option<MutexGuard<'_, T>>
|
||||
where
|
||||
U: Into<u64> + Copy,
|
||||
{
|
||||
let hash = key.into() & self.mask;
|
||||
self.map[hash as usize].try_lock(timeout).await
|
||||
}*/
|
||||
|
||||
pub async fn lock_hash<U>(&self, key: U) -> MutexGuard<'_, T>
|
||||
where
|
||||
U: Hash,
|
||||
{
|
||||
let mut hasher = self.hasher.clone();
|
||||
key.hash(&mut hasher);
|
||||
let hash = hasher.finish() & self.mask;
|
||||
self.map[hash as usize].lock().await
|
||||
}
|
||||
|
||||
/*pub async fn try_lock_hash<U>(&self, key: U, timeout: Duration) -> Option<MutexGuard<'_, T>>
|
||||
where
|
||||
U: Hash,
|
||||
{
|
||||
let mut hasher = self.hasher.clone();
|
||||
key.hash(&mut hasher);
|
||||
let hash = hasher.finish() & self.mask;
|
||||
self.map[hash as usize].try_lock_for(timeout).await
|
||||
}*/
|
||||
}
|
||||
Reference in New Issue
Block a user