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
use super::lock_table::LockTable;
use parking_lot::Mutex;
use std::{cell::UnsafeCell, mem, sync::Arc};
use tokio::sync::{Mutex as AsyncMutex, MutexGuard as AsyncMutexGuard};
use txn_types::{Key, Lock};
pub struct KeyHandle {
pub key: Key,
table: UnsafeCell<Option<LockTable>>,
mutex: AsyncMutex<()>,
lock_store: Mutex<Option<Lock>>,
}
impl KeyHandle {
pub fn new(key: Key) -> Self {
KeyHandle {
key,
table: UnsafeCell::new(None),
mutex: AsyncMutex::new(()),
lock_store: Mutex::new(None),
}
}
pub async fn lock(self: Arc<Self>) -> KeyHandleGuard {
let mutex_guard = unsafe { mem::transmute(self.mutex.lock().await) };
KeyHandleGuard {
_mutex_guard: mutex_guard,
handle: self,
}
}
pub fn with_lock<T>(&self, f: impl FnOnce(&Option<Lock>) -> T) -> T {
f(&*self.lock_store.lock())
}
pub(crate) unsafe fn set_table(&self, table: LockTable) {
*self.table.get() = Some(table);
}
}
impl Drop for KeyHandle {
fn drop(&mut self) {
unsafe {
if let Some(table) = &*self.table.get() {
table.remove(&self.key);
}
}
}
}
unsafe impl Sync for KeyHandle {}
pub struct KeyHandleGuard {
_mutex_guard: AsyncMutexGuard<'static, ()>,
handle: Arc<KeyHandle>,
}
impl KeyHandleGuard {
pub fn key(&self) -> &Key {
&self.handle.key
}
pub fn with_lock<T>(&self, f: impl FnOnce(&mut Option<Lock>) -> T) -> T {
f(&mut *self.handle.lock_store.lock())
}
pub(crate) fn handle(&self) -> &Arc<KeyHandle> {
&self.handle
}
}
impl Drop for KeyHandleGuard {
fn drop(&mut self) {
*self.handle.lock_store.lock() = None;
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::{
sync::atomic::{AtomicUsize, Ordering},
time::Duration,
};
use tokio::time::delay_for;
#[tokio::test]
async fn test_key_mutex() {
let key_handle = Arc::new(KeyHandle::new(Key::from_raw(b"k")));
let counter = Arc::new(AtomicUsize::new(0));
let mut handles = Vec::new();
for _ in 0..100 {
let key_handle = key_handle.clone();
let counter = counter.clone();
let handle = tokio::spawn(async move {
let _guard = key_handle.lock().await;
let counter_val = counter.fetch_add(1, Ordering::SeqCst) + 1;
delay_for(Duration::from_millis(1)).await;
assert_eq!(counter.load(Ordering::SeqCst), counter_val);
});
handles.push(handle);
}
for handle in handles {
handle.await.unwrap();
}
assert_eq!(counter.load(Ordering::SeqCst), 100);
}
#[tokio::test]
async fn test_ref_count() {
let table = LockTable::default();
let k = Key::from_raw(b"k");
let handle = Arc::new(KeyHandle::new(k.clone()));
table.0.insert(k.clone(), Arc::downgrade(&handle));
unsafe {
handle.set_table(table.clone());
}
let lock_ref1 = table.get(&k).unwrap();
let lock_ref2 = table.get(&k).unwrap();
drop(handle);
drop(lock_ref1);
assert!(table.get(&k).is_some());
drop(lock_ref2);
assert!(table.get(&k).is_none());
}
}