Wie funktioniert {}, während (0) im Makro arbeitet?
Obwohl dieses Thema schon oft in diesem Forum und allen anderen Foren diskutiert wurde, habe ich immer noch Zweifel. Bitte helfen Sie.
Wie funktioniert dasdo{} while(0)
in Makro Arbeit im Linux-Kernel? Beispielsweise
#define preempt_disable() do { } while (0)
Wie wird Preempt deaktiviert?
#define might_resched() do { } while (0)
Wie wird neu geplant?
Ähnlich habe ich auch Makros für Mutex-Sperren und andere gesehen. Wie hilft das? Ich verstehe für folgendes Problem aber nicht für die obigen Beispiele.
#define foo(x) do { do something } while(0)
Bearbeiten
Was ist mit dem folgenden Code fürrt_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);
}
}
Ich bin verwirrt, weilrt_mutex_deadlock_account_lock
wird an zwei Stellen im Kernel definiert:
Imkernel/rtmutex-debug.c
:
void rt_mutex_deadlock_account_lock(struct rt_mutex *lock,
struct task_struct *task)
{
//....
}
Imkernel/rtmutex.h
:
#define rt_mutex_deadlock_account_lock(m, t) do { } while (0)
In neuem Kernel 2.6.35.4 im i2c-Treiberrt_mutex_lock(&adap->bus_lock);
hat das @ ersetmutex_lock()
. Wie sperrt das denn?