1
- # ` compiler_barriers `
1
+ # ` compiler_fences `
2
2
3
3
The tracking issue for this feature is: [ #41091 ]
4
4
5
5
[ #41091 ] : https://github.com/rust-lang/rust/issues/41091
6
6
7
7
------------------------
8
8
9
- The ` compiler_barriers ` feature exposes the ` compiler_barrier ` function
9
+ The ` compiler_fences ` feature exposes the ` compiler_fence ` function
10
10
in ` std::sync::atomic ` . This function is conceptually similar to C++'s
11
11
` atomic_signal_fence ` , which can currently only be accessed in nightly
12
12
Rust using the ` atomic_singlethreadfence_* ` instrinsic functions in
@@ -17,18 +17,18 @@ Rust using the `atomic_singlethreadfence_*` instrinsic functions in
17
17
unsafe { asm! ("" :: : " memory" : " volatile" ) };
18
18
```
19
19
20
- A ` compiler_barrier ` restricts the kinds of memory re-ordering the
20
+ A ` compiler_fence ` restricts the kinds of memory re-ordering the
21
21
compiler is allowed to do. Specifically, depending on the given ordering
22
22
semantics, the compiler may be disallowed from moving reads or writes
23
23
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*
25
25
from doing such re-ordering. This is not a problem in a single-threaded,
26
26
execution context, but when other threads may modify memory at the same
27
27
time, stronger synchronization primitives are required.
28
28
29
29
## Examples
30
30
31
- ` compiler_barrier ` is generally only useful for preventing a thread from
31
+ ` compiler_fence ` is generally only useful for preventing a thread from
32
32
racing * with itself* . That is, if a given thread is executing one piece
33
33
of code, and is then interrupted, and starts executing code elsewhere
34
34
(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
37
37
arise when handling interrupts, when implementing green threads with
38
38
pre-emption, etc.
39
39
40
- To give a straightforward example of when a ` compiler_barrier ` is
40
+ To give a straightforward example of when a ` compiler_fence ` is
41
41
necessary, consider the following example:
42
42
43
43
``` rust
@@ -67,22 +67,22 @@ remember that the compiler is free to swap the stores to
67
67
after ` IS_READY ` is updated, then the signal handler will see
68
68
` IS_READY=1 ` , but ` IMPORTANT_VARIABLE=0 ` .
69
69
70
- Using a ` compiler_barrier ` , we can remedy this situation:
70
+ Using a ` compiler_fence ` , we can remedy this situation:
71
71
72
72
``` rust
73
- #![feature(compiler_barriers )]
73
+ #![feature(compiler_fences )]
74
74
# use std :: sync :: atomic :: {AtomicBool , AtomicUsize };
75
75
# use std :: sync :: atomic :: {ATOMIC_BOOL_INIT , ATOMIC_USIZE_INIT };
76
76
# use std :: sync :: atomic :: Ordering ;
77
- use std :: sync :: atomic :: compiler_barrier ;
77
+ use std :: sync :: atomic :: compiler_fence ;
78
78
79
79
static IMPORTANT_VARIABLE : AtomicUsize = ATOMIC_USIZE_INIT ;
80
80
static IS_READY : AtomicBool = ATOMIC_BOOL_INIT ;
81
81
82
82
fn main () {
83
83
IMPORTANT_VARIABLE . store (42 , Ordering :: Relaxed );
84
84
// prevent earlier writes from being moved beyond this point
85
- compiler_barrier (Ordering :: Release );
85
+ compiler_fence (Ordering :: Release );
86
86
IS_READY . store (true , Ordering :: Relaxed );
87
87
}
88
88
0 commit comments