fetch_and_and, fetch_and_or, fetch_and_andlp, and fetch_and_orlp Subroutines

Purpose

Sets or clears bits in a variable atomically.

Library

Standard C library (libc.a)

Syntax

#include <sys/atomic_op.h>
uint fetch_and_and (  addr, mask)
atomic_p addr;
unit mask;

ulong fetch_and_andlp ( addr, mask)
atomic_l addr;
ulong mask;

uint fetch_and_or (  addr,mask)
atomic_p addr;
intunit mask;

ulong fetch_and_orlp ( addr, mask)
atomic_l addr;
ulong mask;

Description

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:

  1. A process fetches the flags variable and sets a bit in it.
  2. A second process fetches the flags variable, sets a different bit, and stores it.
  3. The first process stores its value.

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.

Parameters

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.

Return Values

These subroutines return the original value of the variable.