Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ sudo cargo flamegraph --no-default-features --test tests -- {test_name}
- Michael-Scott queue

### Linked List
- TODO: implement Harris linked list
- Harris List

### AVL Tree
- SeqLockAVLTree, RwLockAVLTree(use crossbeam_utils::sync::ShardedLock)
Expand All @@ -76,6 +76,9 @@ sudo cargo flamegraph --no-default-features --test tests -- {test_name}
### Queue
- two lock queue, Michael-Scott Queue: https://www.cs.rochester.edu/~scott/papers/1996_PODC_queues.pdf

### List
- Harris List: https://www.cl.cam.ac.uk/research/srg/netos/papers/2001-caslists.pdf

### Binary Search Tree
- AVL Tree: https://stanford-ppl.github.io/website/papers/ppopp207-bronson.pdf
- B+ Tree: http://www.vldb.org/pvldb/vol4/p795-sewall.pdf
Expand Down
174 changes: 174 additions & 0 deletions src/linkedlist/harris.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
use std::{mem::MaybeUninit, ptr, sync::atomic::Ordering};

use crossbeam_epoch::{pin, Atomic, Guard, Owned, Shared};

use crate::map::ConcurrentMap;

struct Node<K, V> {
key: MaybeUninit<K>,
value: MaybeUninit<V>,
next: Atomic<Node<K, V>>,
}

impl<K, V> Node<K, V> {
fn uninit() -> Self {
Self {
key: MaybeUninit::uninit(),
value: MaybeUninit::uninit(),
next: Atomic::null(),
}
}

fn new(key: K, value: V) -> Self {
Self {
key: MaybeUninit::new(key),
value: MaybeUninit::new(value),
next: Atomic::null(),
}
}
}

struct HarrisList<K, V> {
head: Atomic<Node<K, V>>,
}

impl<K, V> HarrisList<K, V>
where
K: Eq + PartialOrd,
{
/// search left.key <= key < right.key and return (left ptr, right)
fn search<'g>(
&'g self,
key: &K,
guard: &'g Guard,
) -> (&'g Atomic<Node<K, V>>, Shared<Node<K, V>>) {
unsafe {
let mut left = &self.head;

loop {
let left_ref = left.load(Ordering::Acquire, guard).deref();
let right_ref = left_ref.next.load(Ordering::Acquire, guard);

if left_ref.key.assume_init_ref() <= key {
if right_ref.is_null() || (key < right_ref.deref().key.assume_init_ref()) {
return (left, right_ref);
}
}

left = &left_ref.next;
}
}
}
}

impl<K, V> ConcurrentMap<K, V> for HarrisList<K, V>
where
K: Eq + PartialOrd + Clone,
{
fn new() -> Self {
Self {
head: Atomic::new(Node::uninit()), // dummy node
}
}

fn insert(&self, key: &K, value: V) -> Result<(), V> {
let guard = pin();

let mut node = Owned::new(Node::new(key.clone(), value));

unsafe {
loop {
let (left, right) = self.search(key, &guard);

let left_ref = left.load(Ordering::Relaxed, &guard);

if left_ref.deref().key.assume_init_ref() == key {
return Err(ptr::read(&node.value).assume_init());
}

node.next.store(right, Ordering::Relaxed);

match left.compare_exchange(
left_ref,
node,
Ordering::Release,
Ordering::Relaxed,
&guard,
) {
Ok(_) => return Ok(()),
Err(e) => node = e.new,
}
}
}
}

fn lookup<F, R>(&self, key: &K, f: F) -> R
where
F: FnOnce(Option<&V>) -> R,
{
let guard = pin();

unsafe {
let (left, _) = self.search(key, &guard);
let left_ref = left.load(Ordering::Relaxed, &guard).deref();

let value = if key == left_ref.key.assume_init_ref() {
Some(left_ref.value.assume_init_ref())
} else {
None
};

f(value)
}
}

fn get(&self, key: &K) -> Option<V>
where
V: Clone,
{
let guard = pin();

unsafe {
let (left, _) = self.search(key, &guard);
let left_ref = left.load(Ordering::Relaxed, &guard).deref();

if key == left_ref.key.assume_init_ref() {
Some(left_ref.value.assume_init_ref().clone())
} else {
None
}
}
}

fn remove(&self, key: &K) -> Result<V, ()> {
let guard = pin();

unsafe {
loop {
let (left, right) = self.search(key, &guard);

let left_ref = left.load(Ordering::Relaxed, &guard);

if left_ref.deref().key.assume_init_ref() != key {
return Err(());
}

if left
.compare_exchange(
left_ref,
right,
Ordering::Relaxed,
Ordering::Relaxed,
&guard,
)
.is_ok()
{
let value = ptr::read(&left_ref.deref().value).assume_init();
guard.defer_destroy(left_ref);

return Ok(value);
}
}
}
}
}
86 changes: 65 additions & 21 deletions src/linkedlist/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
use crate::map::SequentialMap;
mod harris;

// simple sequential linked list
pub struct LinkedList<K, V> {
head: Node<K, V>, // dummy node with key = Default, but the key is not considered on algorithm
}
use std::{fmt::Debug, mem};

use crate::map::SequentialMap;

struct Node<K, V> {
key: K,
value: V,
next: Option<Box<Node<K, V>>>,
}

impl<K: Debug, V: Debug> Debug for Node<K, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Node")
.field("key", &self.key)
.field("value", &self.value)
.field("next", &self.next)
.finish()
}
}

impl<K: Default, V: Default> Default for Node<K, V> {
fn default() -> Self {
Self::new(K::default(), V::default())
Expand All @@ -27,35 +36,70 @@ impl<K, V> Node<K, V> {
}
}

impl<K, V> SequentialMap<K, V> for LinkedList<K, V>
// simple sequential sorted linked list
pub struct SortedList<K, V> {
head: Node<K, V>, // dummy node with key = Default, but the key is not considered on algorithm
}

impl<K: Debug, V: Debug> Debug for SortedList<K, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SortedList")
.field("head", &self.head)
.finish()
}
}

impl<K, V> SortedList<K, V> {
pub fn keys(&self) -> Vec<&K> {
let mut result = Vec::new();

let mut node = &self.head.next;

while let Some(inner) = node {
result.push(&inner.key);
node = &inner.next;
}

result
}
}

impl<K, V> SequentialMap<K, V> for SortedList<K, V>
where
K: Default + Eq + Clone,
K: Default + Eq + PartialOrd + Clone,
V: Default,
{
fn new() -> LinkedList<K, V> {
LinkedList {
fn new() -> SortedList<K, V> {
SortedList {
head: Node::default(),
}
}

fn insert(&mut self, key: &K, value: V) -> Result<(), V> {
let new = Box::new(Node::new(key.clone(), value));
let mut new = Box::new(Node::new(key.clone(), value));

let mut current = &mut self.head.next;
let mut current = &mut self.head;

loop {
match current {
Some(node) => {
if node.key == *key {
return Err(new.value);
}
let next = &mut current.next;

if next.is_some() {
let next_key = &next.as_ref().unwrap().key;

if next_key == key {
return Err(new.value);
} else if next_key > key {
// insert for sorted keys
mem::swap(next, &mut new.next);
let _ = mem::replace(next, Some(new));

current = &mut node.next;
}
None => {
*current = Some(new);
return Ok(());
}

current = next.as_mut().unwrap();
} else {
*next = Some(new);
return Ok(());
}
}
}
Expand Down Expand Up @@ -100,7 +144,7 @@ where
}
}

impl<K, V> Drop for LinkedList<K, V> {
impl<K, V> Drop for SortedList<K, V> {
fn drop(&mut self) {
let mut node = self.head.next.take();

Expand Down
29 changes: 24 additions & 5 deletions tests/linkedlist/linkedlist.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::util::map::stress_sequential;
use cds::linkedlist::LinkedList;
use cds::linkedlist::SortedList;
use cds::map::SequentialMap;
use rand::{thread_rng, Rng};

#[test]
fn test_linkedlist() {
let mut list: LinkedList<i32, i32> = LinkedList::new();
fn test_sorted_list() {
let mut list: SortedList<i32, i32> = SortedList::new();

assert_eq!(list.lookup(&1), None);

Expand Down Expand Up @@ -40,6 +41,24 @@ fn test_linkedlist() {
}

#[test]
fn stress_linkedlist() {
stress_sequential::<String, LinkedList<_, _>>(100_000);
fn test_sorted_list_is_sorted() {
let mut list: SortedList<i32, i32> = SortedList::new();

let mut rng = thread_rng();

for _ in 0..100 {
let val = rng.gen_range(0..39393939);
assert_eq!(list.insert(&val, val), Ok(()));
}

list.keys()
.windows(2)
.enumerate()
.map(|(i, n)| assert!(n[0] < n[1], "{} < {} on {}", n[0], n[1], i))
.nth(98);
}

#[test]
fn stress_sorted_list() {
stress_sequential::<String, SortedList<_, _>>(100_000);
}