2

有没有更好的方法在 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;
}

是否有完全避免 DISI 的变体,因为“[...] 此指令不会阻止优先级 7 中断和陷阱运行。”

4

0 回答 0