-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
Expand file tree
/
Copy pathsync_table.rs
More file actions
168 lines (138 loc) · 4.49 KB
/
sync_table.rs
File metadata and controls
168 lines (138 loc) · 4.49 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
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
use std::borrow::Borrow;
use std::hash::{BuildHasher, Hash, Hasher};
use std::ops::{Deref, DerefMut};
use horde::collect::{Pin, pin};
pub use horde::sync_table::Read;
use horde::sync_table::Write;
use rustc_hash::FxBuildHasher;
use crate::sharded::IntoPointer;
use crate::sync::{DynSync, Lock, LockGuard, Mode};
pub struct SyncTable<K, V> {
// We use this lock to protect `table` instead of the internal mutex in `horde::SyncTable`
// as it's faster when synchronization is disabled.
lock: Lock<()>,
table: horde::SyncTable<K, V, FxBuildHasher>,
}
// Memory reclamation can move elements to other threads for dropping,
// so we require `Sync` instead of `DynSync` here
unsafe impl<K: Sync, V: Sync> DynSync for SyncTable<K, V> where FxBuildHasher: Sync {}
impl<K, V> Default for SyncTable<K, V> {
fn default() -> Self {
Self { lock: Lock::default(), table: horde::SyncTable::default() }
}
}
impl<K, V> SyncTable<K, V> {
/// Creates a [Read] handle from a pinned region.
///
/// Use [horde::collect::pin] to get a `Pin` instance.
#[inline]
pub fn read<'a>(&'a self, pin: Pin<'a>) -> Read<'a, K, V, FxBuildHasher> {
self.table.read(pin)
}
/// Creates a [LockedWrite] handle by taking the underlying mutex that protects writes.
#[inline]
pub fn lock(&self) -> LockedWrite<'_, K, V> {
LockedWrite {
_guard: self.lock.lock(),
table: {
// SAFETY: We ensure there's only 1 writer at a time using our own lock
unsafe { self.table.unsafe_write() }
},
}
}
/// Hashes a key with the table's hasher.
#[inline]
pub fn hash_key<Q>(&self, key: &Q) -> u64
where
K: Borrow<Q>,
Q: ?Sized + Hash,
{
self.table.hash_key::<Q>(key)
}
pub fn len(&self) -> usize {
pin(|pin| self.read(pin).len())
}
pub fn with_capacity(cap: usize) -> Self {
Self { lock: Lock::new(()), table: horde::SyncTable::new_with(FxBuildHasher, cap) }
}
}
/// A handle to a [SyncTable] with write access protected by a lock.
pub struct LockedWrite<'a, K, V> {
table: Write<'a, K, V, FxBuildHasher>,
_guard: LockGuard<'a, ()>,
}
impl<'a, K, V> Deref for LockedWrite<'a, K, V> {
type Target = Write<'a, K, V, FxBuildHasher>;
#[inline]
fn deref(&self) -> &Self::Target {
&self.table
}
}
impl<'a, K, V> DerefMut for LockedWrite<'a, K, V> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.table
}
}
impl<K: Eq + Hash + Copy + Send> SyncTable<K, ()> {
pub fn contains_pointer_to<T: Hash + IntoPointer>(&self, value: &T) -> bool
where
K: IntoPointer,
{
pin(|pin| {
let mut state = FxBuildHasher.build_hasher();
value.hash(&mut state);
let hash = state.finish();
let value = value.into_pointer();
self.read(pin).get_from_hash(hash, |entry| entry.into_pointer() == value).is_some()
})
}
#[inline]
pub fn intern_ref<Q: ?Sized>(&self, value: &Q, make: impl FnOnce() -> K) -> K
where
K: Borrow<Q>,
Q: Hash + Eq,
{
pin(|pin| {
let hash = self.hash_key(value);
let entry = self.read(pin).get(value, Some(hash));
if let Some(entry) = entry {
return *entry.0;
}
let mut write = self.lock();
if self.lock.mode() == Mode::Sync {
let entry = self.read(pin).get(value, Some(hash));
if let Some(entry) = entry {
return *entry.0;
}
}
let result = make();
write.insert_new(result, (), Some(hash));
result
})
}
#[inline]
pub fn intern<Q>(&self, value: Q, make: impl FnOnce(Q) -> K) -> K
where
K: Borrow<Q>,
Q: Hash + Eq,
{
pin(|pin| {
let hash = self.hash_key(&value);
let entry = self.read(pin).get(&value, Some(hash));
if let Some(entry) = entry {
return *entry.0;
}
let mut write = self.lock();
if self.lock.mode() == Mode::Sync {
let entry = self.read(pin).get(&value, Some(hash));
if let Some(entry) = entry {
return *entry.0;
}
}
let result = make(value);
write.insert_new(result, (), Some(hash));
result
})
}
}