QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 4120|回复: 6

大家看看LINUX中C实现为什么总是"多此一举"?

[复制链接]
发表于 2006-9-11 18:43:00 | 显示全部楼层 |阅读模式
do { \
  inc_preempt_count(); \
  barrier(); \
} while (0)
上面是读写自旋锁实现时的一段代码,想问问各位为何要如此实现,而不是下面这样的实现(类似案例很多),
inc_preempt_count(); \
barrier(); \
即去掉do { )while (0),功能完全一样?
我想可能跟编译有关,但不甚明白,还请大虾不吝赐教,谢谢!
发表于 2006-9-12 12:45:05 | 显示全部楼层
关注
回复

使用道具 举报

 楼主| 发表于 2006-9-12 13:33:48 | 显示全部楼层
搜了一下,发现竟然好多人问这个问题.看看别人的回答.
do {......} while(0)

A few days ago a came across a macro in the linux kernel code

#define MACRONAME do {Some code} while (0)now I was wondering what is the use of having while(0)
here's the explaination for it, by Ben Collins , on kernelnewbies


It allows you to use more complex macros in conditional code. Imagine a macro of several lines of code like:

#define FOO(x)  printf("arg is %s\n", x); do_something_useful(x);Now imagine using it like:

        if (blah == 2)          FOO(blah);This interprets to:

        if (blah == 2)          printf("arg is %s\n", blah);          do_something_useful(blah);;As you can see, the if then only encompasses the printf(), and the do_something_useful() call is unconditional (not within the scope of the if), like you wanted it. So, by using a block like do{...}while(0), you would get this:

        if (blah == 2)          do {                  printf("arg is %s\n", blah);                  do_something_useful(blah);          } while (0);Which is exactly what you want.
(from Per Persson) As Collins point out, you want a block statement so you can have several lines of code and declare local variables. But then the natural thing would be to just use for example:

  #define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }However that wouldn't work in some cases. The following code is meant to be an if-statement with two branches:

  if(x>y)exch(x,y);          // Branch 1elsedo_something();     // Branch 2But it would be interpreted as an if-statement with only one branch:

  if(x>y) {          // Single-branch if-statement!!!int tmp;            // The one and only branch consiststmp = x;            // of the block.x = y;y = tmp;};                     // empty statementelse                  // ERROR!!! "parse error before else"do_something();The problem is the semi-colon (;) coming directly after the block.

The solution for this is to sandwich the block between do and while(0). Then we have a single statement with the capabilities of a block, but not considered as being a block statement by the compiler.

Our if-statement now becomes:

  if(x>y)do {int tmp;tmp = x;x = y;y = tmp;} while(0);elsedo_something();

又看:http://www.everything2.com/index.pl?node_id=1180050

答的真好!!!
回复

使用道具 举报

发表于 2006-9-12 22:24:33 | 显示全部楼层
的确是极好的解释!
不过下次请排一下版,中间的代码、注释什么的全乱了,搞得我看得糊涂。幸好找到了原文。还是谢谢楼主的好贴子! :D
原文链接:http://kernelnewbies.org/FAQ/DoWhile0

我还是排下版发上来吧。
============================================
FAQ/DoWhile0
Why do a lot of #defines in the kernel use do { ... } while(0)?

There are a couple of reasons:

(from Dave Miller) Empty statements give a warning from the compiler so this is why you see #define FOO do { } while(0).

(from Dave Miller) It gives you a basic block in which to declare local variables.

(from Ben Collins) It allows you to use more complex macros in conditional code. Imagine a macro of several lines of code like:

[code:1]#define FOO(x) \
        printf("arg is %s\n", x); \
        do_something_useful(x);
[/code:1]
Now imagine using it like:
[code:1]if (blah == 2)
        FOO(blah);
[/code:1]
This interprets to:
[code:1]if (blah == 2)
        printf("arg is %s\n", blah);
        do_something_useful(blah);;
[/code:1]
As you can see, the if then only encompasses the printf(), and the do_something_useful() call is unconditional (not within the scope of the if), like you wanted it. So, by using a block like do { ... } while(0), you would get this:
[code:1]if (blah == 2)
        do {
                printf("arg is %s\n", blah);
                do_something_useful(blah);
        } while (0);
[/code:1]
Which is exactly what you want.

(from Per Persson) As both Miller and Collins point out, you want a block statement so you can have several lines of code and declare local variables. But then the natural thing would be to just use for example:

[code:1]#define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }
[/code:1]
However that wouldn't work in some cases. The following code is meant to be an if-statement with two branches:
[code:1]if (x > y)
        exch(x,y);          // Branch 1
else  
        do_something();     // Branch 2
[/code:1]
But it would be interpreted as an if-statement with only one branch:
[code:1]if (x > y) {                // Single-branch if-statement!!!
        int tmp;            // The one and only branch consists
        tmp = x;            // of the block.
        x = y;
        y = tmp;
}
;                           // empty statement
else                        // ERROR!!! "parse error before else"
        do_something();
[/code:1]
The problem is the semi-colon (;) coming directly after the block. The solution for this is to sandwich the block between do and while (0). Then we have a single statement with the capabilities of a block, but not considered as being a block statement by the compiler. Our if-statement now becomes:
[code:1]if (x > y)
        do {
                int tmp;
                tmp = x;
                x = y;
                y = tmp;
        } while(0);
else
        do_something();
[/code:1]
回复

使用道具 举报

发表于 2006-9-14 08:20:21 | 显示全部楼层
<<Linux内核代码情景分析>>的基础知识部分有很详细的解释!
回复

使用道具 举报

发表于 2006-11-16 21:35:31 | 显示全部楼层
不错阿!有长见识了阿
回复

使用道具 举报

发表于 2006-11-23 17:57:08 | 显示全部楼层
厉害!这都能想到~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-4-23 19:45 , Processed in 0.186698 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表