儘管在這個論壇和所有其他論壇上已經多次討論過這個話題,但我仍然有疑問。請幫忙。{}
宏在內核中的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