atomic_t and spin_lock are implemented on the base of lock prefix of assembly.
#define spin_lock_string /
"/n1:/t" / "lock ; decb %0/n/t" / "jns 3f/n" / "2:/t" / "rep;nop/n/t" / "cmpb $0,%0/n/t" / "jle 2b/n/t" / "jmp 1b/n" / "3:/n/t"
above code is spin_lock of kernel. The third line uses the lock prefix and atomic operation on memory is guaranteed.
#define spin_unlock_string /
"movb $1,%0" /
:"=m" (lock->slock) : : "memory"
上面这段代码是内核的spin_lock的解锁代码,使用的是只写指令,而现在的x86中,内存只写指令本身就是原子操作,所以不必加lock前缀。
RCU:
内核怎么知道所有的读者都完成了读操作?
RCU读者是不允许阻塞,切换到用户模式和进入idle状态,所以如果以上三种状态发生,就可以说明读者都完成了读操作。