1. Re-order
In order to improve performance when executing programs, the compiler and processor often reorder instructions. Reorder Three types:
1. Compiler-Optimized reordering. The compiler can rearrange the execution order of the statements without changing the semantics of the single-threaded procedure.
2. Command-level parallel reordering. Modern processors use instruction-level parallelism (Instruction-level Parallelism, ILP) to overlap multiple instructions. If there is no data dependency, the processor can change the order in which the statement corresponds to the machine instruction.
3. Reordering of memory systems. Because the processor is using cache and read/write buffers, this makes the load and storage operations appear to be out of order.
From the Java source code to the final actual execution of the sequence of instructions, will go through the following three reordering:
The above 1 belongs to the compiler reordering, and 2 and 3 are processor reordering. These reordering can cause memory visibility issues with multithreaded programs. For compilers, compiler JMM for a compiler prevents a particular type of compiler from being reordered (not all compilers are disabled). For processor reordering, The JMM collation of a handler requires the Java compiler to insert a specific type of memory barrier (Barriers,inter called memory fence) instruction when generating a sequence of instructions, and to suppress a particular handler reordering by a characteristic memory barrier ( Not all handlers are disabled).
JMM is a language-level memory model that ensures consistent memory visibility for programmers on different compilers and on different processor platforms by prohibiting certain types of compilers from reordering and handlers.
2. Processor reordering and memory barrier directives
Modern processors use write buffers to temporarily hold data written to memory. Write buffers keep the instruction pipeline running, and he can avoid delays due to processor pauses waiting to write data to memory. At the same time, by flushing the write buffer in batch mode, and multiple writes to the same memory address in the merge write buffer, you can reduce the memory bus footprint. Although write buffers have so many benefits, the write buffers for each processor are only visible to the processor on which it resides. This feature has a significant impact on the order in which memory operations are executed: processor-to-memory read/ The order in which the writes are executed does not necessarily coincide with the order in which the memory actually occurs in the read/write operation!
Assuming that processor A and processor B perform memory accesses in parallel in the order of the program, they may end up with x=y=0 results. The specific reasons are as follows:
Here processor A and processor B can simultaneously write shared variables to their write buffers (A1,B1), then read another shared variable (A2,B2) from memory, and finally flush the dirty data stored in their write buffers into memory (A3,B3). When executed in this sequence, the program can get x= The result of y=0.
The key here is that because the write buffer is visible only to its own processor, it can cause the processor to perform memory operations in a sequence that is inconsistent with the order in which the memory is actually performed. Because modern processors use write buffers, modern processors allow the reordering of write-read operations.
The following is a list of the types of reordering that are allowed for common processors:
The "n" from the table cell means that the processor does not allow two operations to be reordered, and "Y" means that reordering is allowed.
As we can see from the table above, the common processors allow store-load to be reordered; The common processors do not allow reordering of data-dependent operations. Spare-tso and x86 have relatively strong processor memory models that allow them to be reordered only for write-read operations.
To ensure memory visibility, the Java compiler inserts a memory barrier directive in place of the generated instruction sequence to suppress a particular type of handler reordering. JMM the memory barrier directives into the following four categories:
Storeload barriers is a "all-in-one" barrier that simultaneously has the effect of three other barriers. Modern multiprocessor mostly supports this barrier (other types of barriers are not necessarily supported by all processors). It would be expensive to implement the barrier, Because the current processor usually wants to flush all the data in the write buffer into memory (buffer fully flush).
Reordering data dependencies
If two operations access the same variable, and one of the two operations is a write operation, there is a data dependency between the two operations. Data dependencies are divided into the following three types:
In the above three cases, the execution result of the program will be changed as long as the order of execution of the two operations is reordered.
The compiler and the processor may reorder the operations. When the compiler and processor are reordered, data dependencies are observed, and the compiler and processor do not change the order in which the two operations exist that have data dependencies.
Note: The data dependencies described here are only for sequences of instructions executed in a single processor and for operations performed in a single thread, and data dependencies between different processors and between different threads are not considered by the compiler and processor.
Happens-before
Starting with JDK1.5, Java uses the new JSR-133 memory model. JSR-133 uses the concept of Happens-before to illustrate the memory visibility between operations. In JMM, if the result of one operation needs to be visible to another operation, Then there must be a happens-before relationship between the two operations. The two actions mentioned here can be within a thread or between different threads.
The Happens-before rules that are closely related to programmers are as follows:
Program Order rules: Each action in a thread is happens-before to any subsequent action in that thread.
Monitor lock rule: the unlocking of a monitor happens-before the subsequent lock on the monitor.
Volatile variable rule: writes to a volatile field, happens-before to any subsequent reading of this volatile field.
Transitivity: If a happens-before B, and B happens-before C, then a Happens-before c.
Note: The two operation has a happens-before relationship and does not imply that the previous operation must be performed before the next operation. Happens-before only requires that the previous operation (the result of the execution) be visible to the latter operation, and that the previous operation precedes the second operation in order.
As-if-serial semantics
As-if-serial semantics means: No matter how reordering (compilers and processors in order to improve parallelism), the execution results of (single-threaded) programs cannot be changed. The compiler, runtime, and processor must obey the as-if-serial semantics.
To comply with as-if-serial semantics, the compiler and processor do not reorder operations that have data dependencies, because this reordering alters the execution result. However, if there is no data dependency relationship between actions, these operations can be reordered by the compiler and handlers.
As-if-serial semantics protects the single-threaded program, adheres to the as-if-serial semantic compiler, and the runtime and the processor create an illusion for programmers who write single-threaded programming: Single-threaded programs are executed in the order of the program. As-if-serial semantics enable single-threaded programmers to interfere with reordering without worrying about memory visibility issues.
Reordering of Java Memory models