Info

typedef struct { volatile int counter; } atomic_t;

Table 5-1: Atomic Operations

Operati

on

Effect

atomic_

_read(atomic_t *v)

Reads the value of the atomic variable.

atomic_

_set(atomic_t *v, int i)

Sets v to i.

atomic_

_add(int i, atomic_t *v)

Adds i to v.

atomic_

_add_return(int i, atomic_t

*v)

Adds i to v and returns the result.

atomic_

_sub(int i, atomic_t *v)

Subtracts i from v.

atomic_

_sub_return(int i, atomic_t

*v)

Subtracts i from v and returns the result.

atomic_

_sub_and_test(int i, atomic.

_t *

v)

Subtracts i from v. Returns a true value if the result is 0, otherwise false.

atomic_

_inc(atomic_t *v)

Subtracts 1 from v.

atomic_

_inc_and_test(atomic_t *v)

Adds 1 to v. Returns true if the result is 0, otherwise false.

atomic_

_dec(atomic_t *v)

Subtracts 1 from v.

atomic_

_dec_and_test(atomic_t *v)

Subtracts 1 from v. Returns true if the result is 0, otherwise false.

atomic_

_add_negative(int i, atomic.

_t *

v)

Adds i to v. Returns true if the result is less than 0, otherwise false.

atomic_

_add_negative(int i, atomic.

_t *

v)

Adds i to v and returns true if the result is negative, otherwise false.

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

The kernel provides the local_t data type for SMP systems. This permits atomic operations on a single CPU. To modify variables of this kind, the kernel basically makes the same functions available as for the atomic_t data type, but it is then necessary to replace atomic with local.

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.

Continue reading here: Spinlocks

Was this article helpful?

0 0