非抢占式Linux内核上的spin_lock [英] spin_lock on non-preemtive linux kernels

查看:121
本文介绍了非抢占式Linux内核上的spin_lock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,在具有1个CPU和非抢占式Linux内核(2.6.x)的系统上,spin_lock调用等效于一个空调用,并以此方式实现.

I read that on a system with 1 CPU and non preemtive linux kernel (2.6.x) a spin_lock call is equivalent to an empty call, and thus implemented that way.

我不明白:这不应该等同于互斥锁上的睡眠吗?甚至在非抢占式内核上,中断处理程序可能仍会执行,例如,或者我可能会调用使原始线程进入睡眠状态的函数.因此,并非空的spin_lock调用是安全的",就像将其实现为互斥锁一样.

I can't understand that: shouldn't it be equivalent to a sleep on a mutex? Even on non-preemtive kernels interrupt handlers may still be executed for example or I might call a function that would put the original thread to sleep. So it's not true that an empty spin_lock call is "safe" as it would be if it was implemented as a mutex.

我有没有得到的东西?

推荐答案

引用自«Linux设备驱动程序»,作者是Jonathan Corbet,Alessandro Rubini和Greg Kroah-Hartman:

Quoted from «Linux Device Drivers», by Jonathan Corbet, Alessandro Rubini and Greg Kroah-Hartman:

如果非抢占式单处理器系统曾经在 锁定,它将永远旋转;没有其他线程能够 获取CPU来释放锁(因为它无法屈服). 因此,单处理器系统上的自旋锁操作无需 优化了启用抢占的功能,但以下情况除外: 更改IRQ屏蔽状态的代码(在Linux中, spin_lock_irqsave()).由于抢占,即使您从未 期望您的代码在SMP系统上运行,您仍然需要实现 正确锁定.

If a nonpreemptive uniprocessor system ever went into a spin on a lock, it would spin forever; no other thread would ever be able to obtain the CPU to release the lock (because it couldn't yield). Because of this, spinlock operations on uniprocessor systems without preemption enabled are optimized to do nothing, with the exception of the ones that change the IRQ masking status (in Linux, that would be spin_lock_irqsave()). Because of preemption, even if you never expect your code to run on an SMP system, you still need to implement proper locking.

如果您对在中断上下文(硬件或软件)中运行的代码可以采取的自旋锁感兴趣,则必须使用spin_lock_*形式来禁用中断.进入关键部分后,中断到达后,不这样做将使系统死锁.

If you're interested in a spinlock that can be taken by code running in interrupt context (hardware or software), you must use a form of spin_lock_* that disables interrupts. Not doing so will deadlock the system as soon as an interrupt arrives while you have entered your critical section.

这篇关于非抢占式Linux内核上的spin_lock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆