User`s guide
How the MACRO Compiler Functions on Different Platforms
2.11 Preserving VAX Atomicity and Granularity
The problem with this Alpha/Itanium code sequence is that an interrupt can
occur between any of the instructions. If the interrupt causes an AST routine to
execute or causes another process to be scheduled between the LDL and the STL,
and the AST or other process updates the data pointed to by R1, the STL will
store the result (R1) based on stale data.
When an atomic operation is required, and /PRESERVE=ATOMICITY (or
.PRESERVE ATOMICITY) is specified, the compiler generates the following
Alpha instruction sequence for INCL (R1):
Retry: LDL_L R28,(R1)
ADDL R28,#1,R28
STL_C R28,(R1)
BEQ R28, fail
.
.
.
fail: BR Retry
and the following Itanium instruction sequence:
$L3: ld4 r23 = [r9]
mov.m apccv = r23
mov r22 = r23
sxt4 r23 = r23
adds r23 = 1, r23
cmpxchg4.acq r23, [r9] = r23
cmp.eq pr0, pr6 = r22, r23
(pr6) br.cond.dpnt.few $L3
On the OpenVMS Alpha system, if (R1) is modified by any other code thread
on the current or any other processor during this sequence, the Store Longword
Conditional instruction (STL_C) will not update (R1), but will indicate an error by
writing 0 into R28. In this case, the code branches back and retries the operation
until it completes without interference.
The BEQ Fail and BR Retry are done instead of a BEQ Retry because the branch
prediction logic of the Alpha architecture assumes that backward conditional
branches will be taken. Since this operation will rarely need to be retried, it is
more efficient to make a forward conditional branch which is assumed not to be
taken.
Because of the way atomicity is preserved on OpenVMS Alpha systems, this
guarantee of atomicity applies to both uniprocessor and multiprocessor systems.
This guarantee applies only to the actual modify instruction and does not extend
interlocking to subsequent or previous memory accesses (see Section 2.11.6).
The OpenVMS I64 version of the code uses the compare-exchange instruction
(cmpxchg) to implement the locked access, but the effect is the same: If other
code has modified the location being modified here, then this code will loop back
and retry the operation.
You should take special care in porting an application to an OpenVMS Alpha or
OpenVMS I64 system if it involves multiple processes that modify shared data
in a writable global section, even if the application executes only on a single
processor. Additionally, you should examine any application in which a mainline
process routine modifies data in process space that can also be modified by an
asynchronous system trap (AST) routine or condition handler. See Migrating to
How the MACRO Compiler Functions on Different Platforms 2–23