使用spinlock需要注意的事项

本文详细阐述了Spinlock在并发编程中的使用原则,包括不能在持有Spinlock时进行睡眠操作、在获取Spinlock前禁用预取、仅在本地CPU上禁用中断、尽可能快速获取Spinlock等要点。同时强调了在特定情况下如何安全地避免死锁,如通过使用spin_lock_bh来确保硬件中断能够被服务。
Concurrency IV -- Spinlock, may be used in code that can't sleep
Pinciples for using spinlock
1. CANNOT sleep when holding a spinlock
2. disable preemption before holding a apinlock
   this is done by kernel, the spinlock code itself
3. disable the interrupt on the local cpu only
4. hold the spin lock in minimum time
   If you have a spinlock that can be taken by code that runs
   in (hardware or software) interrupt context, you must use one
   of the forms of spin_lock that disables interrupts. Doing
   otherwise can deadlock the system, sooner or later. If you
   do not access your lock in a hardware interrupt handler, but
   you do via software interrupts (in code that runs out of a
   tasklet, for example, a topic covered in Chapter 7), you can
   use spin_lock_bh to safely avoid deadlocks while still allowing
   hardware interrupts to be serviced.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值