Skip to content

Commit 368d560

Browse files
committed
Rename compiler_barrier to compiler_fence
This addresses concerns raised following the merge of #41092. Specifically: > The naming of these seems surprising: the multithreaded functions (and > both the single and multithreaded intrinsics themselves) are fences, > but this is a barrier. It's not incorrect, but the latter is both > inconsistent with the existing functions and slightly confusing with > another type in std (e.g., `Barrier`). `compiler_fence` carries the same semantic implication that this is a compiler-only operation, while being more in line with the fence/barrier concepts already in use in `std`.
1 parent 14481f7 commit 368d560

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

src/doc/unstable-book/src/SUMMARY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
- [collections](collections.md)
3939
- [collections_range](collections-range.md)
4040
- [command_envs](command-envs.md)
41-
- [compiler_barriers](compiler-barriers.md)
41+
- [compiler_fences](compiler-fences.md)
4242
- [compiler_builtins](compiler-builtins.md)
4343
- [compiler_builtins_lib](compiler-builtins-lib.md)
4444
- [concat_idents](concat-idents.md)

src/doc/unstable-book/src/compiler-barriers.md renamed to src/doc/unstable-book/src/compiler-fences.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# `compiler_barriers`
1+
# `compiler_fences`
22

33
The tracking issue for this feature is: [#41091]
44

55
[#41091]: https://github.com/rust-lang/rust/issues/41091
66

77
------------------------
88

9-
The `compiler_barriers` feature exposes the `compiler_barrier` function
9+
The `compiler_fences` feature exposes the `compiler_fence` function
1010
in `std::sync::atomic`. This function is conceptually similar to C++'s
1111
`atomic_signal_fence`, which can currently only be accessed in nightly
1212
Rust using the `atomic_singlethreadfence_*` instrinsic functions in
@@ -17,18 +17,18 @@ Rust using the `atomic_singlethreadfence_*` instrinsic functions in
1717
unsafe { asm!("" ::: "memory" : "volatile") };
1818
```
1919

20-
A `compiler_barrier` restricts the kinds of memory re-ordering the
20+
A `compiler_fence` restricts the kinds of memory re-ordering the
2121
compiler is allowed to do. Specifically, depending on the given ordering
2222
semantics, the compiler may be disallowed from moving reads or writes
2323
from before or after the call to the other side of the call to
24-
`compiler_barrier`. Note that it does **not** prevent the *hardware*
24+
`compiler_fence`. Note that it does **not** prevent the *hardware*
2525
from doing such re-ordering. This is not a problem in a single-threaded,
2626
execution context, but when other threads may modify memory at the same
2727
time, stronger synchronization primitives are required.
2828

2929
## Examples
3030

31-
`compiler_barrier` is generally only useful for preventing a thread from
31+
`compiler_fence` is generally only useful for preventing a thread from
3232
racing *with itself*. That is, if a given thread is executing one piece
3333
of code, and is then interrupted, and starts executing code elsewhere
3434
(while still in the same thread, and conceptually still on the same
@@ -37,7 +37,7 @@ handler is registered. In more low-level code, such situations can also
3737
arise when handling interrupts, when implementing green threads with
3838
pre-emption, etc.
3939

40-
To give a straightforward example of when a `compiler_barrier` is
40+
To give a straightforward example of when a `compiler_fence` is
4141
necessary, consider the following example:
4242

4343
```rust
@@ -67,22 +67,22 @@ remember that the compiler is free to swap the stores to
6767
after `IS_READY` is updated, then the signal handler will see
6868
`IS_READY=1`, but `IMPORTANT_VARIABLE=0`.
6969

70-
Using a `compiler_barrier`, we can remedy this situation:
70+
Using a `compiler_fence`, we can remedy this situation:
7171

7272
```rust
73-
#![feature(compiler_barriers)]
73+
#![feature(compiler_fences)]
7474
# use std::sync::atomic::{AtomicBool, AtomicUsize};
7575
# use std::sync::atomic::{ATOMIC_BOOL_INIT, ATOMIC_USIZE_INIT};
7676
# use std::sync::atomic::Ordering;
77-
use std::sync::atomic::compiler_barrier;
77+
use std::sync::atomic::compiler_fence;
7878

7979
static IMPORTANT_VARIABLE: AtomicUsize = ATOMIC_USIZE_INIT;
8080
static IS_READY: AtomicBool = ATOMIC_BOOL_INIT;
8181

8282
fn main() {
8383
IMPORTANT_VARIABLE.store(42, Ordering::Relaxed);
8484
// prevent earlier writes from being moved beyond this point
85-
compiler_barrier(Ordering::Release);
85+
compiler_fence(Ordering::Release);
8686
IS_READY.store(true, Ordering::Relaxed);
8787
}
8888

src/libcore/sync/atomic.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1591,11 +1591,11 @@ pub fn fence(order: Ordering) {
15911591
}
15921592

15931593

1594-
/// A compiler memory barrier.
1594+
/// A compiler memory fence.
15951595
///
1596-
/// `compiler_barrier` does not emit any machine code, but prevents the compiler from re-ordering
1596+
/// `compiler_fence` does not emit any machine code, but prevents the compiler from re-ordering
15971597
/// memory operations across this point. Which reorderings are disallowed is dictated by the given
1598-
/// [`Ordering`]. Note that `compiler_barrier` does *not* introduce inter-thread memory
1598+
/// [`Ordering`]. Note that `compiler_fence` does *not* introduce inter-thread memory
15991599
/// synchronization; for that, a [`fence`] is needed.
16001600
///
16011601
/// The re-ordering prevented by the different ordering semantics are:
@@ -1617,15 +1617,15 @@ pub fn fence(order: Ordering) {
16171617
/// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
16181618
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
16191619
#[inline]
1620-
#[unstable(feature = "compiler_barriers", issue = "41091")]
1621-
pub fn compiler_barrier(order: Ordering) {
1620+
#[unstable(feature = "compiler_fences", issue = "41091")]
1621+
pub fn compiler_fence(order: Ordering) {
16221622
unsafe {
16231623
match order {
16241624
Acquire => intrinsics::atomic_singlethreadfence_acq(),
16251625
Release => intrinsics::atomic_singlethreadfence_rel(),
16261626
AcqRel => intrinsics::atomic_singlethreadfence_acqrel(),
16271627
SeqCst => intrinsics::atomic_singlethreadfence(),
1628-
Relaxed => panic!("there is no such thing as a relaxed barrier"),
1628+
Relaxed => panic!("there is no such thing as a relaxed compiler fence"),
16291629
__Nonexhaustive => panic!("invalid memory ordering"),
16301630
}
16311631
}

0 commit comments

Comments
 (0)