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
use std::mem;
use std::ptr;
use std::marker::PhantomData;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::{self, AcqRel, Acquire, Release};
use epoch::Pin;
fn low_bits<T>() -> usize {
(1 << mem::align_of::<T>().trailing_zeros()) - 1
}
fn raw_and_tag<T>(raw: *mut T, tag: usize) -> usize {
let mask = low_bits::<T>();
assert!(raw as usize & mask == 0, "unaligned pointer");
assert!(tag <= mask, "tag too large to fit into the unused bits: {} > {}", tag, mask);
raw as usize | tag
}
#[derive(Debug)]
pub struct Atomic<T> {
data: AtomicUsize,
_marker: PhantomData<*mut T>,
}
unsafe impl<T: Send + Sync> Send for Atomic<T> {}
unsafe impl<T: Send + Sync> Sync for Atomic<T> {}
impl<T> Atomic<T> {
unsafe fn from_data(data: usize) -> Self {
Atomic {
data: AtomicUsize::new(data),
_marker: PhantomData,
}
}
pub fn null(tag: usize) -> Self {
unsafe { Self::from_raw(ptr::null_mut(), tag) }
}
pub fn new(data: T, tag: usize) -> Self {
unsafe { Self::from_raw(Box::into_raw(Box::new(data)), tag) }
}
pub fn from_ptr(ptr: Ptr<T>) -> Self {
unsafe { Self::from_data(ptr.data) }
}
pub fn from_box(b: Box<T>, tag: usize) -> Self {
unsafe { Self::from_raw(Box::into_raw(b), tag) }
}
pub unsafe fn from_raw(raw: *mut T, tag: usize) -> Self {
Self::from_data(raw_and_tag(raw, tag))
}
pub fn load<'p>(&self, _: &'p Pin) -> Ptr<'p, T> {
unsafe { Ptr::from_data(self.data.load(Acquire)) }
}
pub fn load_raw(&self, order: Ordering) -> (*mut T, usize) {
let p = unsafe { Ptr::<T>::from_data(self.data.load(order)) };
(p.as_raw(), p.tag())
}
pub fn store<'p>(&self, new: Ptr<'p, T>) {
self.data.store(new.data, Release);
}
pub fn store_box<'p>(&self, new: Box<T>, tag: usize, _: &'p Pin) -> Ptr<'p, T> {
let ptr = unsafe { Ptr::from_raw(Box::into_raw(new), tag) };
self.data.store(ptr.data, Release);
ptr
}
pub fn store_raw<'p>(&self, new: *mut T, tag: usize, order: Ordering, _: &'p Pin)
-> Ptr<'p, T> {
let ptr = unsafe { Ptr::from_raw(new, tag) };
self.data.store(ptr.data, order);
ptr
}
pub fn swap<'p>(&self, new: Ptr<'p, T>) -> Ptr<'p, T> {
unsafe { Ptr::from_data(self.data.swap(new.data, AcqRel)) }
}
pub fn swap_box<'p>(&self, new: Box<T>, tag: usize, _: &'p Pin) -> Ptr<'p, T> {
let data = unsafe { Ptr::from_raw(Box::into_raw(new), tag).data };
unsafe { Ptr::from_data(self.data.swap(data, AcqRel)) }
}
pub unsafe fn swap_raw<'p>(&self, new: *mut T, tag: usize, order: Ordering) -> Ptr<'p, T> {
let data = Ptr::from_raw(new, tag).data;
Ptr::from_data(self.data.swap(data, order))
}
pub fn cas<'p>(&self, current: Ptr<'p, T>, new: Ptr<'p, T>) -> Result<(), Ptr<'p, T>> {
let previous = self.data.compare_and_swap(current.data, new.data, AcqRel);
if previous == current.data {
Ok(())
} else {
unsafe { Err(Ptr::from_data(previous)) }
}
}
pub fn cas_box<'p>(&self, current: Ptr<'p, T>, mut new: Box<T>, tag: usize)
-> Result<Ptr<'p, T>, (Ptr<'p, T>, Box<T>)> {
let new_data = raw_and_tag(new.as_mut(), tag);
let previous = self.data.compare_and_swap(current.data, new_data, AcqRel);
if previous == current.data {
mem::forget(new);
unsafe { Ok(Ptr::from_data(new_data)) }
} else {
unsafe { Err((Ptr::from_data(previous), new)) }
}
}
pub unsafe fn cas_raw(&self, current: (*mut T, usize), new: (*mut T, usize), order: Ordering)
-> Result<(), (*mut T, usize)> {
let current_data = raw_and_tag(current.0, current.1);
let new_data = raw_and_tag(new.0, new.1);
let previous = self.data.compare_and_swap(current_data, new_data, order);
if previous == current_data {
Ok(())
} else {
let ptr = Ptr::from_data(previous);
Err((ptr.as_raw(), ptr.tag()))
}
}
}
impl<T> Default for Atomic<T> {
fn default() -> Self {
Atomic {
data: AtomicUsize::new(0),
_marker: PhantomData,
}
}
}
#[derive(Debug)]
pub struct Ptr<'p, T: 'p> {
data: usize,
_marker: PhantomData<(*mut T, &'p T)>,
}
impl<'a, T> Clone for Ptr<'a, T> {
fn clone(&self) -> Self {
Ptr {
data: self.data,
_marker: PhantomData,
}
}
}
impl<'a, T> Copy for Ptr<'a, T> {}
impl<'p, T: 'p> Ptr<'p, T> {
unsafe fn from_data(data: usize) -> Self {
Ptr {
data: data,
_marker: PhantomData,
}
}
pub fn null(tag: usize) -> Self {
unsafe { Self::from_data(raw_and_tag::<T>(ptr::null_mut(), tag)) }
}
pub unsafe fn from_raw(raw: *mut T, tag: usize) -> Self {
Self::from_data(raw_and_tag(raw, tag))
}
pub fn is_null(&self) -> bool {
self.as_raw().is_null()
}
pub fn as_ref(&self) -> Option<&'p T> {
unsafe { self.as_raw().as_ref() }
}
pub fn as_raw(&self) -> *mut T {
(self.data & !low_bits::<T>()) as *mut T
}
pub fn unwrap(&self) -> &'p T {
self.as_ref().unwrap()
}
pub fn tag(&self) -> usize {
self.data & low_bits::<T>()
}
pub fn with_tag(&self, tag: usize) -> Self {
unsafe { Self::from_raw(self.as_raw(), tag) }
}
}
impl<'p, T> Default for Ptr<'p, T> {
fn default() -> Self {
Ptr {
data: 0,
_marker: PhantomData,
}
}
}