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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright 2018 k3d3 <k@3d3.ca>

#![warn(missing_docs)]
//! This library provides a Software Transactional Memory structure that
//! can be used for sharing data among multiple threads in a way that is
//! safe and can be loaded quickly.
//!
//! For more information, look at the documentation for the `Stm` struct.
//!
//! # Example
//! ```
//! use crossbeam_arccell::ArcCell;
//!
//! // Create a new ArcCell with a Vec of numbers
//! let arc = ArcCell::new(vec![1,2,3,4]);
//!
//! // Read from the ArcCell
//! {
//!     let data = arc.load();
//!     println!("Current ArcCell value: {:?}", data);
//! }
//!
//! // Update the ArcCell pointer to add a new number
//! arc.update(|old| {
//!     let mut new = old.clone();
//!     new.push(5);
//!     new
//! });
//!
//! // Read the new data
//! {
//!     let data = arc.load();
//!     println!("Current ArcCell: {:?}", data);
//! }
//!
//! // Set the ArcCell pointer
//! let data = vec![9,8,7,6];
//! arc.set(data);
//!
//! // Read the new data, again
//! {
//!     let data = arc.load();
//!     println!("Current ArcCell: {:?}", data);
//! }
//! ```

extern crate crossbeam_epoch;

use crossbeam_epoch::{Atomic, Owned};
use std::sync::atomic::Ordering;
use std::ops::Deref;
use std::fmt;

/// An updatable Arc.
///
/// Loads should always be constant-time, even in the face of both load
/// and update contention.
///
/// Updates might take a long time, and the closure passed to it might
/// run multiple times. This is because if the "old" value is updated
/// before the closure finishes, the closure might overwrite up-to-date
/// data and must be run again with said new data passed in. Additionally,
/// memory reclamation of old ArcCell values is performed at this point.
///
/// Sets take much longer than loads as well, but they should be approximately
/// constant-time as they don't need to be re-run if a different thread
/// sets the ArcCell before it can finish.
pub struct ArcCell<T: 'static + Send + Sync> {
    inner: Atomic<T>,
}

impl<T: 'static + Send + Sync> ArcCell<T> {
    /// Create a new ArcCell pointing to `data`.
    ///
    /// # Example
    /// ```
    /// # use crossbeam_arccell::ArcCell;
    /// let arc = ArcCell::new(vec![1,2,3,4]);
    /// ```
    pub fn new(data: T) -> ArcCell<T> {
        ArcCell {
            inner: Atomic::new(data),
        }
    }

    fn update_fallible_inner<F, E>(&self, f: F, reclaim: bool) -> Result<(), E>
    where
        F: Fn(&T) -> Result<T, E>,
    {
        let guard = crossbeam_epoch::pin();
        if reclaim {
            guard.flush();
        }
        loop {
            let shared = self.inner.load(Ordering::Acquire, &guard);
            let data = unsafe { shared.as_ref().unwrap() };
            let t = f(data)?;
            let r = self.inner
                .compare_and_set(shared, Owned::new(t), Ordering::AcqRel, &guard);
            if let Ok(r) = r {
                unsafe { guard.defer(move || r.into_owned()) }
                break;
            }
        }
        Ok(())
    }

    /// Update the ArcCell.
    ///
    /// This is done by passing the current ArcCell value to a closure and
    /// setting the ArcCell to the closure's return value, provided no other
    /// threads have changed the ArcCell in the meantime.
    ///
    /// If you don't care about any other threads setting the ArcCell during
    /// processing, use the `set()` method.
    ///
    /// # Example
    /// ```
    /// # use crossbeam_arccell::ArcCell;
    /// let arc = ArcCell::new(vec![1,2,3,4]);
    /// arc.update(|old| {
    ///     let mut new = old.clone();
    ///     new.push(5);
    ///     new
    /// })
    /// ```
    pub fn update<F>(&self, f: F)
    where
        F: Fn(&T) -> T,
    {
        self.update_fallible_inner(|t| Ok::<T, ()>(f(t)), true).unwrap()
    }
    
    /// Update the ArcCell in a fallible fashion.
    pub fn update_fallible<F, E>(&self, f: F) -> Result<(), E>
    where
        F: Fn(&T) -> Result<T, E>,
    {
        self.update_fallible_inner(f, true)
    }

    /// Update the ArcCell without reclaiming any memory.
    /// Note that without calling reclaim() at some future point, this can cause a memory leak.
    pub fn update_no_reclaim<F>(&self, f: F)
    where
        F: Fn(&T) -> T,
    {
        self.update_fallible_inner(|t| Ok::<T, ()>(f(t)), false).unwrap()
    }

    /// Update the ArcCell in a fallible fashion without reclaiming any memory.
    /// Note that without calling reclaim() at some future point, this can cause a memory leak.
    pub fn update_fallible_no_reclaim<F, E>(&self, f: F) -> Result<(), E>
    where
        F: Fn(&T) -> Result<T, E>,
    {
        self.update_fallible_inner(f, false)
    }

    fn set_inner(&self, data: T, reclaim: bool) {
        let guard = crossbeam_epoch::pin();
        if reclaim {
            guard.flush();
        }
        let r = self.inner.swap(Owned::new(data), Ordering::Release, &guard);
        unsafe { guard.defer(move || r.into_owned()) }
    }

    /// Update the ArcCell, ignoring the current value.
    ///
    /// # Example
    /// ```
    /// # use crossbeam_arccell::ArcCell;
    /// let arc = ArcCell::new(vec![1,2,3,4]);
    /// arc.set(vec![9,8,7,6]);
    /// ```
    pub fn set(&self, data: T) {
        self.set_inner(data, true)
    }

    /// Update the ArcCell, ignoring the current value and not reclaiming any memory.
    /// Note that without calling reclaim() at some future point, this can cause a memory leak.
    pub fn set_no_reclaim(&self, data: T) {
        self.set_inner(data, false)
    }

    /// Reclaim memory after calling `update_no_reclaim()`, `update_fallible_no_reclaim()`  or `set_no_reclaim()`.
    pub fn reclaim(&self) {
        let guard = crossbeam_epoch::pin();
        guard.flush();
    }

    /// Load the current value from the ArcCell.
    ///
    /// This returns an ArcCell guard, rather than returning the
    /// internal value directly. In order to access the value explicitly,
    /// it must be dereferenced.
    ///
    /// # Example
    /// ```
    /// # use crossbeam_arccell::ArcCell;
    /// let arc = ArcCell::new(vec![1,2,3,4]);
    /// let guard = arc.load();
    /// assert_eq!(*guard, vec![1,2,3,4]);
    /// ```
    ///
    /// # Warning
    /// This method returns a guard that will pin the current thread, but
    /// won't directly hold on to a particular value. This means that even
    /// though `load()` has been called, it's not a guarantee that the data
    /// won't change between dereferences. As an example,
    ///
    /// ```
    /// # use crossbeam_arccell::ArcCell;
    /// let arc = ArcCell::new(vec![1,2,3,4]);
    /// let guard = arc.load();
    /// assert_eq!(*guard, vec![1,2,3,4]);
    /// arc.set(vec![9,8,7,6]);
    /// assert_eq!(*guard, vec![9,8,7,6]);
    /// ```
    pub fn load(&self) -> ArcCellGuard<T> {
        ArcCellGuard {
            parent: self,
            inner: crossbeam_epoch::pin(),
        }
    }
}

impl<T: 'static + Send + Sync + fmt::Debug> fmt::Debug for ArcCell<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("StmGuard")
            .field("data", self.load().deref())
            .finish()
    }
}

impl<T: 'static + Send + Sync + fmt::Display> fmt::Display for ArcCell<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.load().deref().fmt(f)
    }
}

impl<T: 'static + Send + Sync> Drop for ArcCell<T> {
    fn drop(&mut self) {
        let guard = crossbeam_epoch::pin();
        let shared = self.inner.load(Ordering::Acquire, &guard);
        unsafe {
            shared.into_owned();
        }
    }
}

impl<T: 'static + Send + Sync> Clone for ArcCell<T> {
    fn clone(&self) -> ArcCell<T> {
        ArcCell {
            inner: self.inner.clone()
        }
    }
}

/// Structure that ensures any loaded data won't be freed by a future update.
///
/// Once this structure is dropped, the memory it dereferences to can be
/// reclaimed.
pub struct ArcCellGuard<'a, T: 'static + Send + Sync> {
    parent: &'a ArcCell<T>,
    inner: crossbeam_epoch::Guard,
}

impl<'a, T: 'static + Send + Sync> Deref for ArcCellGuard<'a, T> {
    type Target = T;
    fn deref(&self) -> &T {
        let shared = self.parent.inner.load(Ordering::Acquire, &self.inner);
        unsafe { shared.as_ref().unwrap() }
    }
}

impl<'a, T: 'static + Send + Sync + fmt::Debug> fmt::Debug for ArcCellGuard<'a, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("StmGuard")
            .field("data", &self.deref())
            .finish()
    }
}

impl<'a, T: 'static + Send + Sync + fmt::Display> fmt::Display for ArcCellGuard<'a, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.deref().fmt(f)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::sync::atomic::{AtomicUsize, Ordering};
    static DROPCOUNTER: AtomicUsize = AtomicUsize::new(0);

    #[test]
    fn arc_test() {
        let arc = ArcCell::new(vec![1, 2, 3]);
        {
            let data = arc.load();
            assert_eq!(*data, vec![1, 2, 3]);
        }

        arc.update(|v| {
            let mut v = v.clone();
            v.push(4);
            v
        });

        {
            let data = arc.load();
            assert_eq!(*data, vec![1, 2, 3, 4]);
        }

        arc.update(|_| vec![1]);

        {
            let data = arc.load();
            assert_eq!(*data, vec![1]);
        }
    }

    #[test]
    fn test_no_leaks() {
        DROPCOUNTER.store(0, Ordering::SeqCst);

        struct DropCounter<'a> {
            r: &'a AtomicUsize,
        }

        impl<'a> Drop for DropCounter<'a> {
            fn drop(&mut self) {
                self.r.fetch_add(1, Ordering::SeqCst);
            }
        }

        drop(ArcCell::new(DropCounter { r: &DROPCOUNTER }));

        // We expect the value to have been dropped exactly once.
        assert_eq!(DROPCOUNTER.load(Ordering::SeqCst), 1);
    }
}