1use crate::rcu_ptr::{RcuPtr, RcuReadGuard};
6use crate::rcu_read_scope::RcuReadScope;
7use crate::state_machine::rcu_drop;
8use crate::subtle::RcuPtrRef;
9use std::sync::Arc;
10
11#[derive(Debug)]
16pub struct RcuArc<T: Send + Sync + 'static> {
17 ptr: RcuPtr<T>,
18}
19
20impl<T: Send + Sync + 'static> RcuArc<T> {
21 pub fn new(data: Arc<T>) -> Self {
23 Self { ptr: RcuPtr::new(Self::into_ptr(data)) }
24 }
25
26 pub fn read(&self) -> RcuReadGuard<T> {
31 self.ptr.get()
32 }
33
34 pub fn as_ref<'a>(&self, scope: &'a RcuReadScope) -> &'a T {
39 self.ptr.read(scope).as_ref().unwrap()
40 }
41
42 pub fn update(&self, data: Arc<T>) {
47 let ptr = Self::into_ptr(data);
48 unsafe { self.replace(ptr) };
50 }
51
52 pub fn update_swap<'a>(&self, scope: &'a RcuReadScope, data: Arc<T>) -> &'a T {
57 let ptr = Self::into_ptr(data);
58 unsafe { self.replace_swap(scope, ptr) }
60 }
61
62 pub fn to_arc(&self) -> Arc<T> {
67 let scope = RcuReadScope::new();
68 let ptr = self.ptr.read(&scope);
69 unsafe { rcu_ptr_to_arc(ptr) }
72 }
73
74 fn into_ptr(data: Arc<T>) -> *mut T {
79 Arc::into_raw(data) as *mut T
80 }
81
82 unsafe fn replace(&self, ptr: *mut T) {
88 let old_ptr = self.ptr.replace(ptr);
89 let arc = unsafe { Arc::from_raw(old_ptr) };
90 rcu_drop(arc);
91 }
92
93 unsafe fn replace_swap<'a>(&self, scope: &'a RcuReadScope, ptr: *mut T) -> &'a T {
100 let old_ptr_ref = self.ptr.swap(scope, ptr);
101 let arc = unsafe { Arc::from_raw(old_ptr_ref.as_ptr()) };
103 rcu_drop(arc);
104 old_ptr_ref.as_ref().unwrap()
105 }
106}
107
108impl<T: Send + Sync + 'static> Drop for RcuArc<T> {
109 fn drop(&mut self) {
110 unsafe { self.replace(std::ptr::null_mut()) };
112 }
113}
114
115impl<T: Send + Sync + 'static> Clone for RcuArc<T> {
116 fn clone(&self) -> Self {
117 Self::new(self.to_arc())
118 }
119}
120
121impl<T: Send + Sync + 'static> From<Arc<T>> for RcuArc<T> {
122 fn from(data: Arc<T>) -> Self {
123 Self::new(data)
124 }
125}
126
127impl<T: Default + Send + Sync + 'static> Default for RcuArc<T> {
128 fn default() -> Self {
129 Self::new(Arc::new(T::default()))
130 }
131}
132
133pub unsafe fn rcu_ptr_to_arc<'a, T>(ptr: RcuPtrRef<'a, T>) -> Arc<T> {
139 let raw_ptr = ptr.as_ptr();
140 unsafe {
141 Arc::increment_strong_count(raw_ptr);
142 Arc::from_raw(raw_ptr)
143 }
144}
145
146#[cfg(test)]
147mod tests {
148 use super::*;
149 use crate::state_machine::rcu_synchronize;
150 use std::sync::atomic::{AtomicUsize, Ordering};
151
152 struct DropCounter {
153 value: usize,
154 drops: Arc<AtomicUsize>,
155 }
156
157 impl DropCounter {
158 pub fn new(value: usize) -> Arc<Self> {
159 Arc::new(Self { value, drops: Arc::new(AtomicUsize::new(0)) })
160 }
161 }
162
163 impl Drop for DropCounter {
164 fn drop(&mut self) {
165 self.drops.fetch_add(1, Ordering::Relaxed);
166 }
167 }
168
169 #[test]
170 fn test_rcu_arc_update() {
171 let object = DropCounter::new(42);
172 let drops = object.drops.clone();
173
174 let arc = RcuArc::from(object);
175 assert_eq!(arc.read().value, 42);
176 assert_eq!(drops.load(Ordering::Relaxed), 0);
177 arc.update(DropCounter::new(43));
178 assert_eq!(arc.read().value, 43);
179 assert_eq!(drops.load(Ordering::Relaxed), 0);
180
181 rcu_synchronize();
182 assert_eq!(drops.load(Ordering::Relaxed), 1);
183 }
184
185 #[test]
186 fn test_rcu_arc_update_swap() {
187 let object = DropCounter::new(42);
188 let drops = object.drops.clone();
189
190 let arc = RcuArc::from(object);
191 {
192 let scope = RcuReadScope::new();
193 let old_object = arc.update_swap(&scope, DropCounter::new(43));
194 assert_eq!(old_object.value, 42);
195 assert_eq!(arc.read().value, 43);
196 assert_eq!(drops.load(Ordering::Relaxed), 0);
197 }
198
199 rcu_synchronize();
200 assert_eq!(drops.load(Ordering::Relaxed), 1);
201 }
202}