diff options
-rw-r--r-- | Documentation/memory-barriers.txt | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/Documentation/memory-barriers.txt b/Documentation/memory-barriers.txt index 102dc19c4119..f9ff060d8320 100644 --- a/Documentation/memory-barriers.txt +++ b/Documentation/memory-barriers.txt | |||
@@ -1249,6 +1249,23 @@ The ACCESS_ONCE() function can prevent any number of optimizations that, | |||
1249 | while perfectly safe in single-threaded code, can be fatal in concurrent | 1249 | while perfectly safe in single-threaded code, can be fatal in concurrent |
1250 | code. Here are some examples of these sorts of optimizations: | 1250 | code. Here are some examples of these sorts of optimizations: |
1251 | 1251 | ||
1252 | (*) The compiler is within its rights to reorder loads and stores | ||
1253 | to the same variable, and in some cases, the CPU is within its | ||
1254 | rights to reorder loads to the same variable. This means that | ||
1255 | the following code: | ||
1256 | |||
1257 | a[0] = x; | ||
1258 | a[1] = x; | ||
1259 | |||
1260 | Might result in an older value of x stored in a[1] than in a[0]. | ||
1261 | Prevent both the compiler and the CPU from doing this as follows: | ||
1262 | |||
1263 | a[0] = ACCESS_ONCE(x); | ||
1264 | a[1] = ACCESS_ONCE(x); | ||
1265 | |||
1266 | In short, ACCESS_ONCE() provides cache coherence for accesses from | ||
1267 | multiple CPUs to a single variable. | ||
1268 | |||
1252 | (*) The compiler is within its rights to merge successive loads from | 1269 | (*) The compiler is within its rights to merge successive loads from |
1253 | the same variable. Such merging can cause the compiler to "optimize" | 1270 | the same variable. Such merging can cause the compiler to "optimize" |
1254 | the following code: | 1271 | the following code: |