arm-linux atomic operations

Atomic operations:
Atomic operations refer to operations that cannot be further divided. Generally, atomic operations are used for variable or bit operations.
use atomic operations to
Realize mutually exclusive access to the LED device, that is, only one application can use the LED light at a time.
In the module initialization
init function:

    /* Initialize atomic variables */

    atomic_set(&gpioled.lock, 1); /* The initial value of the atomic variable is 1 */

open function:

if (!atomic_dec_and_test(&gpioled.lock)) {

        atomic_inc(&gpioled.lock); /* If it is less than 0, add 1 to make the atomic variable equal to 0 */

        return -EBUSY; /* LED is used, return busy */

    }

If the atomic_dec_and_test function returns
If the return value is false, it means that the current value of the lock is negative (the lock value is 1 by default) , and there is only one possibility of the lock value being negative, then
Yes other devices are using the LED . If other devices are using LED lights, then you can only exit, call before exiting
The function atomic_inc adds 1 to the lock , because the value of the lock is reduced to a negative number at this time, it must be added 1 to the value of the lock
becomes 0 .

release function:

    /* Release the atomic variable when closing the driver file */

    atomic_inc(&dev->lock);

Guess you like

Origin blog.csdn.net/L1153413073/article/details/125531243