This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Sharing Volatile Global variable between ISRs

Hi yall

Could we expect problems (unexpected behavior) in 2478 if there is a variable thats being accessed by three different ISRs(specifically Timer ISRs) even after we declare that variable as volatile.

Thanks,
Nav

Parents
  • Depends on the size of the variable, and how it is used.

    Volatile just means it won't be cached. It doesn't say anything about atomicity.

    If the variable is too large for atomic updates, you may always get into troubles.

    If you have a 64-bit variable that requires two 32-bit reads, you can get an interrupt in-between the two reads, and the second read can happen after the variable has received a new value.

    But next thing is that in a a load/modify/store architecture, even a one-byte variable that gets read, modified and then stored means that you can get an interrupt between the involved instructions. Two interrupts can read the same value, perform their increment and then store back the result - so two increments resulted in the variable only being incremented once. Or one ISR does an increment and one does a decrement, and your variable may be decremented, unchanged or incremented depending on where the two ISR nested.

    So an important thing here is if you allow interrupt nesting or not. If you do allow interrupt nesting then you will probably need to disable interrupts (all or at least the interrupts who fights for the variable) while performing operations on the variable.

Reply
  • Depends on the size of the variable, and how it is used.

    Volatile just means it won't be cached. It doesn't say anything about atomicity.

    If the variable is too large for atomic updates, you may always get into troubles.

    If you have a 64-bit variable that requires two 32-bit reads, you can get an interrupt in-between the two reads, and the second read can happen after the variable has received a new value.

    But next thing is that in a a load/modify/store architecture, even a one-byte variable that gets read, modified and then stored means that you can get an interrupt between the involved instructions. Two interrupts can read the same value, perform their increment and then store back the result - so two increments resulted in the variable only being incremented once. Or one ISR does an increment and one does a decrement, and your variable may be decremented, unchanged or incremented depending on where the two ISR nested.

    So an important thing here is if you allow interrupt nesting or not. If you do allow interrupt nesting then you will probably need to disable interrupts (all or at least the interrupts who fights for the variable) while performing operations on the variable.

Children
No data