Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 5: Locking and Interprocess Communication


<asm-arch/atomic.h>
typedef struct { volatile int counter; } atomic_t;

Atomic Operations on Integers


Operation Effect

atomic_read(atomic_t *v) Reads the value of the atomic variable.

atomic_set(atomic_t *v, int i) Setsvtoi.

atomic_add(int i, atomic_t *v) Addsitov.

atomic_add_return(int i, atomic_t *v) Addsitovand returns the result.

atomic_sub(int i, atomic_t *v) Subtractsifromv.

atomic_sub_return(int i, atomic_t *v) Subtractsifromvand returns the result.

atomic_sub_and_test(int i, atomic_t *v) Subtractsifromv. Returns a true value if the result is 0,
otherwisefalse.

atomic_inc(atomic_t *v) Subtracts 1 fromv.

atomic_inc_and_test(atomic_t *v) Adds 1 tov.Returnstrueif the result is 0, otherwise
false.

atomic_dec(atomic_t *v) Subtracts 1 fromv.

atomic_dec_and_test(atomic_t *v) Subtracts 1 fromv.Returnstrueif the result is 0, other-
wisefalse.

atomic_add_negative(int i, atomic_t *v) Addsitov.Returnstrueif the result is less than 0,
otherwisefalse.

atomic_add_negative(int i, atomic_t *v) Addsitovand returnstrueif the result is negative,
otherwisefalse.

If the kernel was compiled without SMP support, the operations described are implemented in the same
way as for normal variables (onlyatomic_tencapsulation is observed) because there is no interference
from other processors.

The kernel provides thelocal_tdata type for SMP systems. This permits atomic operationson a single
CPU. To modify variables of this kind, the kernel basically makes the same functions available as for the
atomic_tdata type, but it is then necessary to replaceatomicwithlocal.

Notice that atomic variables are well suited for integer operations, but not so for bit operations. Each
architecture therefore has to define a set of bit manipulation operations, and these also work atomically
to provide coherence across processors on SMP systems. The available operations are summarized in
Section A.8.
Free download pdf