gpt4 book ai didi

c - {} while(0) 在宏中如何工作?

转载 作者:太空狗 更新时间:2023-10-29 17:05:37 26 4
gpt4 key购买 nike

虽然这个话题在本论坛和所有其他论坛已经讨论了很多次,但我仍然有疑问。请帮忙。

宏中的 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 驱动程序 rt_mutex_lock(&adap->bus_lock); 替换了 mutex_lock()。那这个怎么锁呢?

最佳答案

参见 this link以获得比我能给出的更好的解释。

关于c - {} while(0) 在宏中如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3766827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com