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
use std::collections::hash_map::DefaultHasher;
use std::collections::VecDeque;
use std::hash::{Hash, Hasher};
use std::usize;
use crossbeam::utils::CachePadded;
use parking_lot::{Mutex, MutexGuard};
const WAITING_LIST_SHRINK_SIZE: usize = 8;
const WAITING_LIST_MAX_CAPACITY: usize = 16;
#[derive(Clone)]
struct Latch {
pub waiting: VecDeque<Option<(u64, u64)>>,
}
impl Latch {
pub fn new() -> Latch {
Latch {
waiting: VecDeque::new(),
}
}
pub fn get_first_req_by_hash(&self, hash: u64) -> Option<u64> {
for (h, cid) in self.waiting.iter().flatten() {
if *h == hash {
return Some(*cid);
}
}
None
}
pub fn pop_front(&mut self, key_hash: u64) -> Option<(u64, u64)> {
if let Some(item) = self.waiting.pop_front() {
if let Some((k, _)) = item.as_ref() {
if *k == key_hash {
self.maybe_shrink();
return item;
}
self.waiting.push_front(item);
}
#[allow(clippy::manual_flatten)]
for it in self.waiting.iter_mut() {
if let Some((v, _)) = it {
if *v == key_hash {
return it.take();
}
}
}
}
None
}
pub fn wait_for_wake(&mut self, key_hash: u64, cid: u64) {
self.waiting.push_back(Some((key_hash, cid)));
}
fn maybe_shrink(&mut self) {
while let Some(item) = self.waiting.front() {
if item.is_some() {
break;
}
self.waiting.pop_front().unwrap();
}
if self.waiting.capacity() > WAITING_LIST_MAX_CAPACITY
&& self.waiting.len() < WAITING_LIST_SHRINK_SIZE
{
self.waiting.shrink_to_fit();
}
}
}
#[derive(Clone)]
pub struct Lock {
pub required_hashes: Vec<u64>,
pub owned_count: usize,
}
impl Lock {
pub fn new<'a, K, I>(keys: I) -> Lock
where
K: Hash + 'a,
I: IntoIterator<Item = &'a K>,
{
let mut required_hashes: Vec<u64> = keys
.into_iter()
.map(|key| {
let mut s = DefaultHasher::new();
key.hash(&mut s);
s.finish()
})
.collect();
required_hashes.sort_unstable();
required_hashes.dedup();
Lock {
required_hashes,
owned_count: 0,
}
}
pub fn acquired(&self) -> bool {
self.required_hashes.len() == self.owned_count
}
pub fn is_write_lock(&self) -> bool {
!self.required_hashes.is_empty()
}
}
pub struct Latches {
slots: Vec<CachePadded<Mutex<Latch>>>,
size: usize,
}
impl Latches {
pub fn new(size: usize) -> Latches {
let size = usize::next_power_of_two(size);
let mut slots = Vec::with_capacity(size);
(0..size).for_each(|_| slots.push(Mutex::new(Latch::new()).into()));
Latches { slots, size }
}
pub fn acquire(&self, lock: &mut Lock, who: u64) -> bool {
let mut acquired_count: usize = 0;
for &key_hash in &lock.required_hashes[lock.owned_count..] {
let mut latch = self.lock_latch(key_hash);
match latch.get_first_req_by_hash(key_hash) {
Some(cid) => {
if cid == who {
acquired_count += 1;
} else {
latch.wait_for_wake(key_hash, who);
break;
}
}
None => {
latch.wait_for_wake(key_hash, who);
acquired_count += 1;
}
}
}
lock.owned_count += acquired_count;
lock.acquired()
}
pub fn release(&self, lock: &Lock, who: u64) -> Vec<u64> {
let mut wakeup_list: Vec<u64> = vec![];
for &key_hash in &lock.required_hashes[..lock.owned_count] {
let mut latch = self.lock_latch(key_hash);
let (v, front) = latch.pop_front(key_hash).unwrap();
assert_eq!(front, who);
assert_eq!(v, key_hash);
if let Some(wakeup) = latch.get_first_req_by_hash(key_hash) {
wakeup_list.push(wakeup);
}
}
wakeup_list
}
#[inline]
fn lock_latch(&self, hash: u64) -> MutexGuard<Latch> {
self.slots[(hash as usize) & (self.size - 1)].lock()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_wakeup() {
let latches = Latches::new(256);
let keys_a = vec!["k1", "k3", "k5"];
let mut lock_a = Lock::new(keys_a.iter());
let keys_b = vec!["k4", "k5", "k6"];
let mut lock_b = Lock::new(keys_b.iter());
let cid_a: u64 = 1;
let cid_b: u64 = 2;
let acquired_a = latches.acquire(&mut lock_a, cid_a);
assert_eq!(acquired_a, true);
let mut acquired_b = latches.acquire(&mut lock_b, cid_b);
assert_eq!(acquired_b, false);
let wakeup = latches.release(&lock_a, cid_a);
assert_eq!(wakeup[0], cid_b);
acquired_b = latches.acquire(&mut lock_b, cid_b);
assert_eq!(acquired_b, true);
}
#[test]
fn test_wakeup_by_multi_cmds() {
let latches = Latches::new(256);
let keys_a = vec!["k1", "k2", "k3"];
let keys_b = vec!["k4", "k5", "k6"];
let keys_c = vec!["k3", "k4"];
let mut lock_a = Lock::new(keys_a.iter());
let mut lock_b = Lock::new(keys_b.iter());
let mut lock_c = Lock::new(keys_c.iter());
let cid_a: u64 = 1;
let cid_b: u64 = 2;
let cid_c: u64 = 3;
let acquired_a = latches.acquire(&mut lock_a, cid_a);
assert_eq!(acquired_a, true);
let acquired_b = latches.acquire(&mut lock_b, cid_b);
assert_eq!(acquired_b, true);
let mut acquired_c = latches.acquire(&mut lock_c, cid_c);
assert_eq!(acquired_c, false);
let wakeup = latches.release(&lock_a, cid_a);
assert_eq!(wakeup[0], cid_c);
acquired_c = latches.acquire(&mut lock_c, cid_c);
assert_eq!(acquired_c, false);
let wakeup = latches.release(&lock_b, cid_b);
assert_eq!(wakeup[0], cid_c);
acquired_c = latches.acquire(&mut lock_c, cid_c);
assert_eq!(acquired_c, true);
}
#[test]
fn test_wakeup_by_small_latch_slot() {
let latches = Latches::new(5);
let keys_a = vec!["k1", "k2", "k3"];
let keys_b = vec!["k6", "k7", "k8"];
let keys_c = vec!["k3", "k4"];
let keys_d = vec!["k7", "k10"];
let mut lock_a = Lock::new(keys_a.iter());
let mut lock_b = Lock::new(keys_b.iter());
let mut lock_c = Lock::new(keys_c.iter());
let mut lock_d = Lock::new(keys_d.iter());
let cid_a: u64 = 1;
let cid_b: u64 = 2;
let cid_c: u64 = 3;
let cid_d: u64 = 4;
let acquired_a = latches.acquire(&mut lock_a, cid_a);
assert_eq!(acquired_a, true);
let mut acquired_c = latches.acquire(&mut lock_c, cid_c);
assert_eq!(acquired_c, false);
let acquired_b = latches.acquire(&mut lock_b, cid_b);
assert_eq!(acquired_b, true);
let mut acquired_d = latches.acquire(&mut lock_d, cid_d);
assert_eq!(acquired_d, false);
let wakeup = latches.release(&lock_a, cid_a);
assert_eq!(wakeup[0], cid_c);
acquired_c = latches.acquire(&mut lock_c, cid_c);
assert_eq!(acquired_c, true);
let wakeup = latches.release(&lock_b, cid_b);
assert_eq!(wakeup[0], cid_d);
acquired_d = latches.acquire(&mut lock_d, cid_d);
assert_eq!(acquired_d, true);
}
}