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
use std::{
hint::spin_loop,
sync::{atomic::{AtomicBool, AtomicUsize, Ordering}}, cell::UnsafeCell
};
pub mod armc_ref_guard;
pub mod armc_guard;
use std::ops::{Deref, DerefMut};
use self::{armc_ref_guard::ArmcRefGuard, armc_guard::ArmcGuard};
pub(crate) struct Core<T: ?Sized> {
count_ref: AtomicUsize,
looked: AtomicBool,
data: UnsafeCell<T>,
}
impl<T: ?Sized> Core<T> {
pub fn lock(&self) -> ArmcGuard<'_, T> {
while !self.looked.swap(true, Ordering::AcqRel)
&& self.count_ref.load(Ordering::Relaxed) > 0
{
spin_loop();
}
let ref_mut = unsafe {
&mut *(( self as *const Self) as *mut Self)
};
ArmcGuard::new(ref_mut)
}
pub fn lock_ref(&self) -> ArmcRefGuard<'_, T> {
while self.looked.load(Ordering::Relaxed) {
spin_loop();
}
self.count_ref.fetch_add(1, Ordering::Relaxed);
ArmcRefGuard::new(&self)
}
fn drop(&self) {
self.looked.store(false, Ordering::Release);
}
fn drop_ref(&self) {
self.count_ref.fetch_sub(1, Ordering::Relaxed);
}
}
impl<T : Sized> Core<T>{
pub fn new(data: T) -> Core<T> {
Core {
count_ref: AtomicUsize::default(),
looked: AtomicBool::default(),
data: UnsafeCell::new(data),
}
}
pub fn unwrap(a: Self) -> T {
a.data.into_inner()
}
}
impl<T: ?Sized> Deref for Core<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
while self.looked.load(Ordering::SeqCst) {
spin_loop();
}
unsafe{&*self.data.get()}
}
}
impl<T: ?Sized> DerefMut for Core<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe{&mut *self.data.get()}
}
}
unsafe impl<T: ?Sized> Send for Core<T> where T: Send {}
unsafe impl<T: ?Sized> Sync for Core<T> where T: Send {}