gpt4 book ai didi

linux-kernel - 关于 linux 内核中的信号量 up() 和 mutex_unlock()

转载 作者:行者123 更新时间:2023-12-04 14:24:31 26 4
gpt4 key购买 nike

我想知道为什么我们可以在中断上下文中使用信号量 up() 而互斥量的相同变体,即 mutex_unlock() 不能在中断上下文中使用。以下是内核的片段

/**
* mutex_unlock - release the mutex
* @lock: the mutex to be released
*
* Unlock a mutex that has been locked by this task previously.
*
* This function must not be used in interrupt context. Unlocking
* of a not locked mutex is not allowed.
*
* This function is similar to (but not equivalent to) up().
*/
void __sched mutex_unlock(struct mutex *lock)
{
#ifndef CONFIG_DEBUG_LOCK_ALLOC
if (__mutex_unlock_fast(lock))
return;
#endif
__mutex_unlock_slowpath(lock, _RET_IP_);
}EXPORT_SYMBOL(mutex_unlock);


/**
* up - release the semaphore
* @sem: the semaphore to release
*
* Release the semaphore. Unlike mutexes, up() may be called from any
* context and even by tasks which have never called down().
*/
void up(struct semaphore *sem)
{
unsigned long flags;

raw_spin_lock_irqsave(&sem->lock, flags);
if (likely(list_empty(&sem->wait_list)))
sem->count++;
else
__up(sem);
raw_spin_unlock_irqrestore(&sem->lock, flags);
}
EXPORT_SYMBOL(up);

最佳答案

mutex_unlock 需要一个 mutex-internal spinlock in a non-irq-safefashion - 例如如果设置了 CONFIG_PREEMPT_RTraw_spin_lock 可能会休眠 - 因此,如果时机正确,IRQ 上下文中的 mutex_unlock 将死锁。

关于linux-kernel - 关于 linux 内核中的信号量 up() 和 mutex_unlock(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48703000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com