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.
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.
本文详细阐述了Spinlock在并发编程中的使用原则,包括不能在持有Spinlock时进行睡眠操作、在获取Spinlock前禁用预取、仅在本地CPU上禁用中断、尽可能快速获取Spinlock等要点。同时强调了在特定情况下如何安全地避免死锁,如通过使用spin_lock_bh来确保硬件中断能够被服务。
1517

被折叠的 条评论
为什么被折叠?



