I have a program that does serial communications. I use generic circular buffers for incoming and outgoing data. The buffers are accessed by the same code which is called by both the main program and from inside the serial interrupt handlers
I was getting glitches that I traced down to non-atomic access of the buffers by the main program.
It would be easy to stick a cli() and an sei() at the beginning and end of the buffer access routines. But that would enable interrupts in the middle of the serial interrupt handler. Not good.
So I save the interrupt status by saving (uint8_t int_reg = SREG) the entire SREG, just before executing the cli(); When I want to "restore" interrupts, I write the stored value back to SREG.
So, I believe this will work, and as far as I can tell, none of the other bits in SREG are going to be changed between the time I save and restore the SREG value. But are there any gotcha's?