__fetch_and_swap, __fetch_and_swaplp

Purpose

Sets the word or doubleword specified by addr to the value of val and returns the original value of addr, in a single atomic operation.

Prototype

unsigned int __fetch_and_swap (volatile unsigned int* addr, unsigned int val);

unsigned long __fetch_and_swaplp (volatile unsigned long* addr, unsigned long 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.

Usage

This operation is useful when a variable is shared between several threads or processes, and one thread needs to update the value of the variable without losing the value that was originally stored in the location.

__fetch_and_swaplp is valid only in 64-bit mode.