- allow atomic access of variable
- set, read, test, add and subtract values
- good for protecting small data (e.g. counter, ...)
- very simple (-> hard to mess up
)
- bitmask operations also available
- implementation is architecture specific
example:
initalize:
atomic_t counter ATOMIC_INIT(0); /* or */
atomic_set(&counter, 0);
use:
atomic_add(3, &counter);
atomic_inc(&counter);
atomic_sub(2, &counter);
atomic_dec(&counter);
val = atomic_read(&counter);
operate and test:
if (atomic_sub_and_test(1, &counter) {
printk(KERN_INFO "counter is zero!\n");
}
...