Jump to content

volatile (computer programming)

From Wikipedia, the free encyclopedia

In computer programming, a volatile value is a value that can be read by something else or changed by something else while the current code is running.

Despite being a common keyword across many programming languages, the behavior of the volatile keyword differs in subtle and important ways between different programming languages. The common theme is that a volatile variable is a variable that may be read or modified by something outside of the current program while the current program is running. In particular, the value of a volatile variable may appear change spontaneously without any apparent cause from the code in the program. The typical things that can read or modify Volatile variables include hardware devices via memory-mapped I/O (where reading from or writing to memory is used to communicate with peripheral devices), asynchronous signal handlers, and other threads of execution, but not all programming languages use the volatile keyword for all of these examples.

Volatility can have implications regarding function calling conventions and how variables are stored, accessed and cached.

In C and C++

[edit]

In C and C++, volatile is a type qualifier, like const, and is a part of a type (e.g. the type of a variable or field).

In C, and consequently C++, the volatile keyword was intended to:[1]

  • allow access to memory-mapped I/O devices
  • allow uses of variables between setjmp and longjmp
  • allow uses of sig_atomic_t variables in signal handlers.

Loosely, the volatile keyword tells a C/C++ optimizing compiler to not remove reads and writes (including seemingly redundant reads and writes), and to not introduce new reads and writes, and to not change the relative order of reads and writes. For example, the optimizing compiler cannot store a value written to a volatile variable in a register and then use that register for a later read of the same volatile variable. Instead, the compiler must flush the value to main memory for every write instruction in the code, and it must fetch the value from main memory for every read instruction in the code. These strict guarantees about preserving the exact number and order of reads and writes are required to write correct code to access memory-mapped I/O devices, to write correct code using setjmp and longjmp, and to write correct code that shares values with asynchronous signal handlers.

Multi-Threading

[edit]

It is a common misconception that the volatile keyword is useful for portable multi-threading code in C and C++. Unlike the Java and C# programming languages, operations on volatile variables in C and C++ are not atomic. The volatile keyword has never functioned as a useful, portable tool for any multi-threading scenario.[2][3][4][5] Most C and C++ compilers, linkers, and runtimes simply do not provide the necessary guarantees (such as memory barriers) to make the volatile keyword useful for any multi-threading scenario. Before the C11 and C++11 standards, programmers were forced to rely on guarantees from the individual implementations and platforms (e.g. POSIX and WIN32) to write multi-threading code. The C11 and C++11 standards introduced new constructs such as the std::atomic<T> templates which allowed writing portable multi-threading code.[6]

Example of memory-mapped I/O in C

[edit]

In this example, the code sets the value stored in foo to 0. It then starts to poll that value repeatedly until it changes to 255:

static int foo;

void bar(void) {
    foo = 0;

    while (foo != 255)
         ;
}

An optimizing compiler will notice that no other code can possibly change the value stored in foo, and will assume that it will remain equal to 0 at all times. The compiler will therefore replace the function body with an infinite loop similar to this:

void bar_optimized(void) {
    foo = 0;

    while (true)
         ;
}

However, the programmer may make foo refer to another element of the computer system such as a hardware register of a device connected to the CPU which may change the value of foo while this code is running. (This example does not include the details on how to make foo refer to a hardware register of a device connected to the CPU.) Without the volatile keyword, an optimizing compiler will likely convert the code from the first sample with the read in the loop to the second sample without the read in the loop as part of the common loop-invariant code-motion optimization, and thus the code will likely never notice the change that it is waiting for.

To prevent the compiler from doing this optimization, the volatile keyword can be used:

static volatile int foo;

void bar (void) {
    foo = 0;

    while (foo != 255)
        ;
}

The volatile keyword prevents the compiler from moving the read out of the loop, and thus the code will notice the expected change to the variable foo.

Optimization comparison in C

[edit]

The following C programs, and accompanying assembler language excerpts, demonstrate how the volatile keyword affects the compiler's output. The compiler in this case was GCC.

While observing the assembly code, it is clearly visible that the code generated with volatile objects is more verbose, making it longer so the nature of volatile objects can be fulfilled. The volatile keyword prevents the compiler from performing optimization on code involving volatile objects, thus ensuring that each volatile variable assignment and read has a corresponding memory access. Without the volatile keyword, the compiler knows a variable does not need to be reread from memory at each use, because there should not be any writes to its memory location from any other thread or process.

Standards Defects

[edit]

While intended by both C and C++, the current C standard fails to express that the volatile semantics refer to the lvalue, not the referenced object. The respective defect report DR 476 (to C11) is still under review with C17.[7]

In Java

[edit]

In all modern versions of the Java programming language, the volatile keyword gives the following guarantees:

  • volatile reads and writes are atomic. In particular, reads and writes to long and double fields will not tear. (The atomic guarantee applies only to the volatile primitive value or the volatile reference value, and not to any Object value.)
  • There is a single global ordering of all volatile reads and writes. In other words, a volatile will read the current value. It will not read an older value, and it will not read a value from the future.
  • volatile reads and writes have "acquire" and "release" memory barrier semantics (known in the Java standard as happens-before).[8][9] In other words, volatile provides guarantees about the relative order of volatile and non-volatile reads and writes. In other words, volatile basically the same memory visibility guarantees as a Java synchronized block (but without the mutual exclusion guarantees of a synchronized block).

Together, these guarantees make volatile into a useful multi-threading construct in Java. In particular, the typical double-checked locking algorithm with volatile works correctly in Java.[10]

Very Old Versions Of Java

[edit]

Before Java version 5, the Java standard did not guarantee the relative ordering of volatile and non-volatile reads and writes. In other words, volatile did not have "acquire" and "release" memory barrier semantics. This greatly limited its use as a multi-threading construct. In particular, the typical double-checked locking algorithm with volatile did not work correctly.

In C#

[edit]

In C#, volatile ensures that code accessing the field is not subject to some thread-unsafe optimizations that may be performed by the compiler, the CLR, or by hardware. When a field is marked volatile, the compiler is instructed to generate a "memory barrier" or "fence" around it, which prevents instruction reordering or caching tied to the field. When reading a volatile field, the compiler generates an acquire-fence, which prevents other reads and writes to the field from being moved before the fence. When writing to a volatile field, the compiler generates a release-fence; this fence prevents other reads and writes to the field from being moved after the fence.[11]

Only the following types can be marked volatile: all reference types, Single, Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Char, and all enumerated types with an underlying type of Byte, SByte, Int16, UInt16, Int32, or UInt32.[12] (This excludes value structs, as well as the primitive types Double, Int64, UInt64 and Decimal.)

Using the volatile keyword does not support fields that are passed by reference or captured local variables; in these cases, Thread.VolatileRead and Thread.VolatileWrite must be used instead.[11]

In effect, these methods disable some optimizations usually performed by the C# compiler, the JIT compiler, or the CPU itself. The guarantees provided by Thread.VolatileRead and Thread.VolatileWrite are a superset of the guarantees provided by the volatile keyword: instead of generating a "half fence" (ie an acquire-fence only prevents instruction reordering and caching that comes before it), VolatileRead and VolatileWrite generate a "full fence" which prevent instruction reordering and caching of that field in both directions.[11] These methods work as follows:[13]

  • The Thread.VolatileWrite method forces the value in the field to be written to at the point of the call. In addition, any earlier program-order loads and stores must occur before the call to VolatileWrite and any later program-order loads and stores must occur after the call.
  • The Thread.VolatileRead method forces the value in the field to be read from at the point of the call. In addition, any earlier program-order loads and stores must occur before the call to VolatileRead and any later program-order loads and stores must occur after the call.

The Thread.VolatileRead and Thread.VolatileWrite methods generate a full fence by calling the Thread.MemoryBarrier method, which constructs a memory barrier that works in both directions. In addition to the motivations for using a full fence given above, one potential problem with the volatile keyword that is solved by using a full fence generated by Thread.MemoryBarrier is as follows: due to the asymmetric nature of half fences, a volatile field with a write instruction followed by a read instruction may still have the execution order swapped by the compiler. Because full fences are symmetric, this is not a problem when using Thread.MemoryBarrier.[11]

In Fortran

[edit]

VOLATILE is part of the Fortran 2003 standard,[14] although earlier version supported it as an extension. Making all variables volatile in a function is also useful finding aliasing related bugs.

integer, volatile :: i ! When not defined volatile the following two lines of code are identical
write(*,*) i**2  ! Loads the variable i once from memory and multiplies that value times itself
write(*,*) i*i   ! Loads the variable i twice from memory and multiplies those values

By always "drilling down" to memory of a VOLATILE, the Fortran compiler is precluded from reordering reads or writes to volatiles. This makes visible to other threads actions done in this thread, and vice versa.[15]

Use of VOLATILE reduces and can even prevent optimization.[16]

References

[edit]
  1. ^ "Publication on C++ standards committee".
  2. ^ "Volatile Keyword In Visual C++". Microsoft MSDN.
  3. ^ "Linux Kernel Documentation – Why the "volatile" type class should not be used". kernel.org.
  4. ^ Scott Meyers; Andrei Alexandrescu (2004). "C++ and the Perils of Double-Checked Locking" (PDF). DDJ.
  5. ^ Jeremy Andrews (2007). "Linux: Volatile Superstition". kerneltrap.org. Archived from the original on 2010-06-20. Retrieved Jan 9, 2011.
  6. ^ "volatile (C++)". Microsoft MSDN.
  7. ^ Clarification Request Summary for C11. Version 1.13, October 2017.
  8. ^ Section 17.4.4: Synchronization Order "The Java® Language Specification, Java SE 7 Edition". Oracle Corporation. 2013. Retrieved 2013-05-12.
  9. ^ "Java Concurrency: Understanding the 'Volatile' Keyword". dzone.com. 2021-03-08. Archived from the original on 2021-05-09. Retrieved 2021-05-09.
  10. ^ Neil Coffey. "Double-checked Locking (DCL) and how to fix it". Javamex. Retrieved 2009-09-19.
  11. ^ a b c d Albahari, Joseph. "Part 4: Advanced Threading". Threading in C#. O'Reilly Media. Archived from the original on 12 December 2019. Retrieved 9 December 2019.{{cite web}}: CS1 maint: bot: original URL status unknown (link)
  12. ^ Richter, Jeffrey (February 11, 2010). "Chapter 7: Constants and Fields". CLR Via C#. Microsoft Press. pp. 183. ISBN 978-0-7356-2704-8.
  13. ^ Richter, Jeffrey (February 11, 2010). "Chapter 28: Primitive Thread Synchronization Constructs". CLR Via C#. Microsoft Press. pp. 797–803. ISBN 978-0-7356-2704-8.
  14. ^ "VOLATILE Attribute and Statement". Cray. Archived from the original on 2018-01-23. Retrieved 2016-04-22.
  15. ^ "Volatile and shared array in Fortran". Intel.com.
  16. ^ "VOLATILE". Oracle.com.
[edit]