Skip to content

Commit

Permalink
Fix insufficient synchronization in examples (#30)
Browse files Browse the repository at this point in the history
The examples use `Arc::strong_count` in order to await all other threads
having terminated. Unfortunately, `Arc::strong_count` uses a `Relaxed`
atomic to load the value, so this can not be used to establish an
ordering, and later leads to a weak memory data race. The alternative
is to use `get_mut`, which uses the internal `is_unique` function, which
is unfortunately private and therefore not usable directly.
  • Loading branch information
JoJoDeveloping authored Aug 28, 2024
1 parent bcd0436 commit b2eb642
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions examples/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn new_value(i: usize) -> Vec<u8> {
fn main() {
const N: usize = 1000;
let l = SkipMap::with_options(Options::new().with_capacity(1 << 20)).unwrap();
let wg = Arc::new(());
let mut wg = Arc::new(());
for i in 0..N {
let w = wg.clone();
let l = l.clone();
Expand All @@ -21,7 +21,7 @@ fn main() {
drop(w);
});
}
while Arc::strong_count(&wg) > 1 {}
while Arc::get_mut(&mut wg).is_none() {}
for i in 0..N {
let w = wg.clone();
let l = l.clone();
Expand Down
4 changes: 2 additions & 2 deletions examples/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
.write(true);

let l = SkipMap::map_mut("test.wal", open_options, mmap_options).unwrap();
let wg = Arc::new(());
let mut wg = Arc::new(());
for i in 0..N {
let w = wg.clone();
let l = l.clone();
Expand All @@ -27,7 +27,7 @@ fn main() {
drop(w);
});
}
while Arc::strong_count(&wg) > 1 {}
while Arc::get_mut(&mut wg).is_none() {}
for i in 0..N {
let w = wg.clone();
let l = l.clone();
Expand Down
4 changes: 2 additions & 2 deletions examples/mmap_anon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {

let mmap_options = skl::MmapOptions::default().len(1 << 20);
let l = SkipMap::map_anon(mmap_options).unwrap();
let wg = Arc::new(());
let mut wg = Arc::new(());
for i in 0..N {
let w = wg.clone();
let l = l.clone();
Expand All @@ -23,7 +23,7 @@ fn main() {
drop(w);
});
}
while Arc::strong_count(&wg) > 1 {}
while Arc::get_mut(&mut wg).is_none() {}
for i in 0..N {
let w = wg.clone();
let l = l.clone();
Expand Down

0 comments on commit b2eb642

Please sign in to comment.