自旋锁:自旋就是当进程不能获得锁时,它会while(1);循环。
2种方式退出:1》进程时间片消耗完。 2》得到锁。
所以spin lock属于非睡眠锁,可用于中断上下文。
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2> 进程间使用:
1》定义锁: spinlock_t lsx_lock;
2》初始化锁:spin_lock_init(spinlock_t *lock);
/*定义并初始化*/
DEFINE_SPINLOCK(lsx_lock)
3》上锁: spin_lock(spinlock_t *lock);
4》开锁:spin_unlock(spinlock_t *lock);
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3> 进程与中断间:
1>> 定义初始化锁:同上。
2>> 上锁:spin_lock_irqsave(spinlock *lock, unsigned long flags); //用flags保存先前的中断状态, 并关闭处理器所有中断。
/*spin_lock_irqsave()调用了arch_local_irq_save())禁用cpsr 的第i位*/
点击(此处)折叠或打开
-
static inline unsigned long arch_local_irq_save(void)
-
{
-
unsigned long flags;
-
-
asm volatile(
-
" mrs %0, cpsr @ arch_local_irq_save\n"
-
" cpsid i"
-
-
: "=r" (flags)
-
:
-
: "memory", "cc"
-
);
-
return flags;
- }
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4> 中断下半部:
1>>定义并初始化:同上;
2>>上锁:void spin_lock_bh(spinlock_t *lock); //只关软件中断,不关硬件中断
3>>开锁:void spin_unlock_bh(spinlock_t *lock);
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
补充:
进程上下文:
中断上下文:
软件中断:
硬件中断: