This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Can uint16_t variable read and write be considered atomic?

I am writing to unsigned 16 bits variable in a interrupt.

The Interrupt runs independently from the main program.

Main program reads that 16 bit variable carelessly from the interrupt.

Are those operation atomic?

Is the program safe from the memory corruption hazard?

  • I would say, yes. The MSP430 can read and write 16 bit entries atomically. (Of course, assuming that the entry isn't placed in a "packed" struct.)

    However, you must declare the variable "volatile". If you don't, the code generated for the main thread might assume that the value never changes. For example, a read can be hoisted out of a loop.

    If you use IAR Emebdded Workbench, this is guaranteed (see the manual for details). I guess that other tools also document this.

        -- Anders Lindgren, IAR Systems, Author of the IAR compiler for MSP430

  • I would say, yes. The MSP430 can read and write 16 bit entries atomically. (Of course, assuming that the entry isn't placed in a "packed" struct.)

    This question becomes much more interesting when dealing with peripheral registers, and especially with RMW access.

    However, you must declare the variable "volatile". If you don't, the code generated for the main thread might assume that the value never changes. For example, a read can be hoisted out of a loop.

    This is IMHO correct, but has nothing to do with "atomicity" (which is not implied, to be honest).

  • f. m. said:

    This question becomes much more interesting when dealing with peripheral registers, and especially with RMW access.

    Absolutely. In the IAR tools we take the approach that we use instructions to operate on memory location as much as possible, even when the variable is volatile. For example "var += 1;" result in a single ADD instruction. On the other hand, it is impossible to do the same for "var /= x;" as there is no division instruction on the MSP430.

    f. m. said:

    This is IMHO correct, but has nothing to do with "atomicity" (which is not implied, to be honest).

    True. It was just a friendly advice.
        -- Anders
  • It's a little bit more complicated than that. In general, what Anders says is true that the actual assignment or compare will be atomic (if the variable is properly aligned in memory). However, if the value that gets written to the uint16_t takes several machine-code instructions to piece together (say because you are combining several pieces of information into a bit-field) then certainly the what is one assignment line in C can be a ton of instructions, and could get interrupted in the middle if interrupts are still enabled.

    That is something to keep in mind.

    If there is a code block that needs to be atomic, then the best bet is to disable interrupts, set/check the value, then re-enable interrupts.

**Attention** This is a public forum