怎么办{}而(0)在宏观调控工作? [英] how does do{} while(0) work in macro?

查看:161
本文介绍了怎么办{}而(0)在宏观调控工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然这个话题在这个论坛和所有其他论坛多次讨论,我还是有疑问。请帮助。

Though this topic has been discussed many times in this forum and all other forums, still I have doubts. Please help.

如何在做{}而(0)在Linux内核的宏观调控工作?
例如,

How does the do{} while(0) in macro work in Linux kernel? For example,

#define preempt_disable()    do { } while (0)

它如何禁用preempt?

How does it disable preempt?

#define might_resched()    do { } while (0)

它是如何重新安排?

How does it reschedule?

有关互斥锁等也同样地,我看到了宏。怎样帮助呢?我明白以下问题,但不是上面的例子。

Similarly I have seen macros for mutex locks and other also. How does this help? I understand for following problem but not for the examples above.

#define foo(x)    do { do something } while(0)

编辑:

有关下列code为 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 被定义在内核两个地方:

I am confused because rt_mutex_deadlock_account_lock is define at two places in the kernel:

内核/ rtmutex-debug.c

void rt_mutex_deadlock_account_lock(struct rt_mutex *lock, 
    struct task_struct *task)
{
    //....
}

内核/ rtmutex.h

#define rt_mutex_deadlock_account_lock(m, t) do { } while (0)

在在I2C驱动新内核2.6.35.4 rt_mutex_lock(安培;适配> bus_lock); 已取代了的mutex_lock()。请问这个锁呢?

In new kernel 2.6.35.4 in the i2c driver rt_mutex_lock(&adap->bus_lock); has replaced the mutex_lock(). How does this lock then?

推荐答案

@Kragen已经回答了什么做... while结构是 - 它基本上使得宏更安全的使用

@Kragen has answered what the do...while construct is for - it basically makes a macro much safer to use.

不过,我不认为它回答了这个问题:请问这是怎么工作的?

However, I don't think it answers the question of "how does this work?":

#define preempt_disable()    do { } while (0)

宏定义为的无能为力的。为什么你想什么都不做?

The macro is defined to do nothing. Why would you want to do nothing?


  • 在你想使用一个宏为做事的占位符某些情况下。比如,你可以写一个系统,其中preempt是不是一个问题,在code,但你知道code可能会被移植到一个系统,preempt需要一些特殊处理。所以,你使用宏无处不在第二个系统需要它(这样的处理很容易在以后启用),但对于第一个系统,你再定义宏作为一个空白宏。

  • In some cases you want to use a macro as a placeholder for doing something. For example, you might write code on one system where "preempt" isn't an issue, but you know the code might be ported to a system where "preempt" needs some special handling. So you use a macro everywhere the second system needs it (so that the handling is easy to enable later), but for the first system you then define that macro as a blank macro.

在某些情况下,你可能想要做这样的事情是由不同部分的任务(例如START_TABLE(); TABLE_ENTRY(1); TABLE_ENTRY(2); END_TABLE())。这使得一个很好的清洁明确的实施你的表。但是你发现,你实际上并不需要的END_TABLE()宏。为了保持客户端code整洁,你离开定义的宏,并简单地定义它什么都不做。这样一来,所有的表有一个END_TABLE和code是更容易阅读。

In some cases you may want to do things like a task that is made up of different parts, (e.g. START_TABLE(); TABLE_ENTRY(1); TABLE_ENTRY(2); END_TABLE();). This makes a nice clean clear implementation of your table. But then you find that you don't actually need the END_TABLE() macro. To keep the client code tidy, you leave the macro defined, and simply define it to do nothing. That way, all your tables have an END_TABLE and the code is easier to read.

一个类似的情况下,可以以两种状态(启用/禁用),其中一个国家需要宏做一些事情,但其他国家只是在默认情况下发生的,因此一个的实现是空的出现 - 你仍然使用宏,因为它使客户端code更容易理解,因为它明确规定这里的东西启用或禁用的地方。

A similar case can occur with two states (enable/disable) where one state needs the macro to do something, but the other state just happens by default, so the implementation of one is "empty" - you still use the macro because it makes the client code easier to understand, because it explicitly states the places where things are enabled or disabled.

这篇关于怎么办{}而(0)在宏观调控工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆