__stdcx, __stwcx

Purpose

Store Doubleword Conditional Indexed, Store Word Conditional Indexed

Stores the value specified by val into the memory location specified by addr.

Prototype

int __stdcx(volatile long* addr, long val);

int __stwcx(volatile int* addr, int val);

Parameters

addr
The address of the variable to be updated. Must be aligned on a 4-byte boundary for a single word and on an 8-byte boundary for a doubleword.
value
The value which is to be assigned to addr.

Return value

Returns 1 if the update of addr is successful and 0 if it is unsuccessful.

Usage

This function can be used with a preceding __ldarx (or __lwarx) built-in function to implement a read-modify-write on a specified memory location. The two built-in functions work together to ensure that if the store is successfully performed, no other processor or mechanism can modify the target doubleword between the time the __ldarx function is executed and the time the __stdcx function completes. This has the same effect as inserting __fence built-in functions before and after the __stdcx built-in function and can inhibit compiler optimization of surrounding code.

__stdcx is valid only in 64-bit mode.