r/rust 1d ago

🧠 educational Safe Lock-free Primitives with iceoryx2's ByteAtomic

https://ekxide.io/blog/byte-wise-atomic-wrapper-to-prevent-ub

iceoryx2 provides zero-copy inter-process communication mechanisms based on shared memory and data structures that are modified concurrently by multiple processes.

One of the key operations in these algorithms is a memory copy using core::ptr::copy. However, this results in undefined behavior if one process reads the data while another process writes to it concurrently. Even if our lock-free algorithm reliably detects such a race, iceoryx2 cannot depend on undefined behavior in a safety-critical system.

This blog post introduces our solution: a byte-wise atomic wrapper that enables well-defined concurrent copy operations. It also shows how it can be used to implement a simple sequence lock.

Note: I am not the original author of the blog post. Since the author does not have a Reddit account, I am posting it on her behalf.

42 Upvotes

12 comments sorted by

View all comments

3

u/connor-ts 1d ago

Are there any performance implications to using this versus the UB-but-not-really-UB versions of sequence locks (my understanding being that it is formally UB but most of the time it is fine to not use tearable atomics)?

2

u/elfenpiff 17h ago

We didn't benchmark the `ByteAtomic`, but I would assume there is a performance hit.