-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Add safe wrapper for atomic_compilerfence intrinsics #41092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2598e45
Add safe wrapper for atomic_singlethreadfence_*
jonhoo f6d262a
Add unstable book entry
jonhoo 5c6f7fa
Point to tracking issue, not PR
jonhoo 5202ac5
Correct book examples for hardware re-ordering
jonhoo 8fc3ab2
rustdoc needs space after # to ignore
jonhoo e6597e1
Mention interrupts and green threads
jonhoo f093d59
Address @parched's comments
jonhoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# `compiler_barriers` | ||
|
||
The tracking issue for this feature is: [#41092] | ||
|
||
[#41092]: https://github.com/rust-lang/rust/issues/41092 | ||
|
||
------------------------ | ||
|
||
The `compiler_barriers` feature exposes the `compiler_barrier` function | ||
in `std::sync::atomic`. This function is conceptually similar to C++'s | ||
`atomic_signal_fence`, which can currently only be accessed in nightly | ||
Rust using the `atomic_singlethreadfence_*` instrinsic functions in | ||
`core`, or through the mostly equivalent literal assembly: | ||
|
||
```rust | ||
#![feature(asm)] | ||
unsafe { asm!("" ::: "memory" : "volatile") }; | ||
``` | ||
|
||
A `compiler_barrier` restricts the kinds of memory re-ordering the | ||
compiler is allowed to do. Specifically, depending on the given ordering | ||
semantics, the compiler may be disallowed from moving reads or writes | ||
from before or after the call to the other side of the call to | ||
`compiler_barrier`. | ||
|
||
## Examples | ||
|
||
The need to prevent re-ordering of reads and writes often arises when | ||
working with low-level devices. Consider a piece of code that interacts | ||
with an ethernet card with a set of internal registers that are accessed | ||
through an address port register (`a: &mut usize`) and a data port | ||
register (`d: &usize`). To read internal register 5, the following code | ||
might then be used: | ||
|
||
```rust | ||
fn read_fifth(a: &mut usize, d: &usize) -> usize { | ||
*a = 5; | ||
*d | ||
} | ||
``` | ||
|
||
In this case, the compiler is free to re-order these two statements if | ||
it thinks doing so might result in better performance, register use, or | ||
anything else compilers care about. However, in doing so, it would break | ||
the code, as `x` would be set to the value of some other device | ||
register! | ||
|
||
By inserting a compiler barrier, we can force the compiler to not | ||
re-arrange these two statements, making the code function correctly | ||
again: | ||
|
||
```rust | ||
#![feature(compiler_barriers)] | ||
use std::sync::atomic; | ||
|
||
fn read_fifth(a: &mut usize, d: &usize) -> usize { | ||
*a = 5; | ||
atomic::compiler_barrier(atomic::Ordering::SeqCst); | ||
*d | ||
} | ||
``` | ||
|
||
Compiler barriers are also useful in code that implements low-level | ||
synchronization primitives. Consider a structure with two different | ||
atomic variables, with a dependency chain between them: | ||
|
||
```rust | ||
use std::sync::atomic; | ||
|
||
fn thread1(x: &atomic::AtomicUsize, y: &atomic::AtomicUsize) { | ||
x.store(1, atomic::Ordering::Release); | ||
let v1 = y.load(atomic::Ordering::Acquire); | ||
} | ||
fn thread2(x: &atomic::AtomicUsize, y: &atomic::AtomicUsize) { | ||
y.store(1, atomic::Ordering::Release); | ||
let v2 = x.load(atomic::Ordering::Acquire); | ||
} | ||
``` | ||
|
||
This code will guarantee that `thread1` sees any writes to `y` made by | ||
`thread2`, and that `thread2` sees any writes to `x`. Intuitively, one | ||
might also expect that if `thread2` sees `v2 == 0`, `thread1` must see | ||
`v1 == 1` (since `thread2`'s store happened before its `load`, and its | ||
load did not see `thread1`'s store). However, the code as written does | ||
*not* guarantee this, because the compiler is allowed to re-order the | ||
store and load within each thread. To enforce this particular behavior, | ||
a call to `compiler_barrier(Ordering::SeqCst)` would need to be inserted | ||
between the `store` and `load` in both functions. | ||
|
||
Compiler barriers with weaker re-ordering semantics (such as | ||
`Ordering::Acquire`) can also be useful, but are beyond the scope of | ||
this text. Curious readers are encouraged to read the Linux kernel's | ||
discussion of [memory barriers][1], as well as C++ references on | ||
[`std::memory_order`][2] and [`atomic_signal_fence`][3]. | ||
|
||
[1]: https://www.kernel.org/doc/Documentation/memory-barriers.txt | ||
[2]: http://en.cppreference.com/w/cpp/atomic/memory_order | ||
[3]: http://www.cplusplus.com/reference/atomic/atomic_signal_fence/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hardware can still make this reordering regardless of what the compiler does. A better example might be something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you're right. In fact, even the example above is wrong for the same reason (you would need to use
volatile_*
). Maybe I'm finding myself agreeing more with the original RFC's conclusion that this is not something that people should generally need (at least until Rust start supporting signal handlers)...