Linux and Rt-linux on spinlock differences and usage considerations

Source: Internet
Author: User
Tags mutex

1. Origin of Spinlock

Spin Lock: An Access Protection lock on a shared resource, which uses a "spin" loop to wait for a mutex to be acquired.

在Linux中,有多套spinlock操作接口。但是,实现机制并不相同。根据使用场景,实现机制可能是“自旋“,也可能不是”自旋”方式。这些实现机制的共通特点是:以不产生调度的方式,获取互斥锁。但是,在实时Linux(RtLinux)中,spinlock却是产生调度的。
2. Spinlock use
spinlock的不产生调度机制决定了用途:1、对共享数据的访问操作时间一定要短。不同系统对实时性要求不同,这个时间要求有差异,应该控制在5us以内。2、spinlock接口性能较高,适用在高频繁使用,但是冲突又少的场景。3、spinlock一般作为系统基础接口,保护共享资源。例如,mutex中含有的基础变量的保护,需要使用spinlock接口。
与mutex的差异: mutex是以可产生调度的方式保护共享资源。当一方占用资源时,另一方可挂起等待,能够释放CPU资源。mutex在单核、多核、普通Linux,RtLinux中,都能对共享资源保护,没有差异,接口统一。mutex只能在线程上下文中,在允许调度的环境中才能使用。
spinlock可在中断、线程、单核、多核等场景中使用。在不同的配置下,实现方式不同,不同的场景需要选择不同的接口,在Linux中使用最为广泛。Spinlock使用场景宽泛,接口数量和使用限制多,容易导致错误,一定要注意。
3, the difference of spinlock in different Linux environment
Interface Linux Single Core Linux Multi-core Rt-linux
Spin_lock Lock preemption Lock preemption + spin mode lock variable Mutex Amount
Spin_lock_irq Lock Interrupt + lock preemption Lock Interrupt + lock preemption + spin mode lock variable Mutex Amount
Spin_lock_irqsave Lock Interrupt + lock preemption Lock Interrupt + lock preemption + spin mode lock variable Mutex Amount
Spin_lock_bh Lock Soft Interrupt Lock soft Interrupt + spin mode lock variable Lock Soft Interrupt + Mutex
Raw_spin_lock Lock preemption Lock preemption + spin mode lock variable With Linux single-core or multi-core
Raw_spin_lock_irq Lock Interrupt + lock preemption Lock Interrupt + lock preemption + spin mode lock variable With Linux single-core or multi-core
Raw_spin_lock_irqsave Lock Interrupt + lock preemption Lock Interrupt + lock preemption + spin mode lock variable With Linux single-core or multi-core
Raw_spin_lock_bh Lock Soft Interrupt Lock soft Interrupt + spin mode lock variable With Linux single-core or multi-core
3. Spinlock Usage Specification
普通linux:spin_lock:在线程间保持互斥保护spin_lock_irq:  在线程间、以及线程、中断、软中断间保持互斥同步Spin_lock_bh:  在线程间、以及线程和软中断间保持互斥同步
实时linux:spin_lock: 在线程间保持互斥同步spin_lock_irq:在线程间、以及线程、中断线程、软中断线程保持互斥同步spin_lock_bh: 在线程间、以及线程和软中断线程保持互斥同步raw_spin_lock_irq:在线程间、以及线程、中断、中断线程、软中断线程间保持互斥同步

' Blocking function ★★★★★ ' is not allowed in Spinlock's reserve

Linux and Rt-linux on spinlock differences and usage considerations

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.