SPRUI04F july 2015 – april 2023
The C/C++ compiler supports three intrinsics for enabling, disabling, and restoring interrupts. The syntaxes are:
unsigned int | _disable_interrupts ( ); |
unsigned int | _enable_interrupts ( ); |
void | _restore_interrupts (unsigned int); |
The _disable_interrupts() and _enable_interrupts( ) intrinsics both return an unsigned int that can be subsequently passed to _restore_interrupts( ) to restore the previous interrupt state. These intrinsics provide a barrier to optimization and are therefore appropriate for implementing a critical (or atomic) section. For example,
unsigned int restore_value;
restore_value = _disable_interrupts();
if (sem) sem--;
_restore_interrupts(restore_value);
The example code disables interrupts so that the value of sem read for the conditional clause does not change before the modification of sem in the then clause. The intrinsics are barriers to optimization, so the memory reads and writes of sem do not cross the _disable_interrupts or _restore_interrupts locations.
The _restore_interrupts( ) intrinsic overwrites the CSR control register with the value in the argument. Any CSR bits changed since the _disable_interrupts( ) intrinsic or _enable_interrupts( ) intrinsic will be lost.
The _restore_interrupts( ) intrinsic does not use the RINT instruction.