Counters That Need No Lock
For a single number shared between an interrupt handler and the rest of a driver, taking a lock is heavier than the work itself. An atomic integer is a value the processor can read, write, add to or compare-and-swap as one indivisible step, so two processors can never tear or lose an update. The kernel crate exposes these with explicit ordering, which says how much other memory traffic is allowed to be reordered around the operation. Reach for one when the shared state is genuinely a single counter or flag; reach for a lock the moment two values must agree with each other.
Questions this Concept answers
- Why does the `kernel` crate make you state a memory ordering on an atomic operation instead of always using the strongest one?
This Concept is waiting for its first lesson!
For a single number shared between an interrupt handler and the rest of a driver, taking a lock is heavier than the work itself. An atomic integer is a value the processor can read, write, add to or compare-and-swap as one indivisible step, so two processors can never tear or lose an update. The kernel crate exposes these with explicit ordering, which says how much other memory traffic is allowed to be reordered around the operation. Reach for one when the shared state is genuinely a single counter or flag; reach for a lock the moment two values must agree with each other.
Are you a teacher? Sign in to start contributing.
Sign In