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
use crate::{common::guards_guard, error::LockType, tracker::GuardTracker};
use parking_lot::{MutexGuard, RwLockReadGuard, RwLockWriteGuard};
use snowflake::ProcessUniqueId;
use std::{
borrow::{Borrow, BorrowMut},
ops::{Deref, DerefMut},
};
macro_rules! guard_struct {
($HcGuard:ident, $Guard:ident, $lock_type:ident) => {
pub struct $HcGuard<'a, T: ?Sized> {
puid: ProcessUniqueId,
inner: Option<$Guard<'a, T>>,
fair_unlocking: bool,
}
impl<'a, T: ?Sized> $HcGuard<'a, T> {
pub fn new(inner: $Guard<'a, T>) -> Self {
let puid = ProcessUniqueId::new();
guards_guard().insert(puid, GuardTracker::new(puid, LockType::$lock_type));
Self {
puid,
inner: Some(inner),
fair_unlocking: false,
}
}
pub fn annotate<S: Into<String>>(self, annotation: S) -> Self {
guards_guard()
.entry(self.puid)
.and_modify(|g| g.annotation = Some(annotation.into()));
self
}
pub fn use_fair_unlocking(mut self) -> Self {
self.fair_unlocking = true;
self
}
pub fn unlock_fair(mut self) {
self._unlock_fair();
}
fn _unlock_fair(&mut self) {
if let Some(inner) = self.inner.take() {
$Guard::unlock_fair(inner);
}
}
}
impl<'a, T: ?Sized> Drop for $HcGuard<'a, T> {
fn drop(&mut self) {
guards_guard().remove(&self.puid);
if self.fair_unlocking {
self._unlock_fair();
}
}
}
};
}
guard_struct!(HcMutexGuard, MutexGuard, Lock);
guard_struct!(HcRwLockReadGuard, RwLockReadGuard, Read);
guard_struct!(HcRwLockWriteGuard, RwLockWriteGuard, Write);
impl<'a, T: ?Sized> Borrow<T> for HcMutexGuard<'a, T> {
fn borrow(&self) -> &T {
self.inner.as_ref().expect("accessed mutex mid-unlock!")
}
}
impl<'a, T: ?Sized> BorrowMut<T> for HcMutexGuard<'a, T> {
fn borrow_mut(&mut self) -> &mut T {
self.inner.as_mut().expect("accessed mutex mid-unlock!")
}
}
impl<'a, T: ?Sized> Deref for HcMutexGuard<'a, T> {
type Target = T;
fn deref(&self) -> &T {
&*self.inner.as_ref().expect("accessed mutex mid-unlock!")
}
}
impl<'a, T: ?Sized> DerefMut for HcMutexGuard<'a, T> {
fn deref_mut(&mut self) -> &mut T {
&mut *self.inner.as_mut().expect("accessed mutex mid-unlock!")
}
}
impl<'a, T: ?Sized> Borrow<T> for HcRwLockReadGuard<'a, T> {
fn borrow(&self) -> &T {
self.inner.as_ref().expect("accessed mutex mid-unlock!")
}
}
impl<'a, T: ?Sized> Deref for HcRwLockReadGuard<'a, T> {
type Target = T;
fn deref(&self) -> &T {
&*self.inner.as_ref().expect("accessed mutex mid-unlock!")
}
}
impl<'a, T: ?Sized> Borrow<T> for HcRwLockWriteGuard<'a, T> {
fn borrow(&self) -> &T {
self.inner.as_ref().expect("accessed mutex mid-unlock!")
}
}
impl<'a, T: ?Sized> BorrowMut<T> for HcRwLockWriteGuard<'a, T> {
fn borrow_mut(&mut self) -> &mut T {
self.inner.as_mut().expect("accessed mutex mid-unlock!")
}
}
impl<'a, T: ?Sized> Deref for HcRwLockWriteGuard<'a, T> {
type Target = T;
fn deref(&self) -> &T {
&*self.inner.as_ref().expect("accessed mutex mid-unlock!")
}
}
impl<'a, T: ?Sized> DerefMut for HcRwLockWriteGuard<'a, T> {
fn deref_mut(&mut self) -> &mut T {
&mut *self.inner.as_mut().expect("accessed mutex mid-unlock!")
}
}