虽然这个话题在本论坛和其他论坛已经被讨论了很多次,但我仍然有疑问。请帮忙。
do{} while(0)
宏在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驱动程序中的
mutex_lock()
已被rt_mutex_lock(&adap->bus_lock);
替换。那么这个锁是如何工作的呢?
do ... while (0)
,但是那里的代码在循环体内有动作,不像这里。所以这有些不同。 - Jonathan Lefflerdo{}while(0)
的含义,而是缺少函数体。 - RBerteig