Note that if you wish to perform simple atomic operations, such as adding a value to a memory location, it isn't necessary to turn off interrupts to ensure that the operation won't be preempted. Instead, use the functions provided in the C include file <atomic.h>, which let you perform the following operations with memory locations in an atomic manner:

Function | Operation |
---|---|

atomic_add() | Add a number |

atomic_add_value() | Add a number and return the original value of *loc |

atomic_clr() | Clear bits |

atomic_clr_value() | Clear bits and return the original value of *loc |

atomic_set() | Set bits |

atomic_set_value() | Set bits and return the original value of *loc |

atomic_sub() | Subtract a number |

atomic_sub_value() | Subtract a number and return the original value of *loc |

atomic_toggle() | Toggle (complement) bits |

atomic_toggle_value() | Toggle (complement) bits and return the original value of *loc |

Note:
The *_value() functions may be slower on some
systems, so don't use them unless you really
want the return value.