I would like to write a function that triggers copy-on-write of a page, without having to modify any values in that page. A simple implementation:
void trigger_cow(char* addr){
*addr = *addr;
}
doesn't work because GCC will optimize out the line. If I use volatile,
void trigger_cow(char* addr){
volatile char* vaddr = (volatile char*) addr;
*vaddr = *vaddr;
}
then this works under -O3.
Will this "hack" work under other compilers or optimization settings?
The description of volatile in most sites I've seen doesn't seem to describe what happens when you write to a volatile pointer, only what happens when you read from one. Thanks!
That is exactly what volatile
does... it forces reads and writes to happen exactly once each time a variable is accessed, and in the same order as the reads and writes appear in the program (they cannot be reordered).
Note that the processor can still reorder reads and writes*, so volatile
is not particularly useful for multithreaded programming.
*Except on the Itanium ABI, but that's unique.
Formally, any access to volatile
variables constitutes observable behavior of the program. It cannot be optimized out and it cannot be reordered with regard to other elements of observable behavior.
Optimizations in C++ programs are carried out under that "as if" rule, which basically gives the compiler permission to do anything as long as it does not change the observable behavior of the program.
The standard (§1.9/8) requires that:
The least requirements on a conforming implementation are:
— Access to volatile objects are evaluated strictly according to the rules of the abstract machine.
"Access" is defined as either reading or writing, so yes, your *vaddr = *vaddr;
must read a value from *vaddr
, and then write the same value back to *vaddr
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With