Precautions to be taken while using Shadow registers across normal application code and interrupt routines

Version 1
    Question: How to use Shadow Registers across Normal Application code and interrupt routines without corrupting the data in shadow registers?



    If we use shadow registers in our Application code and also across interrupt routines, the following problem may occur.
    Problem :

    Updating a port pin through shadow registers requires two steps. 
    1.Update the shadow register 
    2.write back to Physical port.

    If some interrupt triggers after step 1 and before step2 and if ISR starts executing immediately, shadow register will reflect incorrect physical port values inside the ISR, since physical port has not been updated. If some function has to be done inside ISR depending on this port status, then that function may not be executed because of the wrong port status reflected by the shadow register. 

    Solution : 
    In the Application code, before updating the shadow registers, Disable the interrupts by clearing the corresponding INT_MSKx registers and Enable them back after writing back the data to physical ports. This makes sure that, interrupts that occur between the two steps will become posted interrupts and their corresponding ISR will be not be executed immediately. Once, the INT_MSKx bit is again set, Posted interrupts will become pending and ISR executes. This Will not corrupt the data in shadow registers and shadow registers will reflect the physical port values at all the times.