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
use std::cell::{Cell, UnsafeCell};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, AtomicPtr, Ordering};
use std::ptr::null_mut;
pub struct ArcRcu<T> {
inner: Arc<Inner<T>>,
have_borrowed: Cell<bool>,
}
unsafe impl<T: Send + Sync> Send for ArcRcu<T> {}
unsafe impl<T: Send + Sync> Sync for ArcRcu<T> {}
impl<T: Clone> Clone for ArcRcu<T> {
fn clone(&self) -> Self {
ArcRcu {
inner: self.inner.clone(),
have_borrowed: Cell::new(false),
}
}
}
pub struct Inner<T> {
borrow_count: AtomicUsize,
am_writing: AtomicBool,
list: List<T>
}
pub struct List<T> {
value: UnsafeCell<T>,
next: AtomicPtr<List<T>>,
}
impl<T> std::ops::Deref for ArcRcu<T> {
type Target = T;
fn deref(&self) -> &T {
let aleady_borrowed = self.have_borrowed.get();
if !aleady_borrowed {
self.inner.borrow_count.fetch_add(1, Ordering::Relaxed);
self.have_borrowed.set(true);
}
let next = self.inner.list.next.load(Ordering::Acquire);
if next == null_mut() {
unsafe { &* self.inner.list.value.get() }
} else {
unsafe { &* (*next).value.get() }
}
}
}
impl<T> std::borrow::Borrow<T> for ArcRcu<T> {
fn borrow(&self) -> &T {
&*self
}
}
impl<T> Drop for List<T> {
fn drop(&mut self) {
let next = self.next.load(Ordering::Acquire);
if next != null_mut() {
unsafe { Box::from_raw(next); }
}
}
}
impl<'a,T: Clone> ArcRcu<T> {
pub fn new(x: T) -> Self {
ArcRcu {
have_borrowed: Cell::new(false),
inner: Arc::new(Inner {
borrow_count: AtomicUsize::new(0),
am_writing: AtomicBool::new(false),
list: List {
value: UnsafeCell::new(x),
next: AtomicPtr::new(null_mut()),
},
}),
}
}
pub fn update(&'a self) -> Guard<'a, T> {
if self.inner.am_writing.swap(true, Ordering::Relaxed) {
panic!("Cannont update an ArcRcu twice simultaneously.");
}
Guard {
list: Some(List {
value: UnsafeCell::new((*(*self)).clone()),
next: AtomicPtr::new(self.inner.list.next.load(Ordering::Acquire)),
}),
rc_guts: &self.inner,
}
}
pub fn clean(&mut self) {
let aleady_borrowed = self.have_borrowed.get();
if aleady_borrowed {
self.inner.borrow_count.fetch_sub(1, Ordering::Relaxed);
self.have_borrowed.set(false);
}
let borrow_count = self.inner.borrow_count.load(Ordering::Relaxed);
let next = self.inner.list.next.load(Ordering::Acquire);
if borrow_count == 0 && next != null_mut() {
unsafe {
let buffer: UnsafeCell<Option<T>> = UnsafeCell::new(None);
std::ptr::copy_nonoverlapping(self.inner.list.value.get(),
buffer.get() as *mut T, 1);
std::ptr::copy_nonoverlapping((*next).value.get(),
self.inner.list.value.get(), 1);
let _to_be_freed = Box::from_raw(self.inner.list.next.swap(null_mut(), Ordering::Release));
std::ptr::copy_nonoverlapping(buffer.get() as *mut T,
(*next).value.get(), 1);
let buffer_copy: UnsafeCell<Option<T>> = UnsafeCell::new(None);
std::ptr::copy_nonoverlapping(buffer_copy.get(), buffer.get(), 1);
}
}
}
}
pub struct Guard<'a,T: Clone> {
list: Option<List<T>>,
rc_guts: &'a Inner<T>,
}
impl<'a,T: Clone> std::ops::Deref for Guard<'a,T> {
type Target = T;
fn deref(&self) -> &T {
if let Some(ref list) = self.list {
unsafe { & *list.value.get() }
} else {
unreachable!()
}
}
}
impl<'a,T: Clone> std::ops::DerefMut for Guard<'a,T> {
fn deref_mut(&mut self) -> &mut T {
if let Some(ref list) = self.list {
unsafe { &mut *list.value.get() }
} else {
unreachable!()
}
}
}
impl<'a,T: Clone> Drop for Guard<'a,T> {
fn drop(&mut self) {
let list = std::mem::replace(&mut self.list, None);
self.rc_guts.list.next.store(Box::into_raw(Box::new(list.unwrap())),
Ordering::Release);
self.rc_guts.am_writing.store(false, Ordering::Relaxed);
}
}