Skip to content

Commit

Permalink
fixed small bug with prev ptr in linked list test
Browse files Browse the repository at this point in the history
  • Loading branch information
wyang5 committed Mar 4, 2024
1 parent 08b7930 commit dd10f28
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ jobs:
steps:
- uses: actions/checkout@v4
- run: rustup +nightly component add miri
- run: cargo +nightly miri test
- run: cargo +nightly miri test -- --test-threads=1
20 changes: 13 additions & 7 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,36 @@ fn test_sorted_doubly_linked_list() {

impl LinkedList {
fn insert_sorted(&self, val: usize) {
let mut curr_arc = self.head.clone();
let mut next = curr_arc.next.load(SeqCst);
let mut curr_node = self.head.clone();
let mut next = curr_node.next.load(SeqCst);
loop {
if next.is_none() || val < next.as_ref().unwrap().val {
let new = Arc::new(ListNode {
val,
prev: AtomicWeak::from(Arc::downgrade(&curr_arc)),
prev: AtomicWeak::from(Arc::downgrade(&curr_node)),
next: AtomicArc::from(next.clone()),
});
match curr_arc
match curr_node
.next
.compare_exchange(next.as_ref(), Some(&new), SeqCst, SeqCst)
{
Ok(_) => {
// Update the next node's prev ptr unless another thread already did.
if let Some(next_node) = next {
next_node.prev.store(Some(&Arc::downgrade(&new)), SeqCst);
_ = next_node.prev.compare_exchange(
Some(&Arc::downgrade(&curr_node)),
Some(&Arc::downgrade(&new)),
SeqCst,
SeqCst,
);
}
break;
}
Err(actual_next) => next = actual_next,
}
} else {
curr_arc = next.unwrap();
next = curr_arc.next.load(SeqCst);
curr_node = next.unwrap();
next = curr_node.next.load(SeqCst);
}
}
}
Expand Down

0 comments on commit dd10f28

Please sign in to comment.