为什么"睡眠"同时举行了自旋锁不允许? [英] Why is "sleeping" not allowed while holding a spinlock?

查看:305
本文介绍了为什么"睡眠"同时举行了自旋锁不允许?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  为什么不睡觉,同时保持自旋锁?

据我所知,自旋锁应短时间的使用,并在code唯一的选择,如中断处理程序在哪里睡(preemption)是不允许的。

As far as I know, spinlocks should be used in short duration, and are only choices in code such as interrupt handler where sleeping (preemption) is not allowed.

不过,我不知道为什么会有这样的规定,应该有没有睡觉都拿着自旋锁。我知道这是不是一个推荐的做法(因为它在性能上是有害的),但我不明白为什么睡不应该在自旋锁被允许的。

However, I do not know why there is such a "rule" that there SHOULD BE no sleeping at all while holding a spinlock. I know that it is not a recommended practice (since it is detrimental in performance), but I see no reason why sleeps SHOULD NOT be allowed in spinlocks.

当你获得一个信号量不能容纳一个自旋锁,因为你可能都睡在等待信号量,同时保持自旋锁(引自Linux内核开发由罗伯特·爱)你无法入睡。

You cannot hold a spin lock while you acquire a semaphore, because you might have to sleep while waiting for the semaphore, and you cannot sleep while holding a spin lock (from "Linux Kernel Development" by Robert Love).

我能看到的唯一原因是为了可移植性原因,因为在单处理器,自旋锁作为禁止中断,并禁止中断执行,睡眠当然是不允许的(但睡眠不会打破在SMP系统code)

The only reason I can see is for portability reasons, because in uniprocessors, spinlocks are implemented as disabling interrupts, and by disabling interrupts, sleeping is of course not allowed (but sleeping will not break code in SMP systems).

但我想知道如果我的推论是正确的,或者有其他原因。

But I am wondering if my reasoning is correct or if there are any other reasons.

推荐答案

有几个原因,至少在Linux中,在自旋锁睡觉是不允许的:

There are several reasons why, at least in Linux, sleeping in spinlocks is not allowed:


  1. 如果线程A在一个自旋锁睡,而线程B再试图获取同一个自旋锁,单处理器系统就会死锁。线程B将永远不会去睡觉(因为自旋锁没有睡醒B当A被完成必要的等候名单),线程A将永远不会有机会醒来。

  2. 自旋​​锁使用了信号灯precisely因为他们更高效 - 的提供的你不争长。允许睡觉意味着你将有长期竞争周期,擦除使用自旋锁的所有好处。系统会更快只是用在这种情况下,一个信号量是

  3. 自旋​​锁经常被用来与中断处理程序同步,通过的此外的禁用中断。如果你的睡眠(一旦进入中断处理程序,就无法切换回线程让它醒来并完成其自旋锁的临界区)这个用例是不可能的。

  1. If thread A sleeps in a spinlock, and thread B then tries to acquire the same spinlock, a uniprocessor system will deadlock. Thread B will never go to sleep (because spinlocks don't have the waitlist necessary to awaken B when A is done), and thread A will never get a chance to wake up.
  2. Spinlocks are used over semaphores precisely because they're more efficient - provided you do not contend for long. Allowing sleeping means that you will have long contention periods, erasing all the benefit of using a spinlock. Your system would be faster just using a semaphore in this case.
  3. Spinlocks are often used to synchronize with interrupt handlers, by additionally disabling interrupts. This use case is not possible if you sleep (once you enter the interrupt handler, you cannot switch back to the thread to let it wake up and finish its spinlock critical section).

合适的工作使用正确的工具 - 如果你需要睡眠,信号量和互斥是你的朋友。

Use the right tool for the right job - if you need to sleep, semaphores and mutexes are your friends.

这篇关于为什么"睡眠"同时举行了自旋锁不允许?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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