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
use UnsafeCell;
use Deref;
use DerefMut;
use RawRwLock as RawRwLockTrait;
use RawRwLock;
/// Wraps a 'T' that can only be accessed through global mutexes at zero memory overhead per object.
;
// impl<T> ShardedRwLock<T> {
// /// Create a new ShardedRwLock from the given value.
// pub fn new(value: T) -> Self {
// ShardedRwLock(UnsafeCell::new(value))
// }
//
// // idea borrowed from crossbeam SeqLock
// fn get_mutex(&self) -> &'static RawRwLock {
// const LEN: usize = 127;
// #[repr(align(128))] // cache line aligned
// struct Locks([RawRwLock; LEN]);
// static LOCKS: Locks = Locks([RawRwLock::INIT; LEN]);
// &LOCKS.0[self as *const ShardedRwLock<T> as usize % LEN]
// }
//
// /// Acquire a global sharded lock guard with unlock on drop semantics
// pub fn lock(&self) -> ShardedRwLockGuard<T> {
// self.get_mutex().lock();
// ShardedRwLockGuard(&self)
// }
//
// /// Acquire a global sharded lock guard with unlock on drop semantics
// pub fn try_lock(&self) -> Option<ShardedRwLockGuard<T>> {
// self.get_mutex()
// .try_lock()
// .then(|| ShardedRwLockGuard(&self))
// }
// }
//
// /// The guard returned from locking a ShardedRwLock. Dropping this will unlock the mutex.
// /// Access to the underlying value is done by dereferencing this guard.
// pub struct ShardedRwLockGuard<'a, T>(&'a ShardedRwLock<T>);
//
// impl<T> Deref for ShardedRwLockGuard<'_, T> {
// type Target = T;
//
// fn deref(&self) -> &Self::Target {
// unsafe {
// // SAFETY: the guard gurantees that the we own the object
// &*self.0 .0.get()
// }
// }
// }
//
// impl<T> DerefMut for ShardedRwLockGuard<'_, T> {
// fn deref_mut(&mut self) -> &mut Self::Target {
// unsafe {
// // SAFETY: the guard gurantees that the we own the object
// &mut *self.0 .0.get()
// }
// }
// }
//
// impl<T> AsRef<T> for ShardedRwLockGuard<'_, T> {
// fn as_ref(&self) -> &T {
// unsafe {
// // SAFETY: the guard gurantees that the we own the object
// &*self.0 .0.get()
// }
// }
// }
//
// impl<T> AsMut<T> for ShardedRwLockGuard<'_, T> {
// fn as_mut(&mut self) -> &mut T {
// unsafe {
// // SAFETY: the guard gurantees that the we own the object
// &mut *self.0 .0.get()
// }
// }
// }
//
// impl<T> Drop for ShardedRwLockGuard<'_, T> {
// fn drop(&mut self) {
// unsafe {
// // SAFETY: the guard gurantees that the we have the lock
// self.0.get_mutex().unlock();
// }
// }
// }
//
// #[cfg(test)]
// mod tests {
// use crate::ShardedRwLock;
//
// #[test]
// fn smoke() {
// let x = ShardedRwLock::new(123);
// assert_eq!(*x.lock(), 123);
// }
//
// #[test]
// fn simple_lock() {
// let x = ShardedRwLock::new(123);
// assert_eq!(*x.lock(), 123);
//
// let mut guard = x.lock();
//
// *guard = 234;
// drop(guard);
//
// assert_eq!(*x.lock(), 234);
// }
// }