load-link與store-conditional (LL/SC)是一對用於併發同步訪問內存的CPU指令。Load-link返回內存位置處的當前值,隨後的store-conditional在該內存位置處保存新值(若是從load-link後沒有被修改)。這被用於實現無鎖算法與read-modify-write原子操做。php
linux
在<asm/atomic.h>
、<asm/system.h>
、<asm/cmpxchg.h>
、<asm/bitops.h>
、<asm/local.h>
中實現了多種基本的原子操做,以最簡單的atomic_add
來舉例:linux
/* * atomic_add - add integer to atomic variable * @i: integer value to add * @v: pointer of type atomic_t * * Atomically adds @i to @v. */ static __inline__ void atomic_add(int i, atomic_t * v) { if (kernel_uses_llsc && R10000_LLSC_WAR) { int temp; __asm__ __volatile__( " .set mips3 \n" "1: ll %0, %1 # atomic_add \n" " addu %0, %2 \n" " sc %0, %1 \n" " beqzl %0, 1b \n" " .set mips0 \n" : "=&r" (temp), "=m" (v->counter) : "Ir" (i), "m" (v->counter)); } else if (kernel_uses_llsc) { int temp; __asm__ __volatile__( " .set mips3 \n" "1: ll %0, %1 # atomic_add \n" " addu %0, %2 \n" " sc %0, %1 \n" " beqz %0, 2f \n" " .subsection 2 \n" "2: b 1b \n" " .previous \n" " .set mips0 \n" : "=&r" (temp), "=m" (v->counter) : "Ir" (i), "m" (v->counter)); } else { unsigned long flags; raw_local_irq_save(flags); v->counter += i; raw_local_irq_restore(flags); } }
其中,kernel_uses_llsc
是一個宏定義,當定義爲0
時就須要軟件實現。raw_local_irq_save
與raw_local_irq_restore
函數定義在<linux/irqflags.h>
,對於不一樣的mips cpu
有不一樣的實現,最簡單的實現以下:算法
.macro irq_enable_hazard; _ssnop; _ssnop; _ssnop;; .endm .macro irq_disable_hazard; nop; nop; nop; .endm .macro raw_local_irq_save result .set push .set reorder .set noat mfc0 \result, $12 ori $1, \result, 0x1f xori $1, 0x1f .set noreorder mtc0 $1, $12 irq_disable_hazard .set pop .endm .macro raw_local_irq_restore flags .set push .set noreorder .set noat mfc0 $1, $12 andi \flags, 1 ori $1, 0x1f xori $1, 0x1f or \flags, $1 mtc0 \flags, $12 irq_disable_hazard .set pop .endm
raw_local_irq_save
與raw_local_irq_restore
被實現爲兩個mips
的宏定義,raw_local_irq_save
對cp0
的status
寄存器進行修改,讓cpu
進入kernek mode
,ERL
與EXL
置0
,同時禁止中斷,從而保證了原子性。raw_local_irq_restore
將中斷使能打開。併發