411 REFERENCE · Wikibooks
Ada Programming/Pragmas/Atomic
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Pragmas Atomic
__TOC__
pragma Atomic (local_name);
Description
Atomic is a representation pragma that can be used with types and variables to specify that the code generated must read and write the type or variable from memory atomically, i.e. as a single/non-interruptible operation. It implies pragma Volatile, the difference is that pragma Atomic is stronger: the compilation must fail if the variable cannot be updated atomically.
This is typically used in the context of interfacing with hardware registers, which must be written atomically and where the compiler must not reorder or suppress any redundant read or write. In some cases it can also be used for tasking, but for communication of Ada tasks it is safer to use protected types or other synchronization primitives.
Example
Device_Status : Status_Register;
Atomic (Device_Status);
for Device_Status'Address use System Storage_Elements.To_Address (16#8010_FF74#);
Incorrect usage
Portability
Compilers
Atomic 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.
Ada versions
Available in Ada 95 and Ada 2005.
In Ada 83 pragma Shared produces similar effects than pragma Atomic (but it is obsolescent since Ada 95). pragma Shared (was Ada is almost ....)
Robert Dewar
1996-02-17
comp.lang.ada
http://groups.google.es/group/comp.lang.ada/msg/eeee3a7515837ea2
2008-05-28
pragma Atomic is QUITE different from pragma Volatile. pragma Atomic in Ada is essentially identical to pragma Shared in Ada 83. pragma Volatile in Ada is essentially identical to volatile in C, and is a feature that is not available in Ada 83.
Platforms
Atomic is a portability aid between different platforms. For example, when porting between a 64-bit to a 32-bit architecture, the compiler will detect portability problems thanks to this pragma.
Interfacing
The effects of pragma Atomic can be achieved in C using the type volatile sig_atomic_t, defined in signal.h (called volatile std::sig_atomic_t in C++). It is worth noting that the only values that can be portably stored in this type are 0–127, https://www.securecoding.cert.org/confluence/display/seccode/DCL34-C.+Use+volatile+for+data+that+cannot+be+cached
DCL34-C. Use volatile for data that cannot be cached
2009-01-10
Justin Pincar
2008-11-19
CERT C Secure Coding Standard
Computer Emergency Response Team
Integer values from 0 through 127 can be safely stored to a variable of type sig_atomic_t. whereas the Ada pragma doesn't impose any specific limit (other than the type of the atomic object).
However, this C type is rarely known by programmers. Therefore, the C volatile qualifier is the approach widely used both for volatile and atomic data, even if atomic access in required. Thus, when interfacing with existing C code, the programmer must first read the code to understand whether the data needs atomic load and stores or not. If needed, the data should be marked in the Ada side with pragma Atomic or Atomic_Components. Otherwise, pragma Volatile or Volatile_Components should be added instead.
C++11 and C11 incorporated atomic operations and types, Herb Sutter
volatile vs. volatile
Dr. Dobb's
2009-01-08
http://www.ddj.com/hpc-high-performance-computing/212701484
ISO C++ added them to the C++0x draft Standard in 2007, under the templated name atomic<T> (e.g., atomic). They started to become available beginning in 2008 in the Boost project and other implementations. The ISO C++ atomics library also provides a C-compatible way to spell those types and their operations (e.g., atomic_int), and these appear to be likely to be adopted by ISO C in the future.
2009-01-10 but differ from the semantics in Ada and are not compatible.
See also
Wikibook
Ada Programming
Ada Programming/Pragmas
Ada Programming/Pragmas/Volatile
Ada Programming/Pragmas/Atomic Components
Ada Programming/Pragmas/Volatile Components
Ada Programming/Pragmas/Shared (obsolescent)
Ada Reference Manual
C 6 Shared Variable Control
Ada 95 Rationale
C 5 Shared Variable Control 3
Ada 95 Quality and Style Guide
6 2 4 Unprotected Shared Variables
References
Atomic
Atomic