加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

在非preemtive linux内核上锁定 – spin_lock

发布时间:2020-12-14 01:36:49 所属栏目:Linux 来源:网络整理
导读:我在一个具有1个CPU和非预处理 Linux内核(2.6.x)的系统上读到,spin_lock调用相当于一个空调用,因此以这种方式实现. 我无法理解:它不应该相当于在互斥上睡一觉吗?即使在非preemtive内核上,中断处理程序仍然可能被执行,或者我可能会调用一个将原始线程置于休
我在一个具有1个CPU和非预处理 Linux内核(2.6.x)的系统上读到,spin_lock调用相当于一个空调用,因此以这种方式实现.

我无法理解:它不应该相当于在互斥上睡一觉吗?即使在非preemtive内核上,中断处理程序仍然可能被执行,或者我可能会调用一个将原始线程置于休眠状态的函数.因此,空的spin_lock调用是“安全的”并不是真的,如果它被实现为互斥锁的话.

有没有我得不到的东西?

解决方法

引自“Linux设备驱动程序”,Jonathan Corbet,Alessandro Rubini和Greg Kroah-Hartman:

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_ *形式.如果在您进入关键部分时中断到达,则不会这样做会使系统死锁.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读