Rename Node parent to next

This commit is contained in:
Clément Renault 2024-10-03 16:15:05 +02:00
parent 4665bfcb19
commit 58d96fbea3
No known key found for this signature in database
GPG Key ID: F250A4C4E3AE5F5F

View File

@ -8,7 +8,7 @@ pub struct AppendOnlyLinkedList<T> {
struct Node<T> { struct Node<T> {
item: T, item: T,
parent: AtomicPtr<Node<T>>, next: AtomicPtr<Node<T>>,
} }
impl<T> AppendOnlyLinkedList<T> { impl<T> AppendOnlyLinkedList<T> {
@ -22,12 +22,12 @@ impl<T> AppendOnlyLinkedList<T> {
pub fn push(&self, item: T) -> &mut T { pub fn push(&self, item: T) -> &mut T {
use std::sync::atomic::Ordering::{Relaxed, SeqCst}; use std::sync::atomic::Ordering::{Relaxed, SeqCst};
let node = Box::leak(Box::new(Node { item, parent: AtomicPtr::default() })); let node = Box::leak(Box::new(Node { item, next: AtomicPtr::default() }));
let mut head = self.head.load(SeqCst); let mut head = self.head.load(SeqCst);
loop { loop {
std::hint::spin_loop(); std::hint::spin_loop();
node.parent = AtomicPtr::new(head); node.next = AtomicPtr::new(head);
match self.head.compare_exchange_weak(head, node, SeqCst, Relaxed) { match self.head.compare_exchange_weak(head, node, SeqCst, Relaxed) {
Ok(_) => break, Ok(_) => break,
Err(new) => head = new, Err(new) => head = new,
@ -77,8 +77,8 @@ impl<T> Iterator for IntoIter<T> {
None None
} else { } else {
let node = unsafe { Box::from_raw(ptr) }; let node = unsafe { Box::from_raw(ptr) };
// Let's set the next node to read to be the parent of this one // Let's set the next node to read to be the next of this one
self.0 = node.parent; self.0 = node.next;
Some(node.item) Some(node.item)
} }
} }
@ -89,8 +89,8 @@ impl<T> Drop for IntoIter<T> {
let mut ptr = *self.0.get_mut(); let mut ptr = *self.0.get_mut();
while !ptr.is_null() { while !ptr.is_null() {
let mut node = unsafe { Box::from_raw(ptr) }; let mut node = unsafe { Box::from_raw(ptr) };
// Let's set the next node to read to be the parent of this one // Let's set the next node to read to be the next of this one
ptr = *node.parent.get_mut(); ptr = *node.next.get_mut();
} }
} }
} }