尽管在这个论坛和所有其他论坛上已经多次讨论过这个话题,但我仍然有疑问。请帮忙。{}
宏在内核中的do{} while(0)
如何工作? 例如,
#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);
公顷s取代了mutex_lock()
。那么这个锁怎么样?
在我看来就像它重新定义该函数什么都不做。 – mpen 2010-09-22 06:34:51
@ Mark:听起来很有说服力。 ravspratapsingh:我们说得对,对于上面的两个语句,大括号之间确实没有什么关系。或者你是否用这种方法简化了代码?的 – chiccodoro 2010-09-22 06:44:49
可能重复[什么用做的,而(0)当我们定义一个宏?(http://stackoverflow.com/questions/923822/whats-the-use-of-do-while0-when-we- define-a-macro) – paxdiablo 2010-09-22 07:02:32