Sets or clears bits in a variable atomically.
Standard C library (libc.a)
The fetch_and_and, fetch_and_andlp, fetch_and_or, and fetch_and_orlp subroutines respectively clear and set bits in a variable, according to a bit mask, as a single atomic operation.
The fetch_and_and and fetch_and_andlp subroutines clear bits in the variable that correspond to clear bits in the bit mask.
The fetch_and_or and fetch_and_orlp subroutines sets bits in the variable that correspond to set bits in the bit mask.
For 32-bit applications, the fetch_and_and and fetch_and_andlp subroutines are identical and operate on a word aligned single word (32-bit variable aligned on a 4-byte boundary). The fetch_and_or and fetch_and_orlp subroutines are identical and operate on a word aligned single word (32-bit variable aligned on a 4-byte boundary).
For 64-bit applications, the fetch_and_and and fetch_and_or operate on a word aligned single word (32-bit variable aligned on a 4-byte boundary). The fetch_and_addlp and fetch_and_orlp subroutines operate on a double word aligned double word (64-bit variable aligned on an 8 -byte boundary).
These operations are useful when a variable containing bit flags is shared between several threads or processes. When updating such a variable, it is important that the fetch, bit clear or set, and store operations occur atomically (are not interruptible). For example, consider the sequence of events which could occur if the operations were interruptible:
The result is that the update made by the second process is lost.
Traditionally, atomic access to a shared variable would be controlled by a mechanism such as semaphores. Compared to such mechanisms, the fetch_and_and, fetch_and_andlp, fetch_and_or, and fetch_and_orlp subroutines require very little overhead.
Item | Description |
---|---|
addr | Specifies the address of the variable whose bits are to be cleared or set. |
mask | Specifies the bit mask which is to be applied to the variable. |
These subroutines return the original value of the variable.