Spin_lock和Mutex_lock期间的Linux内核抢占 [英] Linux Kernel Preemption during spin_lock and mutex_lock

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

问题描述

当内核空间中的进程持有spin_lock时,由于以下任何一种情况,无法抢占该进程:

When a process in the kernel space is holding a spin_lock, the process cannot be preempted due to any of the following conditions :

  1. 该过程的时间片用完
  2. 当高优先级的流程可以运行时
  3. 发生中断时

但是,如果该进程阻塞,休眠或显式调用schedule(),则该进程可以使处理器屈服.我的理解正确吗?

However the process can yield the processor if it blocks, sleeps, or explicitly call schedule(). Is my understanding correct?

当内核空间中的某个进程持有一个mutex_lock时,由于上述列为1、2和3的条件,可以抢占该进程.

When a process in the kernel space is holding a mutex_lock, can the process be preempted due to the above conditions listed as 1, 2 and 3.

推荐答案

自旋锁的当前实现使用两种完全独立的机制来确保相互排斥,一种用于处理处理器间的排斥,另一种用于处理本地处理器的线程,并且中断处理程序.

Current implementations of spin locks use two entirely separate mechanisms to ensure mutual exclusion, one for dealing with inter-processor exclusion and one for dealing with the local processor threads and interrupt handlers.

  • 有spin_lock本身,仅在两个或多个处理器内核之间提供互斥体.遇到锁定的自旋锁的任何处理器基本上都会卡住,直到另一个处理器释放它为止.自旋锁在单处理器系统上没有任何作用,除了增加总死锁的机会外,因此通常在内核编译时删除.

  • There is the spin_lock itself which is only there to provide mutex between two or more processor cores. Any processor hitting a locked spin lock is basically stuck until another processor releases it. Spin locks serve no purpose on single processor systems - other than to increase the chance of total deadlock - so are usually removed at kernel compile time.

为提供本地处理器互斥锁,spin_lock()调用preempt_disable()(在抢先式调度系统上),以防止在持有该锁的同时运行任何其他线程.类似地,spin_lock_irqsave()也等效于local_irq_save()来禁用中断,以阻止其他任何事情在本地处理器上运行.

To provide local processor mutex, spin_lock() calls preempt_disable() (on pre-emptive scheduling systems) to prevent any other thread from running whilst the lock is held; similarly spin_lock_irqsave() also does the equivalent of local_irq_save() to disable interrupts to prevent anything else at all running on the local processor.

从以上显而易见,使用自旋锁可能会使整台机器胶粘,因此自旋锁应仅在很短的时间内使用,并且在握持锁时切勿做任何可能导致重新安排时间的事情.

As should be obvious from the above, using spin locks can gum up the whole machine so spin locks should just be used for very short periods of time and you should never do anything that might cause a reschedule whilst holding a lock.

mutex_lock的情况完全不同-只有尝试访问该锁的线程会受到影响,如果某个线程碰到了一个锁定的互斥锁,则会发生重新计划.因此,不能在中断(或其他原子)上下文中使用互斥锁.

The case with mutex_lock is totally different - only threads attempting to access the lock are affected and if a thread hits a locked mutex then a reschedule will occur. For this reason mutex_locks cannot be used in interrupt (or other atomic) contexts.

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

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