Is there a better way to implement an interlocked variant of "compare and exchange" on a dsPIC33F?
unsigned int InterlockedCompareExchange(volatile unsigned int* Destination, unsigned int Exchange, unsigned int Compare) {
register unsigned int Old;
__asm volatile (
"DISI #3\n"
"MOV [%[DEST]], %[OLD]\n"
"CPSNE %[COMPARE], %[OLD]\n" // required 1 cycle if not skipped, 2 otherwise
"MOV %[EXCHANGE], [%[DEST]]\n" // is discarded if Compare NEQ [Destination]
: [OLD] "=&a" (Old), [DEST] "+r" (Destination)
: [EXCHANGE] "r" (Exchange), [COMPARE] "r" (Compare)
);
return Old;
}
Is there a variant to avoid DISI at all since "[...] this instruction does not prevent priority 7 interrupts and traps from running."