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

标签 linux-kernel mutex semaphore

我想知道为什么我们可以在中断上下文中使用信号量 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-safe fashion - 例如如果设置了 CONFIG_PREEMPT_RTraw_spin_lock 可能会休眠 - 因此,如果时机正确,IRQ 上下文中的 mutex_unlock 将死锁。

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

相关文章:

operating-system - 为什么我们在生产者消费者问题中需要 2 个信号量变量?

linux "make oldconfig"丢弃条目 CONFIG_K8_NB

c - Init 中的 C 错误中的共享互斥锁

c - 对 `printk' 的 undefined reference

c++ - 访问类数据时替代互斥锁定/解锁

c - 使用信号量的进程之间的互斥

java - 带信号量的多线程

python - C 中灯开关信号量的正确形式

linux - 我应该使用什么 IDE 进行 Linux 模块开发?

c - 为什么我得到 "error: too few arguments to function ‘sock->ops->accept’"