本文介绍了“在(0)时做{...}"是什么意思?完全在内核代码中执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过很多这样的用法,尽管以前我想程序员想轻松地突破一段代码.为什么在这里(0)循环时需要一个do {...}?我们是否要告诉编译器一些东西?

I have seen a lot of usages like this, previously I though that the programmer wanted to break out of a block of code easily. Why do we need a do { ... } while (0) loop here? Are we trying to tell the compiler something?

例如在Linux内核2.6.25中,include/asm-ia64/system.h

For instance in Linux kernel 2.6.25, include/asm-ia64/system.h

/*
 * - clearing psr.i is implicitly serialized (visible by next insn)
 * - setting psr.i requires data serialization
 * - we need a stop-bit before reading PSR because we sometimes
 *   write a floating-point register right before reading the PSR
 *   and that writes to PSR.mfl
 */
#define __local_irq_save(x)         \
do {                    \
    ia64_stop();                \
    (x) = ia64_getreg(_IA64_REG_PSR);   \
    ia64_stop();                \
    ia64_rsm(IA64_PSR_I);           \
} while (0)

推荐答案

它总是在宏中使用,因此在调用后需要分号,就像在调用常规函数时一样.

It's always used in macros so that a semicolon is required after a call, just like when calling a regular function.

在您的示例中,您必须编写

In your example, you have to write

__local_irq_save(1);

同时

__local_irq_save(1)

将导致有关缺少分号的错误.如果do while不存在,则不会发生这种情况.如果仅要进行范围界定,那么一对简单的花括号就足够了.

would result in an error about a missing semicolon. This would not happen if the do while was not there. If it was just about scoping, a simple curly brace pair would suffice.

这篇关于“在(0)时做{...}"是什么意思?完全在内核代码中执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 13:53