fetch_and_add Kernel Services

Purpose

Increments a variable atomically.

Syntax

#include <sys/atomic_op.h>

int fetch_and_add (addr,  value)
atomic_p addr;
int value;

long fetch_and_addlp (addr,  value)
atomic_l addr;
long value;

Parameters

Item Description
addr Specifies the address of the variable to be incremented.
value Specifies the value to be added to the variable.

Description

The fetch_and_add kernel services atomically increment a variable.

The fetch_and_add kernel service operates on a single word (32 bit) variable while the fetch_and_addlp kernel service operates on a double word (64 bit) variable.

These operations are useful when a counter variable is shared between several kernel threads, because it ensures that the fetch, update, and store operations used to increment the counter occur atomically (are not interruptible).

Note:
  • The single word variable for the fetch_and_add kernel service must be aligned on a word (32 bit) boundary.
  • The double word variable for the fetch_and_addlp kernel service must be aligned on a double word (64 bit) boundary.

Execution Environment

The fetch_and_add kernel services can be called from either the process or interrupt environment.

Return Values

The fetch_and_add kernel services return the original value of the variable.