xv6---spinlock自旋锁
Spinlock
目录
Spinlock
自旋锁的结构体
自旋锁的方法
initlock-----设置锁的参数,名字
acquire----循环,直到可以拿到锁。
release---释放锁
holding---检查当前的cpu是否拿到了锁
死锁问题:
中断问题:
参考:xv6 Kernel-4 Spinlocks_哔哩哔哩_bilibili
自旋锁的结构体
// Mutual exclusion lock. struct spinlock {uint locked; // Is the lock held?// For debugging:char *name; // Name of lock.struct cpu *cpu; // The cpu holding the lock. };
自旋锁的方法
initlock-----设置锁的参数,名字
void initlock(struct spinlock *lk, char *name) {lk->name = name;lk->locked = 0;lk->cpu = 0; }
acquire----循环,直到可以拿到锁。
伪代码如下
func:if (*lock == 0) // spinlock空闲{*lock = 1;} else { //spinlock不空闲,循环等待goto func; }
- 上述的代码必须保证原子性,否则当同一个cpu的两个线程同时满足 *lock == 0,那么两个线程都会拿到spinlock,这肯定是不行的。
- 在xv6中,原子操作指令就是ampswap指令,对应到c语言就是__sync_lock_test_and_set函数来保证原子性。
__sync_synchronize(); 避免编译优化改变代码执行顺序。
// Acquire the lock. // Loops (spins) until the lock is acquired. void acquire(struct spinlock *lk) {push_off(); // disable interrupts to avoid deadlock.if(holding(lk))panic("acquire"); { //执行原子操作,避免出现并发问题// On RISC-V, sync_lock_test_and_set turns into an atomic swap:// a5 = 1// s1 = &lk->locked// amoswap.w.aq a5, a5, (s1)while(__sync_lock_test_and_set(&lk->locked, 1) != 0); }// Tell the C compiler and the processor to not move loads or stores// past this point, to ensure that the critical section's memory// references happen strictly after the lock is acquired.// On RISC-V, this emits a fence instruction.__sync_synchronize();// Record info about lock acquisition for holding() and debugging.lk->cpu = mycpu(); }
release---释放锁
释放锁:等价于操作lk->locked = 0 ,用__sync_lock_release(&lk->locked);实现。也是原子操作
// Release the lock. void release(struct spinlock *lk) {if(!holding(lk)) // 正常是当前cpu有spinlock, 所以才不会有panic("release");panic("release");lk->cpu = 0; //因为随后会释放锁,所以不妨设为空,表示当前没有cpu拿到锁// Tell the C compiler and the CPU to not move loads or stores// past this point, to ensure that all the stores in the critical// section are visible to other CPUs before the lock is released,// and that loads in the critical section occur strictly before// the lock is released.// On RISC-V, this emits a fence instruction.__sync_synchronize(); { // amoswap 原子操作// Release the lock, equivalent to lk->locked = 0.// This code doesn't use a C assignment, since the C standard// implies that an assignment might be implemented with// multiple store instructions.// On RISC-V, sync_lock_release turns into an atomic swap:// s1 = &lk->locked// amoswap.w zero, zero, (s1)__sync_lock_release(&lk->locked); }pop_off(); }
holding---检查当前的cpu是否拿到了锁
// Check whether this cpu is holding the lock. // Interrupts must be off. int holding(struct spinlock *lk) {int r;r = (lk->locked && lk->cpu == mycpu()); //获取当前cpu是否拿到了锁return r; }
死锁问题:
进程A:键盘打字---产生中断---acquired 等待spinlock----写入数据
进程B:某个调用拿到了spinlock, 等待键盘打字--写入的数据。
------>产生死锁
解决方法:acquire()的时候disable interupt , release的时候,enable interupt
额外的好处:可以让拿到spinlock的线程不会太久。
所以代码的push_off()和pop_off()中分别会打开/关闭中断来避免死锁
void push_off(void) { ...intr_off(); // 关闭中断 ... }void pop_off(void) { ...intr_on(); //打开中断 ... }
中断问题:
如果此时有三个调用去执行acquire(),其中一个调用拿到了spinlock, diable中断在执行结束后,会在release的时候去enable中断。查看spinlock的acquire实现(如下面的代码)
可知:如果在release的时候打开了中断,那么其他cpu会立刻拿到spinlock, 且这个时候已经关闭过了中断,然后中断又被打开了,这肯定不行!!!
void acquire(struct spinlock *lk) {push_off(); // disable interrupts to avoid deadlock.if(holding(lk))panic("acquire");// 原子操作---等待直到拿到spinlockwhile(__sync_lock_test_and_set(&lk->locked, 1) != 0); ............ }解决方案:加上计数器,实现如下:acquire一次,中断计数就增加(noff变量)
release一次就减1,直到为0才恢复中断(不一定会打开,需要根据,int old = intr_get()的值判断,old == 1代表acquires spinlock之前中断是打开的,反之用完了锁不打开)。
void push_off(void) {int old = intr_get(); // 调用push_off之前 打开了中断 if old != 0intr_off();if(mycpu()->noff == 0) // 第一次调用acquiremycpu()->intena = old; //intena保存锁之前的中断状态mycpu()->noff += 1; }void pop_off(void) {struct cpu *c = mycpu();if(intr_get())panic("pop_off - interruptible");if(c->noff < 1)panic("pop_off");c->noff -= 1;if(c->noff == 0 && c->intena) //直到aqcuire对应的release次数为0 && 调用push_off之前打开了中断intr_on(); //打开中断 }
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
