¿Cómo funciona {} while (0) en macro?
Aunque este tema se ha discutido muchas veces en este foro y en todos los demás foros, aún tengo dudas. Por favor ayuda.
Cómo hace eldo{} while(0)
en macro trabajo en kernel de Linux? Por ejemplo,
#define preempt_disable() do { } while (0)
¿Cómo deshabilita la preferencia?
#define might_resched() do { } while (0)
¿Cómo se reprograma?
Del mismo modo, he visto macros para bloqueos mutex y otros también. ¿Cómo ayuda esto? Entiendo por el siguiente problema, pero no por los ejemplos anteriores.
#define foo(x) do { do something } while(0)
Editar:
¿Qué pasa con el siguiente código parart_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);
}
}
Estoy confundido porquert_mutex_deadlock_account_lock
se define en dos lugares en el núcleo:
Enkernel/rtmutex-debug.c
:
void rt_mutex_deadlock_account_lock(struct rt_mutex *lock,
struct task_struct *task)
{
//....
}
Enkernel/rtmutex.h
:
#define rt_mutex_deadlock_account_lock(m, t) do { } while (0)
En el nuevo kernel 2.6.35.4 en el controlador i2crt_mutex_lock(&adap->bus_lock);
ha reemplazado elmutex_lock()
. ¿Cómo se bloquea esto entonces?