pTAL Conversion Guide

Data Allocation
pTAL Conversion Guide527302-002
5-6
Volatile Data
The VOLATILE attribute is recommended for data items that can be accessed
asynchronously by other processes, for example:
Trap handlers that access global data
I/O operations that do not notify you when they are complete
For I/O operations that notify you when they are complete, you do not need volatile
data: Specify a buffer when you initiate an asynchronous I/O operation and access
the buffer only after the I/O is complete.
In Example 5-2 on page 5-6, the rewritten code fails if a trap handler or another
process changes the value of flag during computation.
Example 5-2. Volatile Data
Original code:
INT flag;
WHILE condition DO
BEGIN
flag := 0;
! computation; Compiler might assume that computation
! does not call a procedure and that
! flag is always 0 and that ...
IF flag THEN ! this test of flag always fails
! remedial action;
END;
How the compiler might rewrite the original code:
INT flag;
flag := 0; ! Moved outside of loop
WHILE condition DO ! IF statement that tests flag removed
BEGIN
! computation;
END;
VOLATILE prevents the rewrite:
VOLATILE INT flag; ! Add VOLATILE attribute
WHILE condition DO
BEGIN
flag := 0;
! computation
IF flag THEN ! Compiler does not remove this test
! remedial action;
END;