Forum Discussion
Altera_Forum
Honored Contributor
22 years agoNios Semaphore?
I need a good semaphore to make a fifo IRQ safe. On the PC it's easy with the assembly instruction BTS which Bit Tests and Sets. So you can try to set a bit and know if you did it in a single c...
Altera_Forum
Honored Contributor
22 years agoIt's particularly convenient to define a pair of functions (or macros)
int disable_interrupts (void);
restore_interrupts(int flag);
// start critical section
int flag = disable_interrupts();
// do stuff in an atomic manner
restore_interrupts(flag);
// end critical section where disable_interrupts() returns a flag indicating whether interrupts were enabled or not before the call, and restore_interrupts() restores them to the previous state. This makes it safe to put the critical section inside a function that might be called from a context where interrupts shouldn't be enabled (like inside another critical section, or during startup code). Unconditionally enabling interrupts (as opposed to restoring the previous state) is usually quite a rare operation: at the end of startup code once the interrupt service routines (ISRs) are setup, or within a NiosI ISR (since interrupts were disabled when the ISR was invoked).