Move the parent assignation before the exchange operation

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

View File

@ -23,15 +23,13 @@ impl<T> AppendOnlyLinkedList<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, parent: 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);
match self.head.compare_exchange_weak(head, node, SeqCst, Relaxed) { match self.head.compare_exchange_weak(head, node, SeqCst, Relaxed) {
Ok(parent) => { Ok(_) => break,
node.parent = AtomicPtr::new(parent);
break;
}
Err(new) => head = new, Err(new) => head = new,
} }
} }