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.

"thread-safe" ISR

What is the best way to implement "thread-safe" access of global variables in an ISR?

I have a few global variables are are accessed both in the ISR and in main functions. If the ISR fires when a variable is accessed in the main code, I may get unpredictable results.

  • Henry,

    The F28x instruction set includes instructions that perform a read-modify-write.  Since these are single instructions, they are guaranteed to execute without being interrupted.  So as long as you're using these atomic instructions you don't have to "protect" the accesses because they would be inherently protected.  However, if you have some kind of structures where you need to modify multiple words without being interrupted for the duration of the entire set of modifications, then you would need to disable interrupts around the accesses.

    Assuming you're not writing your code in assembly, you'll want to know how to use the atomic instructions from C.  Check out the C Compiler User's Guide.  Do a search for the word "atomic".  There are a bunch of intrinsics such that when you use them you are guaranteed the compiler will utilize specific instructions.  For example, a few of the intrinsics include __inc, __dec, __add, __sub, __and, __or, etc. 

    Note that you should not need to use these atomic instructions from the ISRs, unless you're re-enabling interrupts within the ISR.  In other words, since interrupts are disabled upon entering your ISR you don't need to do anything special for these accesses in the ISR since there's no way to be pre-empted (unless you re-enable interrupts).

    If you're thinking about using threads, I highly recommend using our DSP/BIOS RTOS.  It has a lot of excellent capabilities built into it.  It's rock solid and best of all it's free!