Spin_lock_bh () and spin_unlock_bh ()

Source: Internet
Author: User

Spin_lock_bh is usually used in the process to prohibit the hijacking and soft interruption.


In spin_lock_bh (), local_bh_disable () is called to Disable software interruption of the current CPU. While the spin_unlock_bh () function calls local_bh_enable () to interrupt the software of the local CPU. When the software interruption is disabled, all soft interruptions of the local CPU will not be executed.


If a softirq shares data with the user context, there are two problems: first, the current user context may be interrupted by softirq; second, the critical section may enter another CPU. In this case, the spin_lock_bh () (include/Linux/spinlock. h) can be used. It will disable softirqs on that CPU and then get
Lock. Spin_unlock_bh () does the opposite. (For historical reasons, the suffix 'bh 'is the general name for the lower half, which is the old name of software interrupts. In fact, it is only appropriate to call spin_lock_softirq). You can also use spin_lock_irq () or spin_lock_irqsave () Here. This will not only disable softirqs, but also disable hardware interruption.

Tasklets and timer are actually softirq. From the locking point of view, tasklets and timers have the same status.


A hard interrupt usually communicates with a tasklet or softirq. This usually involves placing a task in a queue and then getting it from softirq. If a hardware interruption service routine shares data with a softirq, two considerations are required. First, the execution process of softirq may be interrupted by hardware; second, the critical section may be interrupted by hardware on another CPU. This is exactly where spin_lock_irq () comes in handy. It disables interruption on that CPU and obtains the lock. Spin_unlock_irq () does the opposite.

In a hardware interrupt service routine, you do not need to use spin_lock_irq () Because softirq cannot be executed during execution: it can use spin_lock (), which is faster. The only exception is that another hardware interruption service routine uses the same lock: spin_lock_irq () will disable that hardware interruption.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.