虽然这个话题已经在这个论坛和所有其他论坛上讨论过很多次,但我仍然有疑问。请帮忙。
do{} while(0)
in 宏在 Linux 内核中是如何工作的?例如,
#define preempt_disable() do { } while (0)
它如何禁用抢占?
#define might_resched() do { } while (0)
它如何重新安排?
同样,我也看到了互斥锁和其他宏。这有什么帮助?我了解以下问题,但不了解上面的示例。
#define foo(x) do { do something } while(0)
编辑:
下面的代码rt_mutex_lock
呢?
/**
* rt_mutex_lock - lock a rt_mutex
*
* @lock: the rt_mutex to be locked
*/
void __sched rt_mutex_lock(struct rt_mutex *lock)
{
might_sleep();
rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
}
EXPORT_SYMBOL_GPL(rt_mutex_lock);
/*
* debug aware fast / slowpath lock,trylock,unlock
*
* The atomic acquire/release ops are compiled away, when either the
* architecture does not support cmpxchg or when debugging is enabled.
*/
static inline int rt_mutex_fastlock(struct rt_mutex *lock,
int state, int detect_deadlock, int (*slowfn)(struct rt_mutex *lock,
int state, struct hrtimer_sleeper *timeout, int detect_deadlock))
{
if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
rt_mutex_deadlock_account_lock(lock, current);
return 0;
} else{
return slowfn(lock, state, NULL, detect_deadlock);
}
}
我很困惑,因为rt_mutex_deadlock_account_lock
在内核的两个地方定义:
在kernel/rtmutex-debug.c
:
void rt_mutex_deadlock_account_lock(struct rt_mutex *lock,
struct task_struct *task)
{
//....
}
在kernel/rtmutex.h
:
#define rt_mutex_deadlock_account_lock(m, t) do { } while (0)
在新内核 2.6.35.4 中的 i2c 驱动程序rt_mutex_lock(&adap->bus_lock);
已替换mutex_lock()
. 那这个怎么锁?