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
//! AtomicStampedPtr

#![feature(asm)]
#![feature(test)]
#![feature(heap_api)]
#![feature(alloc)]

extern crate alloc;
extern crate test;

use std::default::Default;
use alloc::heap::{allocate, deallocate};

/// Main class
#[derive(Debug)]
pub struct AtomicStampedPtr<T> {
    p: *mut PtrContainer<T>,
}

unsafe impl<T> Send for AtomicStampedPtr<T> {}
unsafe impl<T> Sync for AtomicStampedPtr<T> {}

impl<T> std::cmp::PartialEq for AtomicStampedPtr<T> {
    fn eq(&self, other: &Self) -> bool {
        self.p == other.p
    }
}

/// Helper class
#[derive(Debug)]
#[repr(packed)]
struct PtrContainer<T> {
    ptr: *mut T,
    version: usize,
}

impl<T> std::cmp::PartialEq for PtrContainer<T> {
    fn eq(&self, other: &Self) -> bool {
        self.ptr == other.ptr && self.version == other.version
    }
}

impl<T> PtrContainer<T> {
    fn new(ptr: *mut T, version: usize) -> Self {
        PtrContainer {
            ptr: ptr,
            version: version,
        }
    }

    fn successor(&self) -> usize {
        if self.version == std::usize::MAX {
            0
        } else {
            self.version + 1
        }
    }
}

impl<T> Default for PtrContainer<T> {
    fn default() -> Self {
        PtrContainer::new(std::ptr::null_mut(), 0)
    }
}

#[cfg(target_arch = "x86_64")]
fn cas_ptr<T>(src: *mut PtrContainer<T>, cmp: &mut PtrContainer<T>, with: PtrContainer<T>) -> bool {
    let result: bool;
    unsafe {
        asm!("
                lock cmpxchg16b $1
                setz $0
            "
             : "=q"(result), "+*m"(src), "+{rdx}"(cmp.version), "+{rax}"(cmp.ptr)
             : "{rcx}"(with.version), "{rbx}"(with.ptr)
             : "memory" "cc"
        );
    }
    result
}
 
impl<T> Default for AtomicStampedPtr<T> {
    fn default() -> Self {
        AtomicStampedPtr::new(std::ptr::null_mut())
    }
}

impl<T> Drop for AtomicStampedPtr<T> {
    fn drop(&mut self) {
        if !self.p.is_null() {
            unsafe { deallocate(self.p as *mut u8, std::mem::size_of::<PtrContainer<T>>(), 16); }
        }
    }
}

impl<T> AtomicStampedPtr<T> {
    /// Constructor
    pub fn new(p: *mut T) -> Self {
        unsafe {
            let size = std::mem::size_of::<PtrContainer<T>>();
            let ptr = allocate(size, 16);
            std::intrinsics::copy(&PtrContainer::new(p, 0) as *const PtrContainer<T> as *const u8, ptr, size);
            AtomicStampedPtr {
                p: ptr as *mut PtrContainer<T>,
            }
        }
    }

    /// Load inner ptr atomiclly
    /// 
    /// # Examples
    /// 
    /// ```
    /// use atomic_stamped_ptr::AtomicStampedPtr;
    ///
    /// let p = Box::into_raw(Box::new(0));
    /// let a = AtomicStampedPtr::new(p);
    /// assert_eq!(a.load(), (p, 0));
    /// ```
    pub fn load(&self) -> (*mut T, usize) {
        let mut current = PtrContainer::default();
        cas_ptr(self.p, &mut current, PtrContainer::default());
        (current.ptr, current.version)
    }

    /// Store a ptr into AtomicStampedPtr and set version to 0.
    ///
    /// # Examples
    ///
    /// ```
    /// use atomic_stamped_ptr::AtomicStampedPtr;
    ///
    /// let a = AtomicStampedPtr::default();
    /// let p = Box::into_raw(Box::new(233));
    /// a.store(p);
    /// assert_eq!(a.load(), (p, 1));
    /// ```
    pub fn store(&self, ptr: *mut T) {
        self.swap(ptr);
    }

    /// Store a ptr into AtomicStampedPtr and return the old one.
    ///
    /// # Examples
    ///
    /// ```
    /// use atomic_stamped_ptr::AtomicStampedPtr;
    ///
    /// let p1 = Box::into_raw(Box::new(1));
    /// let p2 = Box::into_raw(Box::new(2));
    /// let a = AtomicStampedPtr::new(p1);
    /// assert_eq!(a.swap(p2), p1);
    /// assert_eq!(a.load(), (p2, 1));
    /// ```  
    pub fn swap(&self, ptr: *mut T) -> *mut T {
        let mut current = PtrContainer::default();
        let mut successor = current.successor();
        while !cas_ptr(self.p, &mut current, PtrContainer::new(ptr, successor)) {
            successor = current.successor();
        }
        current.ptr
    }

    /// Store a ptr into AtomicStampedPtr and return current value.
    ///
    /// The return value is always the previous value. If it is equal to `current`, then the value
    /// was update.
    ///
    /// # Example
    ///
    /// ```
    /// use atomic_stamped_ptr::AtomicStampedPtr;
    ///
    /// let p1 = Box::into_raw(Box::new(1));
    /// let p2 = Box::into_raw(Box::new(2));
    /// let a = AtomicStampedPtr::new(p1);
    /// assert_eq!(a.compare_and_swap((p1, 0), p2), (p1, 0));
    /// assert_eq!(a.load(), (p2, 1));
    /// ```
    pub fn compare_and_swap(&self, current: (*mut T, usize), ptr: *mut T) -> (*mut T, usize) {
        let mut current = PtrContainer::new(current.0, current.1);
        let relief = PtrContainer::new(ptr, current.successor());
        cas_ptr(self.p, &mut current, relief);
        (current.ptr, current.version)
    }

    /// Stores a value into AtomicStampedPtr if the current value is the same as the `current` value.
    ///
    /// The return value is a result indicating whether the new value was written and containing
    /// the previous value. On success this value is guaranteed to be equal to `current`.
    ///
    /// # Examples
    ///
    /// ```
    /// use atomic_stamped_ptr::AtomicStampedPtr;
    ///
    /// let p = Box::into_raw(Box::new(0));
    /// let a = AtomicStampedPtr::new(p);
    /// assert_eq!(a.compare_exchange((p, 1), p), Err((p, 0)));
    /// assert_eq!(a.compare_exchange((p, 0), p), Ok((p, 0)));
    /// ```
    pub fn compare_exchange(&self, current: (*mut T, usize), ptr: *mut T) -> Result<(*mut T, usize), (*mut T, usize)> {
        let mut current = PtrContainer::new(current.0, current.1);
        let relief = PtrContainer::new(ptr, current.successor());
        if cas_ptr(self.p, &mut current, relief) {
            Ok((current.ptr, current.version))
        } else {
            Err((current.ptr, current.version))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{PtrContainer, AtomicStampedPtr, cas_ptr};
    use std::mem;
    use std::marker::Sync;
    use std::sync::Arc;
    use test::Bencher;

    struct T(i32);
    unsafe impl Sync for T {}

    #[test]
    fn test_cas_failure() {
        let a = PtrContainer{ptr: 233 as *mut i32, version: 0};
        let mut b = PtrContainer::<i32>::default();
        let c = PtrContainer::default();
        assert_eq!(cas_ptr(unsafe{mem::transmute(&a)}, &mut b, c), false);
        assert_eq!(a, b);
    }

    #[test]
    fn test_swap() {
        let a = Arc::new(AtomicStampedPtr::new(0 as *mut T));
        let threads = (1..10).map(|x| {
            let a = a.clone();
            ::std::thread::spawn(move || {
                let mut p = a.swap(x as *mut T);
                while p as usize != x - 1 {
                    p = a.swap(p);
                }
            })
        }).collect::<Vec<_>>();

        for t in threads {
            t.join().unwrap();
        }
    }

    #[test]
    fn test_compare_exchange() {
        let a = Arc::new(AtomicStampedPtr::new(0 as *mut T));
        let threads = (0..1000).map(|_| {
            let a = a.clone();
            ::std::thread::spawn(move || {
                let mut v = 0;
                while let Err((_, e)) = a.compare_exchange((0 as *mut T, v), 0 as *mut T) {
                    v = e;
                }
            })
        }).collect::<Vec<_>>();

        for t in threads {
            t.join().unwrap();
        }

        assert_eq!(a.load(), (0 as *mut T, 1000));
    }

    #[bench]
    fn bench_compare_exchange(r: &mut Bencher) {
        let a = Arc::new(AtomicStampedPtr::new(0 as *mut T));
        r.iter(|| {
            let _ = a.compare_exchange((0 as *mut T, 0), 0 as *mut T);
        });
    }
}