elast0ny / elast0ny/shared_memory

mutex example initialization procedure appears to be incorrectly synchronized

Open
#115 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
444
Forks
63
PR merge metrics
No merged PRs in 30d

Description

https://github.com/elast0ny/shared_memory/blob/fda413410857e295fa7440691a79296e84bed525/examples/mutex.rs#L65-L89

```rust
let mutex = if shmem.is_owner() {
// is_init.store(0, Ordering::Relaxed); // not actually needed
// Initialize the mutex
let (lock, _bytes_used) = unsafe {
Mutex::new(
raw_ptr, // Base address of Mutex
raw_ptr.add(Mutex::size_of(Some(raw_ptr))), // Address of data protected by mutex
)
.unwrap()
};
// is_init.store(1, Ordering::Relaxed); // relaxed wouldn't restrict reordering of mutex init unless i'm missing something
is_init.store(1, Ordering::Release);
lock
} else {
// wait until mutex is initialized
// while is_init.load(Ordering::Relaxed) != 1 {} // need acquire for release store to is_init to synchronize-with load
// could also do a relaxed loop with a acquire fence, but that adds an extra instruction in the common case
while is_init.load(Ordering::Acquire) != 1 {
std::hint::spin_loop();
}
// Load existing mutex
let (lock, _bytes_used) = unsafe {
Mutex::from_existing(
raw_ptr, // Base address of Mutex
raw_ptr.add(Mutex::size_of(Some(raw_ptr))), // Address of data protected by mutex
)
.unwrap()
};
lock
};
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.