forked from mongodb/mongo-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracking_arc.rs
141 lines (128 loc) · 3.75 KB
/
tracking_arc.rs
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
#[cfg(all(test, mongodb_internal_tracking_arc))]
use crate::id_set::{self, IdSet};
use std::sync::Arc;
#[cfg(all(test, mongodb_internal_tracking_arc))]
use std::sync::Mutex as SyncMutex;
/// An `Arc` that records the backtraces of construction of live clones. When not compiled
/// with `cfg(mongodb_internal_tracking_arc)`, a zero-cost `Arc` wrapper.
#[derive(Debug)]
pub(crate) struct TrackingArc<T> {
inner: Arc<Inner<T>>,
#[cfg(all(test, mongodb_internal_tracking_arc))]
clone_id: Option<id_set::Id>,
}
#[derive(Debug)]
struct Inner<T> {
value: T,
#[cfg(all(test, mongodb_internal_tracking_arc))]
clones: SyncMutex<IdSet<backtrace::Backtrace>>,
}
impl<T> TrackingArc<T> {
pub(crate) fn new(value: T) -> Self {
Self {
inner: Arc::new(Inner {
value,
#[cfg(all(test, mongodb_internal_tracking_arc))]
clones: SyncMutex::new(IdSet::new()),
}),
#[cfg(all(test, mongodb_internal_tracking_arc))]
clone_id: None,
}
}
#[allow(unused)]
pub(crate) fn try_unwrap(tracked: Self) -> Result<T, Self> {
let inner = tracked.inner.clone();
#[cfg(all(test, mongodb_internal_tracking_arc))]
let clone_id = {
let mut tracked = tracked;
tracked.clone_id.take()
};
match Arc::try_unwrap(inner) {
Ok(inner) => Ok(inner.value),
Err(inner) => Err(Self {
inner,
#[cfg(all(test, mongodb_internal_tracking_arc))]
clone_id,
}),
}
}
pub(crate) fn downgrade(tracked: &Self) -> Weak<T> {
Weak {
inner: Arc::downgrade(&tracked.inner),
}
}
pub(crate) fn ptr_eq(this: &Self, other: &Self) -> bool {
Arc::ptr_eq(&this.inner, &other.inner)
}
#[cfg(all(test, mongodb_internal_tracking_arc))]
#[allow(unused)]
pub(crate) fn print_live(tracked: &Self) {
let current: Vec<_> = tracked
.inner
.clones
.lock()
.unwrap()
.values()
.cloned()
.collect();
for mut bt in current {
bt.resolve();
println!("{:?}", bt);
}
}
}
impl<T> Clone for TrackingArc<T> {
fn clone(&self) -> Self {
#[cfg(all(test, mongodb_internal_tracking_arc))]
let clone_id = {
let bt = backtrace::Backtrace::new_unresolved();
Some(self.inner.clones.lock().unwrap().insert(bt))
};
Self {
inner: self.inner.clone(),
#[cfg(all(test, mongodb_internal_tracking_arc))]
clone_id,
}
}
}
impl<T> Drop for TrackingArc<T> {
fn drop(&mut self) {
#[cfg(all(test, mongodb_internal_tracking_arc))]
if let Some(id) = &self.clone_id {
self.inner.clones.lock().unwrap().remove(id);
}
}
}
impl<T> std::ops::Deref for TrackingArc<T> {
type Target = T;
fn deref(&self) -> &T {
&self.inner.value
}
}
#[derive(Debug)]
pub(crate) struct Weak<T> {
inner: std::sync::Weak<Inner<T>>,
}
impl<T> Clone for Weak<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<T> Weak<T> {
pub(crate) fn upgrade(&self) -> Option<TrackingArc<T>> {
self.inner.upgrade().map(|inner| {
#[cfg(all(test, mongodb_internal_tracking_arc))]
let clone_id = {
let bt = backtrace::Backtrace::new_unresolved();
Some(inner.clones.lock().unwrap().insert(bt))
};
TrackingArc {
inner,
#[cfg(all(test, mongodb_internal_tracking_arc))]
clone_id,
}
})
}
}