Ada Programming/Pragmas/Volatile

pragma Volatile (local_name);

Description
Volatile is a representation pragma that can be used with types and variables to specify that the variable in question may suddenly change in value. For example, this may occur due to a device writing to a shared buffer.

When this pragma is used, the compiler must suppress any optimizations that would interfere with the correct reading of the volatile variables. For example, two successive readings of the same variable cannot be optimized to just one or reordered.

Compare with pragma Atomic, Atomic_Components, and Volatile_Components.

Example
Video_Buffer  (Natural  <>)  RGB_Value; (Video_Buffer);

Ada versions

 * This pragma is available in Ada 95 and Ada 2005 (Annex C, a specialized needs annex).
 * In Ada 83 there is not an standard pragma completely equivalent to Volatile. However, pragma Shared is similar to Atomic (although it is obsolescent since Ada 95), and some compilers added an implementation-defined pragma for this purpose.

Compilers
Volatile is a standard pragma since Ada 95, defined in the Systems Programming Annex (Annex C). This is a specialized needs annex, therefore this pragma is not available in those compilers not implementing that annex.

Platforms
This pragma is portable to different architectures and operating systems.

Interfacing
In C/C++, the volatile qualifier is equivalent to this pragma. However, this C qualifier is used for both volatile and atomic data, even if atomic access is required. Therefore, when interfacing with existing C code, the programmer must read the code to understand whether the data needs atomic load and stores. If needed, the data should be marked in the Ada side with pragma Atomic or Atomic_Components, or with pragma Volatile or Volatile_Components otherwise.

Volatile objects without address clauses
In many cases, when a volatile object is used for interfacing with a hardware device it should have an address clause too.


 * gnatcheck:

+R Volatile_Objects_Without_Address_Clauses


 * AdaControl:

check object_declarations (volatile_no_address);

Wikibook

 * Ada Programming
 * Ada Programming/Pragmas
 * Ada Programming/Pragmas/Atomic
 * Ada Programming/Pragmas/Atomic Components
 * Ada Programming/Pragmas/Volatile Components
 * Ada Programming/Pragmas/Shared (obsolescent)