按住自旋锁的内核抢占 [英] Kernel preemption while holding spinlock

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

问题描述

在linux内核中,如果在按住自旋锁的同时启用了内核抢占,怎么会发生死锁?

In the linux kernel, if kernel preemption is enabled while holding a spinlock, how can deadlock occur ?

推荐答案

内核抢占不能保证您没有死锁.

Kernel preemption doesn't guarantee that you don't have a deadlock.

一个线程可能仍然持有一个锁而没有释放它,并且如果其他某个线程想要获取相同的锁,那仍然会导致死锁.持有锁的线程必须决定释放它以避免死锁.也就是说,线程或其他逻辑必须抢占线程 并使其释放锁.内核本身无法导致线程释放锁.

A thread may still hold a lock without ever releasing it, and that would still cause a deadlock if some other thread wants to acquire that same lock. The thread that is holding the lock has to decide to release it to avoid deadlocks. That is to say the thread or some other logic has to preempt the thread and cause it to release the lock. The kernel itself can't cause the thread to release the lock.

内核可以简单地调度其他线程运行,但是如果某个其他线程依赖于第一个线程完成,那么该线程也将被阻塞.

The kernel simply can schedule other threads to run, but if some other thread depends on the first thread finishing then that thread will also get blocked.

例如:

线程A正在等待锁,以等待线程B已获取的某些共享资源.

Thread A is waiting on a lock for some shared resource that thread B has acquired.

线程A被抢占,线程B被调度.

Thread A get's preempted and thread B gets scheduled.

线程B正在等待锁,以等待线程A持有某些共享资源.

Thread B is waiting on a lock for some shared resource thread A is holding.

死锁.线程A和线程B都无法取得进展.

Deadlock. Neither thread A nor thread B can make progress.

要打破死锁,必须先抢占线程A或B才能释放它的锁.内核抢占无法做到这一点.

To break the deadlock something has to preempt thread A or B to release it's lock. Kernel preemption can't do that.

这篇关于按住自旋锁的内核抢占的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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