-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathglobal_safety.rs
More file actions
149 lines (130 loc) · 3.54 KB
/
Copy pathglobal_safety.rs
File metadata and controls
149 lines (130 loc) · 3.54 KB
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
use crate::flog::flog;
use std::cell::{Ref, RefMut};
use std::sync::MutexGuard;
use std::sync::atomic::{AtomicBool, AtomicPtr, Ordering};
#[derive(Debug, Default)]
pub struct RelaxedAtomicBool(AtomicBool);
impl RelaxedAtomicBool {
pub const fn new(value: bool) -> Self {
Self(AtomicBool::new(value))
}
pub fn load(&self) -> bool {
self.0.load(Ordering::Relaxed)
}
pub fn store(&self, value: bool) {
self.0.store(value, Ordering::Relaxed);
}
pub fn swap(&self, value: bool) -> bool {
self.0.swap(value, Ordering::Relaxed)
}
}
impl Clone for RelaxedAtomicBool {
fn clone(&self) -> Self {
Self(AtomicBool::new(self.load()))
}
}
/// An atomic reference type, allowing &'static values to be stored.
/// This uses relaxed ordering - it's intended for string literals.
/// Note that because string literals are fat pointers, we can't store one
/// directly in an AtomicPtr, so we store a pointer to the string literal instead!
pub struct AtomicRef<T: ?Sized + 'static>(AtomicPtr<&'static T>);
impl<T: ?Sized> AtomicRef<T> {
pub const fn new(value: &'static &'static T) -> Self {
Self(AtomicPtr::new(std::ptr::from_ref(value).cast_mut()))
}
pub fn load(&self) -> &'static T {
unsafe { *self.0.load(Ordering::Relaxed) }
}
pub fn store(&self, value: &'static &'static T) {
self.0
.store(std::ptr::from_ref(value).cast_mut(), Ordering::Relaxed);
}
}
pub struct DebugRef<'a, T>(Ref<'a, T>);
impl<'a, T> DebugRef<'a, T> {
pub fn new(r: Ref<'a, T>) -> Self {
flog!(
refcell,
"CREATE DebugRef",
std::backtrace::Backtrace::capture()
);
Self(r)
}
}
impl<'a, T> Drop for DebugRef<'a, T> {
fn drop(&mut self) {
flog!(
refcell,
"DROP DebugRef",
std::backtrace::Backtrace::capture()
);
}
}
impl<'a, T> std::ops::Deref for DebugRef<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub struct DebugRefMut<'a, T>(RefMut<'a, T>);
impl<'a, T> DebugRefMut<'a, T> {
pub fn new(r: RefMut<'a, T>) -> Self {
flog!(
refcell,
"CREATE DebugRefMut",
std::backtrace::Backtrace::capture()
);
Self(r)
}
}
impl<'a, T> Drop for DebugRefMut<'a, T> {
fn drop(&mut self) {
flog!(
refcell,
"DROP DebugRefMut",
std::backtrace::Backtrace::capture()
);
}
}
impl<'a, T> std::ops::Deref for DebugRefMut<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a, T> std::ops::DerefMut for DebugRefMut<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub struct DebugMutexGuard<'a, T>(MutexGuard<'a, T>);
impl<'a, T> DebugMutexGuard<'a, T> {
pub fn new(r: MutexGuard<'a, T>) -> Self {
flog!(
refcell,
"CREATE DebugMutexGuard",
std::backtrace::Backtrace::capture()
);
Self(r)
}
}
impl<'a, T> Drop for DebugMutexGuard<'a, T> {
fn drop(&mut self) {
flog!(
refcell,
"DROP DebugMutexGuard",
std::backtrace::Backtrace::capture()
);
}
}
impl<'a, T> std::ops::Deref for DebugMutexGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a, T> std::ops::DerefMut for DebugMutexGuard<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}