-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathaccelerate.rs
More file actions
64 lines (50 loc) · 1.99 KB
/
accelerate.rs
File metadata and controls
64 lines (50 loc) · 1.99 KB
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
use std::sync::atomic::{AtomicUsize, Ordering};
use parking_lot::{MappedRwLockReadGuard, Mutex, RwLock, RwLockReadGuard};
use crate::passthroughhasher::PassthroughHashMap;
/// The global list of currently alive accelerators.
static ACCELERATORS: RwLock<(usize, Vec<Accelerator>)> = RwLock::new((0, Vec::new()));
/// The current ID of the accelerator.
static ID: AtomicUsize = AtomicUsize::new(0);
/// The type of each individual accelerator.
///
/// Maps from call hashes to return hashes.
type Accelerator = Mutex<PassthroughHashMap<u128, u128>>;
/// Generate a new accelerator.
pub fn id() -> usize {
// Get the next ID.
ID.fetch_add(1, Ordering::SeqCst)
}
/// Evict the accelerators.
pub fn evict() {
let mut accelerators = ACCELERATORS.write();
let (offset, vec) = &mut *accelerators;
// Update the offset.
*offset = ID.load(Ordering::SeqCst);
// Clear all accelerators while keeping the memory allocated.
vec.iter_mut().for_each(|accelerator| accelerator.lock().clear())
}
/// Get an accelerator by ID.
pub fn get(id: usize) -> Option<MappedRwLockReadGuard<'static, Accelerator>> {
// We always lock the accelerators, as we need to make sure that the
// accelerator is not removed while we are reading it.
let mut accelerators = ACCELERATORS.read();
let mut i = id.checked_sub(accelerators.0)?;
if i >= accelerators.1.len() {
drop(accelerators);
resize(i + 1);
accelerators = ACCELERATORS.read();
// Because we release the lock before resizing the accelerator, we need
// to check again whether the ID is still valid because another thread
// might evicted the cache.
i = id.checked_sub(accelerators.0)?;
}
Some(RwLockReadGuard::map(accelerators, move |(_, vec)| &vec[i]))
}
/// Adjusts the amount of accelerators.
#[cold]
fn resize(len: usize) {
let mut pair = ACCELERATORS.write();
if len > pair.1.len() {
pair.1.resize_with(len, || Mutex::new(PassthroughHashMap::default()));
}
}