-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathharris_list.rs
381 lines (345 loc) · 11.2 KB
/
harris_list.rs
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//! An implementation of Harris's List with HP++
#![feature(strict_provenance_atomic_ptr)]
use std::{
mem, ptr, slice,
sync::atomic::{AtomicPtr, Ordering},
thread::scope,
};
use hp_pp::*;
// `#[repr(C)]` is used to ensure the first field
// is also the first data in the memory alignment.
#[repr(C)]
pub struct Node<K, V> {
/// tag 1: logically deleted, tag 2: invalidated
next: AtomicPtr<Node<K, V>>,
key: K,
value: V,
}
pub struct List<K, V> {
head: AtomicPtr<Node<K, V>>,
}
impl<K, V> Drop for List<K, V> {
fn drop(&mut self) {
let mut curr = untagged(*self.head.get_mut());
while !curr.is_null() {
curr = untagged(*unsafe { Box::from_raw(curr) }.next.get_mut());
}
}
}
pub struct Handle<'domain> {
prev_h: HazardPointer<'domain>,
curr_h: HazardPointer<'domain>,
anchor_h: HazardPointer<'domain>,
anchor_next_h: HazardPointer<'domain>,
}
impl Default for Handle<'static> {
fn default() -> Self {
Self {
prev_h: HazardPointer::default(),
curr_h: HazardPointer::default(),
anchor_h: HazardPointer::default(),
anchor_next_h: HazardPointer::default(),
}
}
}
impl<'domain> Handle<'domain> {
// bypass E0499-E0503, etc that are supposed to be fixed by polonius
#[inline]
fn launder<'hp1, 'hp2>(&'hp1 mut self) -> &'hp2 mut Self {
unsafe { mem::transmute(self) }
}
}
/// `Cursor` is safe to dereference its members
/// only it the underlying hazard pointers are present.
pub struct Cursor<'domain, 'hp, K, V> {
prev: *mut Node<K, V>,
curr: *mut Node<K, V>,
// Anchor is the last node that was not logically deleted, and anchor_next is anchor's next
// node at that moment. These are used for unlinking the chain of logically deleted nodes. They
// are non-null iff they exist.
anchor: *mut Node<K, V>,
anchor_next: *mut Node<K, V>,
handle: &'hp mut Handle<'domain>,
}
impl<'domain, 'hp, K, V> Cursor<'domain, 'hp, K, V> {
pub fn new(head: &AtomicPtr<Node<K, V>>, handle: &'hp mut Handle<'domain>) -> Self {
Self {
prev: head as *const _ as *mut _,
curr: head.load(Ordering::Acquire),
anchor: ptr::null_mut(),
anchor_next: ptr::null_mut(),
handle,
}
}
}
impl<K, V> hp_pp::Invalidate for Node<K, V> {
fn invalidate(&self) {
let next = self.next.load(Ordering::Acquire);
self.next.store(tagged(next, 1 | 2), Ordering::Release);
}
}
/// Physical unlink in the traverse function of Harris's list
struct HarrisUnlink<'c, 'domain, 'hp, K, V> {
cursor: &'c Cursor<'domain, 'hp, K, V>,
}
impl<'r, 'domain, 'hp, K, V> hp_pp::Unlink<Node<K, V>> for HarrisUnlink<'r, 'domain, 'hp, K, V> {
fn do_unlink(&self) -> Result<Vec<*mut Node<K, V>>, ()> {
if unsafe { &*self.cursor.anchor }
.next
.compare_exchange(
self.cursor.anchor_next,
self.cursor.curr,
Ordering::AcqRel,
Ordering::Relaxed,
)
.is_ok()
{
let mut collected = Vec::with_capacity(16);
let mut node = self.cursor.anchor_next;
loop {
if untagged(node) == self.cursor.curr {
break;
}
let node_ref = unsafe { node.as_ref() }.unwrap();
let next_base = untagged(node_ref.next.load(Ordering::Relaxed));
collected.push(node);
node = next_base;
}
Ok(collected)
} else {
Err(())
}
}
}
struct RemoveUnlink<'c, 'domain, 'hp, K, V> {
cursor: &'c Cursor<'domain, 'hp, K, V>,
next_base: *mut Node<K, V>,
}
impl<'r, 'domain, 'hp, K, V> hp_pp::Unlink<Node<K, V>> for RemoveUnlink<'r, 'domain, 'hp, K, V> {
fn do_unlink(&self) -> Result<Vec<*mut Node<K, V>>, ()> {
let prev = unsafe { &(*self.cursor.prev).next };
if prev
.compare_exchange(
self.cursor.curr,
self.next_base,
Ordering::Release,
Ordering::Relaxed,
)
.is_ok()
{
Ok(vec![self.cursor.curr])
} else {
Err(())
}
}
}
impl<'domain, 'hp, K, V> Cursor<'domain, 'hp, K, V>
where
K: Ord,
{
/// Clean up a chain of logically removed nodes in each traversal.
#[inline]
fn try_search(&mut self, key: &K) -> Result<bool, ()> {
let found = loop {
if self.curr.is_null() {
break false;
}
if self
.handle
.curr_h
.try_protect_pp(
self.curr,
unsafe { &*self.prev },
&unsafe { &*self.prev }.next,
&|node| node.next.load(Ordering::Acquire) as usize & 2 == 2,
)
.is_err()
{
return Err(());
}
let curr_node = unsafe { &*self.curr };
let (next_base, next_tag) = decompose_ptr(curr_node.next.load(Ordering::Acquire));
if next_tag == 0 {
if curr_node.key < *key {
self.prev = self.curr;
self.curr = next_base;
self.anchor = ptr::null_mut();
HazardPointer::swap(&mut self.handle.curr_h, &mut self.handle.prev_h);
} else {
break curr_node.key == *key;
}
} else {
if self.anchor.is_null() {
self.anchor = self.prev;
self.anchor_next = self.curr;
HazardPointer::swap(&mut self.handle.anchor_h, &mut self.handle.prev_h);
} else if self.anchor_next == self.prev {
HazardPointer::swap(&mut self.handle.anchor_next_h, &mut self.handle.prev_h);
}
self.prev = self.curr;
self.curr = next_base;
HazardPointer::swap(&mut self.handle.prev_h, &mut self.handle.curr_h);
}
};
if self.anchor.is_null() {
Ok(found)
} else {
let unlink = HarrisUnlink { cursor: &self };
if unsafe { try_unlink(unlink, slice::from_ref(&self.curr)) } {
self.prev = self.anchor;
Ok(found)
} else {
Err(())
}
}
}
}
impl<K, V> List<K, V>
where
K: Ord,
{
/// Creates a new list.
pub fn new() -> Self {
List {
head: AtomicPtr::new(ptr::null_mut()),
}
}
#[inline]
pub fn get<'domain, 'hp>(&self, key: &K, handle: &'hp mut Handle<'domain>) -> Option<&'hp V> {
loop {
let mut cursor = Cursor::new(&self.head, handle.launder());
match cursor.try_search(key) {
Ok(true) => return unsafe { Some(&((*cursor.curr).value)) },
Ok(false) => return None,
Err(_) => continue,
}
}
}
fn insert_inner<'domain, 'hp>(
&self,
node: *mut Node<K, V>,
handle: &'hp mut Handle<'domain>,
) -> Result<bool, ()> {
loop {
let mut cursor = Cursor::new(&self.head, handle.launder());
let found = cursor.try_search(unsafe { &(*node).key })?;
if found {
drop(unsafe { Box::from_raw(node) });
return Ok(false);
}
unsafe { &*node }.next.store(cursor.curr, Ordering::Relaxed);
if unsafe { &*cursor.prev }
.next
.compare_exchange(cursor.curr, node, Ordering::Release, Ordering::Relaxed)
.is_ok()
{
return Ok(true);
}
}
}
#[inline]
pub fn insert<'domain, 'hp>(&self, key: K, value: V, handle: &'hp mut Handle<'domain>) -> bool {
let node = Box::into_raw(Box::new(Node {
key,
value,
next: AtomicPtr::new(ptr::null_mut()),
}));
loop {
match self.insert_inner(node, handle.launder()) {
Ok(r) => return r,
Err(()) => continue,
}
}
}
fn remove_inner<'domain, 'hp>(
&self,
key: &K,
handle: &'hp mut Handle<'domain>,
) -> Result<Option<&'hp V>, ()> {
loop {
let mut cursor = Cursor::new(&self.head, handle.launder());
let found = cursor.try_search(key)?;
if !found {
return Ok(None);
}
let curr_node = unsafe { &*cursor.curr };
let next = curr_node.next.fetch_or(1, Ordering::AcqRel);
let next_tag = tag(next);
if next_tag == 1 {
continue;
}
let links = slice::from_ref(&next);
let unlink = RemoveUnlink {
cursor: &cursor,
next_base: next,
};
unsafe { try_unlink(unlink, links) };
return Ok(Some(&curr_node.value));
}
}
#[inline]
pub fn remove<'domain, 'hp>(
&self,
key: &K,
handle: &'hp mut Handle<'domain>,
) -> Option<&'hp V> {
loop {
match self.remove_inner(key, handle.launder()) {
Ok(r) => return r,
Err(_) => continue,
}
}
}
pub fn handle() -> Handle<'static> {
Handle::default()
}
}
#[test]
fn smoke_harris() {
const THREADS: i32 = 30;
const ELEMENTS_PER_THREADS: i32 = 1000;
use rand::prelude::*;
let map = &List::new();
scope(|s| {
for t in 0..THREADS {
s.spawn(move || {
let mut handle = List::<i32, String>::handle();
let mut rng = rand::thread_rng();
let mut keys: Vec<i32> =
(0..ELEMENTS_PER_THREADS).map(|k| k * THREADS + t).collect();
keys.shuffle(&mut rng);
for i in keys {
assert!(map.insert(i, i.to_string(), &mut handle));
}
});
}
});
scope(|s| {
for t in 0..(THREADS / 2) {
s.spawn(move || {
let mut handle = List::<i32, String>::handle();
let mut rng = rand::thread_rng();
let mut keys: Vec<i32> =
(0..ELEMENTS_PER_THREADS).map(|k| k * THREADS + t).collect();
keys.shuffle(&mut rng);
for i in keys {
assert_eq!(i.to_string(), *map.remove(&i, &mut handle).unwrap());
}
});
}
});
scope(|s| {
for t in (THREADS / 2)..THREADS {
s.spawn(move || {
let mut handle = List::<i32, String>::handle();
let mut rng = rand::thread_rng();
let mut keys: Vec<i32> =
(0..ELEMENTS_PER_THREADS).map(|k| k * THREADS + t).collect();
keys.shuffle(&mut rng);
for i in keys {
assert_eq!(i.to_string(), *map.get(&i, &mut handle).unwrap());
}
});
}
});
}